当前位置 博文首页 > 4种非常实用的python内置数据结构

    4种非常实用的python内置数据结构

    作者:yanxiangtianji 时间:2021-06-17 18:30

    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)
    for s in scores :
    	if s not in do:
    		do[s] = 1
    	else:
    		do[s] += 1
    print(od)
    

    Named Tuple

    编程实践中我们经常需要创建一些小的数据结构用来整合一组相关联的数据,简单的比如地理坐标的经纬度,颜色的RGB值或者矩形框的左上和右下坐标,复杂的比如构造一个窗口的一组参数。
    实践中,我们通常有3中实现方法:

    • 对每一个这样的数据结构创建一个class。优点是可以直接使用名字访问数据成员,而且支持复杂的访问逻辑和数据操作。缺点是需要编写对应的类和必须的函数,管理文件和引用关系。
    • 使用tuple。优点是编写简单,内存使用效率高。缺点是只能使用下标访问,可读性差,容易出错。
    • 使用dict,用str来作为对于属性的名字。优点是编写相对简单,而且保留了变量的名字。缺点是需要使用字符串表示名字较为麻烦,而且每一个结构都要保存作为名字的字符串,浪费空间。

    collections的nametuple可以为我们直接构造一个具有名字的简单类型,方便快捷地实现类似手写了一个class的效果。
    需要注意的是collections.nametuple是一个factory function,它用来帮我们创建一个类型,而不是这个类型的具体对象。创建类型时,我们可以指定各个属性的名字,之后就可以使用.来访问了,而且它同时还支持使用下标访问。同时Named Tuple还支持_asdict函数用来将内部的数值转换成一个dict。

    # class
    class Rect:
    	def __init__(self, x1, y1, x2, y2):
    		self.x1 = x1
    		self.y1 = y1
    		self.x2 = x2
    		self.y2 = y2
    		
    def area_class(r):
    	w = r.x2 - r.x1
    	h = r.y2 - r.y1
    	return w*h
    
    r1 = Rect(1,3,5,5)
    # <__main__.Rect object at 0x7fde252a87f0>
    # to show its content, we need to implement __repr__(self) or __str__(self)
    print(area_class(r1))
    
    # tuple
    def area_tuple(r):
    	w = r[2]-r[0]
    	h = r[3]-r[1]
    	return w*h
    
    r2 = (1,3,5,5)
    print(r2)
    # (1, 3, 5, 5)
    print(area_tuple(r2))
    
    # dict
    def area_dict(r):
    	w = r["x2"] - r["x1"]
    	h = r["y2"] - r["y1"]
    	return w*h
    
    r3 = {"x1":1, "y1":3, "x2":5, "y2":5}
    print(r3)
    # {'x1': 1, 'y1': 3, 'x2': 5, 'y2': 5}
    print(area_tuple(r3))
    
    # named tuple
    import collections
    Rectangle = collections.namedtuple("Rectangle", ["x1", "y1", "x2", "y2"])
    
    def area_namedtuple(r):
    	w = r.x2 - r.x1
    	y = r.y2 - r.y1
    	return w*h
    
    r4 = Rectangle(1,3,5,5)
    print(r4)
    # Rectangle(x1=1, y1=3, x2=5, y2=5)
    x1,y2,x2,y2 = r4
    print(x1,y2,x2,y2)
    # 1 3 5 5
    print(area_namedtuple(r4))
    print(area_class(r4)) # work with "." grammar
    print(area_tuple(r4)) # work with index
    print(area_dict(r4._asdict())) # work with dict
    

    Counter

    顾名思义,Counter是用来对元素进行计数的,它也是collections这个包里的。根据Python的官方文档,它是dict类型的一个子类。
    在构造的时候输入一个iterable的类型,比如list,range或是一个mapping的类型,比如dict,defaultdict。然后Counter就会对其中的元素进行计数。
    比较特殊的是,Counter对负数没有做特殊处理,就是说在特殊操作下允许出现测试为负,后面我们会有例子。

    c = Counter()                           # a new, empty counter
    c = Counter('gallahad')                 # a new counter from an iterable
    print(c)
    # Counter({'a': 3, 'l': 2, 'g': 1, 'h': 1, 'd': 1})
    c = Counter({'red': 4, 'blue': 2})      # a new counter from a mapping
    print(c)
    # Counter({'red': 4, 'blue': 2})
    c = Counter(cats=4, dogs=8)             # a new counter from keyword args
    print(c)
    # Counter({'dogs': 8, 'cats': 4})
    

    除了基本的计数功能,它还支持一些常用的相关功能。比如:

    • 按照频率排序(most_common([n]))。其中n是可选输入,表示返回前n个最频繁的元素和他们的频率。默认情况下返回所有的元素。
    • 按照频率输出元素本身(elements())。它会返回元素本身,但是元素的顺序不是原来的,相同的元素会连续输出。不同元素之间,按照他们的出现顺序输出,这一点是OrderedDict以及3.7之后的dict所提供的特性。
    • 两个Counter相减(substract(c))。它可以从第一个counter上减去第二个counter中对应元素出现的次数。对于只出现在第二个coutner中元素,默认其在第一个counter中出现0次。
    c = Counter(a=4, b=2, c=0, d=-2)
    sorted(c.elements())
    # ['a', 'a', 'a', 'a', 'b', 'b']
    Counter('abracadabra').most_common(3)
    # [('a', 5), ('b', 2), ('r', 2)]
    
    c1 = Counter(a=4, b=2, d=-2)
    c2 = Counter(a=1, b=2, c=3, d=4)
    c1.subtract(c2)
    c1
    # Counter({'a': 3, 'b': 0, 'c': -3, 'd': -6})
    

    更多的参考信息大家可以参考官方文档:

    https://docs.python.org/3/library/collections.html

    js