元组,列表,字典,集合比较内存大小

  • set(),list(),tuple() 方法只接受一个值,这个值可以是列表或者是元组, 否则报错 TypeError: set expected at most 1 arguments, got 3
  • dict() 方法接受相当于默认参数,key不用引号'',用=号相连
  • sys.getsizeof() 比较中,发现占用内存从小到大依次是:元组,列表,集合,字典

In [123]: import sys

In [127]: a_dict = dict(a='a1',b='b1',c='c1')

In [128]: a_dict
Out[128]: {'a': 'a1', 'b': 'b1', 'c': 'c1'}

In [129]: a_set = set('a','b','c')
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-129-2f7cc4a81ed4> in <module>()
----> 1 a_set = set('a','b','c')

TypeError: set expected at most 1 arguments, got 3

In [130]: a_set = set(('a','b','c'))

In [131]: a_set
Out[131]: {'a', 'b', 'c'}

In [132]: a_list = list('a1','a2','a3')
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-132-3e099cee627d> in <module>()
----> 1 a_list = list('a1','a2','a3')

TypeError: list() takes at most 1 argument (3 given)

In [133]: a_list = list(('a1','a2','a3'))

In [134]: a_list
Out[134]: ['a1', 'a2', 'a3']

In [135]: a_list = tuple('a1','a2','a3')
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-135-df0623f0640f> in <module>()
----> 1 a_list = tuple('a1','a2','a3')

TypeError: tuple() takes at most 1 argument (3 given)

In [136]: a_list = tuple(('a1','a2','a3'))

In [137]: a_list = list(('a1','a2','a3'))

In [138]: a_tuple = tuple(('a1','a2','a3'))

In [139]: a_tuple
Out[139]: ('a1', 'a2', 'a3')

In [140]: getsizeof(a_tuple),getsizeof(a_list),getsizeof(a_dict),getsizeof(a_set)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-140-e700af696dcc> in <module>()
----> 1 getsizeof(a_tuple),getsizeof(a_list),getsizeof(a_dict),getsizeof(a_set)

NameError: name 'getsizeof' is not defined

In [141]: sys.getsizeof(a_tuple),sys.getsizeof(a_list),sys.getsizeof(a_dict),sys.getsizeof(a_set)
Out[141]: (72, 112, 272, 224)

In [142]:

dict的用法:

>>>dict()                        # 创建空字典
{}
>>> dict(a='a', b='b', t='t')     # 传入关键字
{'a': 'a', 'b': 'b', 't': 't'}
>>> dict(zip(['one', 'two', 'three'], [1, 2, 3]))   # 映射函数方式来构造字典
{'three': 3, 'two': 2, 'one': 1} 
>>> dict([('one', 1), ('two', 2), ('three', 3)])    # 可迭代对象方式来构造字典
{'three': 3, 'two': 2, 'one': 1}
>>>

dict 的错误用法

In [142]: dict_2 = dict(1=11,2=12,3=13)
  File "<ipython-input-142-7080a8571a4e>", line 1
    dict_2 = dict(1=11,2=12,3=13)
SyntaxError: keyword can't be an expression


In [143]: dict_2 = dict(1='11',2='12',3='13')
  File "<ipython-input-143-1703496c46f8>", line 1
    dict_2 = dict(1='11',2='12',3='13')
SyntaxError: keyword can't be an expression

In [144]: a_dict = dict(a='a1',b='b1',c='c1')

In [145]: dict_2 = dict('1'=11,'2'=12,'3'=13)
  File "<ipython-input-145-55327d34ed6c>", line 1
    dict_2 = dict('1'=11,'2'=12,'3'=13)
SyntaxError: keyword can't be an expression

In [147]: dict_2 = dict(a1=11,a2=12,a3=13)

In [149]: dict_2
Out[149]: {'a1': 11, 'a2': 12, 'a3': 13}

In [150]:

但是速度方面恰好相反: tuple < list < set < dict 数量多这样考虑才有意义,否则没意义

In [172]: a_set = set(('a','b','c'))

In [173]: a_list = list(('a1','a2','a3'))

In [174]: a_tuple = tuple(('a1','a2','a3'))

In [175]: a_dict
Out[175]: {'a': 'a1', 'b': 'b1', 'c': 'c1'}

In [176]: timeit 'a' in a_tuple
The slowest run took 27.16 times longer than the fastest. This could mean that an intermediate result is being cached.
10000000 loops, best of 3: 55.3 ns per loop

In [177]: timeit 'a' in a_list
10000000 loops, best of 3: 53.7 ns per loop

In [178]: timeit 'a' in a_set
10000000 loops, best of 3: 33.1 ns per loop

In [179]: timeit 'a' in a_dict
10000000 loops, best of 3: 36 ns per loop
写入自己的博客中才能记得长久
原文地址:https://www.cnblogs.com/heris/p/14112072.html