defmin_val(data):"""Find minimum value from the data list."""returnmin(data, key=lambda x:len(x))
改為return min(data, key=lambda x: min(x))
p27
1.3.6 為什么range函數在Python3中更好 譯者翻譯有誤
原文是:
If you have worked with Python 2, you might have used xrange. In Python 3,
xrange has been renamed to range with some extra features. range is
similar to xrange and generate an iterable.
exception Exception
All built-in, non-system-exiting exceptions are derived from this
class.
All user-defined exceptions should also be derived from this class.
1.4.4 只處理特定的異常 這里描述錯誤, 應該是Get list of even numbers from given list
defget_even_list(num_list):"""Get list of odd numbers from given list."""# This can raise NoneType or TypeError exceptionsreturn[item for item in num_list if item%2==0]
原文:
They don’t allow duplicates.
? You can’t access set elements using an index.
? Sets can access elements in O(1) time since they use
hashtables.
? Sets don’t allow some common operations that lists do
like slicing and lookups.
? Sets can sort the elements at insertion time.
譯文:
? 集合元素不能重復
? 不支持索引訪問集合里的元素
譯者翻譯錯誤,集合底層就是散列表,而不是使用散列表
? 集合使用散列表之后,可以在O(1)時間內訪問元素
譯者翻譯錯誤,"糾正"了原文,很遺憾,也是錯誤的,集合不支持,原文是正確的
? 集合支持一些常見的操作,如列表的切片和查詢
集合是無序的,這條是不成立的
? 集合可以在插入元素時對元素進行排序
原文:
Sets are implemented using hashtables, so whenever a new item is
added to a set, the positioning of the item in memory is determined by
the hash of the object.
譯文:
集合是使用散列表實現的,因此每當一個新項添加到集合中時, 該項在內存中的
位置由散列的對象確定.
p39
代碼清單2-2 使用集合去重 描述錯誤集合
原文:
集合可用作字典的鍵, 也可以使用集合用作其他數據結構的鍵, 如列表(list)
集合是可變的, 不可哈希,不能用于字典的鍵
In [1]: l = {1, 2, 3}
In [2]: l.__hash__ is None
Out[2]: True
In [3]: getattr(l, "__hash__") is None
Out[3]: True
In [4]: callable(l.__hash__)
Out[4]: False
In [5]: hash(l)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Input In [5], in <cell line: 1>()
----> 1 hash(l)
TypeError: unhashable type: 'set'
正確譯文
csv
Use csv for reading and writing CSV files. It will save you lot of time instead
of writing your own methods while reading files.
csv模塊用于讀寫csv文件. 它能幫助你讀寫時節省大量的時間,你不需要重復造輪子
p50
代碼清單2-15 使用__future__
from __future__ import division
p54
2.2.3 有序字典、默認字典、普通字典 對于描述部分有"直譯"
As of Python 3.6, dicts are now ordered by insertion order, which
actually reduces the usefulness of ordereddict
python3.6之后(包含), 字典根據插入順序排序, 所以ordereddict自然就基本上沒有
用武之地了,除非要兼容
p64
代碼清單3-5 引發一個異常而不是None 正確代碼如下
#!usr/bin/python# *- coding:utf8 -*-defread_lines_for_python(file_name, file_type):if file_type notin("txt","html"):raise ValueError("Not correct file format")ifnot file_name:raise IOError("File Not Found")filename ="".join([file_name,".", file_type])withopen(filename,"r")as fileread:for line in fileread:if"python"in line:return"Found Python"if __name__ =='__main__':ifnot read_lines_for_python("file_without_python_name","pdf"):print("Python keyword doesn't exists in file")
p66
代碼清單3-11 顯示地返回None 譯文出現"直譯"
正確譯文如下
Here you expect the result to be a value in the sum function, which is
misleading because it could return None or a sum of two numbers. So, you
always need to check the result for None, which is too much noise in the
code and makes the code more complex over time
你期望sum函數返回的是一個值, 如果返回None或者兩數之和會是一種誤導
在這種情況下,你需要不斷的檢查sum的返回結果是否為None. 代碼會隨著時間變得
混亂和復雜
deffind_odd_number(numbers):odd_numbers =[]ifnotisinstance(numbers,list):returnNonefor item in numbers:if item %2!=0:odd_numbers.append(item)return odd_numbersnum = find_odd_number([2,4,6,7,8,10])# return 7
num = find_odd_number((2,4,6,7,8,10))# return None
num = find_odd_number([2,4,6,8,10])# return []"""
This function by default returns None if it does not find an odd
number. The function also returns None if the type of numbers is not a list.
如果numbers的類型不是list,那么返回的是[]空列表,不是None
"""