标签:视图 一个 color col dex append 元素 姓名 添加元素
列表是可修改的序列类型。所以列表不可以作为字典的键。
1 >>> a = [1] 2 >>> hash(a) 3 Traceback (most recent call last): 4 File "<pyshell#1>", line 1, in <module> 5 hash(a) 6 TypeError: unhashable type: ‘list‘
1 >>>s = [1,3] 2 >>> s 3 [1, 3] 4 >>> s.extend([4]) 5 >>> s 6 [1, 3, 4] 7 >>> s += [6,7] 8 >>> s 9 [1, 3, 4, 6, 7] 10 >>> s *= 2 11 >>> s 12 [1, 3, 4, 6, 7, 1, 3, 4, 6, 7] 13 >>> s.insert(3, 10) 14 >>> s 15 [1, 3, 4, 10, 6, 7, 1, 3, 4, 6, 7] 16 >>> s.pop() 17 7 18 >>> s 19 [1, 3, 4, 10, 6, 7, 1, 3, 4, 6] 20 >>> s.pop(3) 21 10 22 >>> s 23 [1, 3, 4, 6, 7, 1, 3, 4, 6] 24 >>> s.remove(1) 25 >>> s 26 [3, 4, 6, 7, 1, 3, 4, 6] 27 >>> s.reverse() 28 >>> s 29 [6, 4, 3, 1, 7, 6, 4, 3] 30 >>> s.remove(100) 31 Traceback (most recent call last): 32 File "<stdin>", line 1, in <module> 33 ValueError: list.remove(x): x not in list
1 >>> t = 1 2 >>> hash(t) 3 3430019387558
1 >>> t = tuple() 2 >>> print(t) 3 () 4 >>> t = 1, 5 >>> print(t) 6 (1,) 7 >>> t[0] 8 1 9 >>> t[1] 10 Traceback (most recent call last): 11 File "<stdin>", line 1, in <module> 12 IndexError: tuple index out of range 13 >>> t2 = (1,2,3) 14 >>> print(t2) 15 (1, 2, 3) 16 >>> t3 = tuple([1,2,3,4]) 17 >>> print(t3) 18 (1, 2, 3, 4) 19 >>> t4 = tuple(range(10)) 20 >>> print(t4) 21 (0, 1, 2, 3, 4, 5, 6, 7, 8, 9) 22 >>> print(t4[3]) 23 3 24 >>> print(t4[3:8]) 25 (3, 4, 5, 6, 7)
1 >>> a = dict(one=1, two=2, three=3) 2 >>> b = {‘one‘: 1, ‘two‘: 2, ‘three‘: 3} 3 >>> a 4 {‘one‘: 1, ‘two‘: 2, ‘three‘: 3} 5 >>> b 6 {‘one‘: 1, ‘two‘: 2, ‘three‘: 3} 7 >>> c = dict(zip([‘one‘, ‘two‘, ‘three‘], [1,2,3])) 8 >>> c 9 {‘one‘: 1, ‘two‘: 2, ‘three‘: 3} 10 >>> d = dict([(‘one‘, 1), (‘two‘, 2), (‘three‘, 3)]) 11 >>> d 12 {‘one‘: 1, ‘two‘: 2, ‘three‘: 3}
1 >>> d = {} 2 >>> d[‘one‘] = 1 3 >>> d[‘two‘] = 2 4 >>> d 5 {‘one‘: 1, ‘two‘: 2} 6 >>> del d[‘not_exists‘] 7 Traceback (most recent call last): 8 File "<stdin>", line 1, in <module> 9 KeyError: ‘not_exists‘ 10 >>> del d[‘one‘] 11 >>> d 12 {‘two‘: 2} 13 >>> d.clear() 14 >>> d 15 {} 16 >>> len(d) 17 0 18 >>> d = dict.fromkeys(‘abc‘, 2) 19 >>> d 20 {‘a‘: 2, ‘c‘: 2, ‘b‘: 2} 21 >>> d.get(‘a‘, 100) 22 2 23 >>> d.get(‘f‘, 100) 24 100 25 >>> d 26 {‘a‘: 2, ‘c‘: 2, ‘b‘: 2} 27 >>> for i in d.items(): print(i) 28 ... 29 (‘a‘, 2) 30 (‘c‘, 2) 31 (‘b‘, 2) 32 >>> for i in d.keys(): print(i) 33 ... 34 a 35 c 36 b 37 >>> d[‘c‘] = 5 38 >>> for i in d.values(): print(i) 39 ... 40 2 41 5 42 2 43 >>> d.pop(‘f‘) 44 Traceback (most recent call last): 45 File "<stdin>", line 1, in <module> 46 KeyError: ‘f‘ 47 >>> d.pop(‘f‘, 99) 48 99 49 >>> d 50 {‘a‘: 2, ‘c‘: 5, ‘b‘: 2} 51 >>> d.popitem() 52 (‘a‘, 2) 53 >>> d 54 {‘c‘: 5, ‘b‘: 2} 55 >>> d.setdefault(‘c‘, 99) 56 5 57 >>> d 58 {‘c‘: 5, ‘b‘: 2} 59 >>> d.setdefault(‘e‘, 99) 60 99 61 >>> d 62 {‘e‘: 99, ‘c‘: 5, ‘b‘: 2} 63 >>> d.update({‘d‘: 8}) 64 >>> d 65 {‘e‘: 99, ‘c‘: 5, ‘b‘: 2, ‘d‘: 8}
标签:视图 一个 color col dex append 元素 姓名 添加元素
原文地址:https://www.cnblogs.com/kadycui/p/9225739.html