array
Python不仅仅可以使用内置的list实现数组,还支持像C语言那样的指定类型的原生数组array。
很显然,因为list可以存储各种类型的对象,而array只存储一个指定的原生类型,所以当数据量较大时,原生array在内存占用方面要比list小。
而且array不像C语言里那样在定义时就限制了大小,它支持list所支持的各种常用函数。相比之下Python的array更像是C++的vector。
from array import array l = list(range(100)) a = array.fromlist(l) print(l.__sizeof__(), a.__sizeof__())
目前array有两个限制。首先,它只支持整数、小数、unicode字符,而不能像C++的vector那样支持多种数据类型。另外目前指定类型比较麻烦,我们需要使用类型对应的字母缩写来指定,而不能使用简单的诸如int,float的方式。
a = array('i') a.append(1) a.append(4)
Type code | C Type | Python Type | Minimum size in bytes |
‘b’ | signed char | int | 1 |
‘B’ | unsigned char | int | 1 |
‘u’ | wchar_t | Unicode character | 2 |
‘h’ | signed short | int | 2 |
‘H’ | unsigned short | int | 2 |
‘i’ | signed int | int | 2 |
‘I’ | unsigned int | int | 2 |
‘l’ | signed long | int | 4 |
‘L’ | unsigned long | int | 4 |
更详细的信息可以参考:https://docs.python.org/3.8/library/array.html
defaultdict
C++的map对于新的key会自动使用value type的默认构造函数构造一个值,而Python默认的dict对于不存在的key的访问会抛出异常(赋值除外)。这是因为Python不知道value的类型,所以没办法为我们默认构造。
defaultdict要求我们在构造时指定一个类型,然后会自动根据需要初始化value。这样我们就可以使用简单的代码来实现很多功能。
下面的代码,我对比了使用defaultdict和original dict实现将学生按照姓的首字母分组的功能,以及分类计数的功能。
import collections students = ['Zhang San', 'Li Si', 'Zhou liu', 'Chen qi', 'Cheng ba'] # using defaultdict dd = collections.defaultdict(list) for s in students: key = s[0] dd[key].append(s) print(dd) # using original dict (method 1) od = {} for s in students: key = s[0] if key not in do: od[key] = [] od[key].append(s) print(od) scores = ['A', 'B', 'C', 'A', 'A', 'B', 'C', 'B', 'A', 'A'] # using defaultdict dd = collections.defaultdict(int) for s in scores : dd[s] += 1 print(dd) # using original dict (method 2) od = collections.defaultdict(int) fo<b style="color:transparent">本文来源gao@!dai!ma.com搞$$代^@码!网!</b>r s in scores : if s not in do: do[s] = 1 else: do[s] += 1 print(od)