字典其实和之前的元祖和列表功能相似,都是用来储存一系列对象的。也就是一种可变容器,或者是我所比喻的革新派的菜单。
但也不是完全相同,我在之前曾经将字典称为特殊的'序列',是字典拥有序列的部分特性,但是又不符合序列的定义。
首先我们来看下字典是如何创建的:
a = {'a':1,'b':2,'c':2}
b = {}
c = dict(a=1)
print a
print b
print c
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAVEAAAA/CAIAAADWnWyLAAACvUlEQVR4nO3dXXaqMBQG0AyLCclwmA2jYTDeB/CnigRIoL2evZ9cS4qnLZ/J0QDpCkSSfrsA4FQyD7HIPMQi8xDLTOb7NqXU9tPD8cGCNdvUErmep38LFHjPfN+mphtuj+8PP1qzTS1/vJ6+Te+5rFfP0DVCT7HZzI8H1tA12aN1zTYf9e2mI/jIeoauSZPVNc2+1suoXreeM6cwfK2FzB9pHBH/yhE8dJfHzKasqip/vU/1yDwVvGW+zgTy/2q8n58trGrlj++r57ntgp1eMp//oOg+6yzOa6XM1yvoWtirXK/jL9W2KaU66XytR0tPsa3j/D2o5YmtNA2eyq2wt+LAXx9z8Rr7etuHcZ4KtvfzUytePrBWyXy1BqFSnqq9J87Vo5+ngq2Zvz1bYZL58kL7DuhHHUPX7a5n6Jr7a9/2s6+eW1TLxvm5enaXBD8sfz8/Zxznm8ulWehZ108W7tvt/R77tqtc0ctvZE+mHe2rp++6fvyAIff7b61HM08dy+vwTlOj/a3pr9VztQ6PWqy3h1hkHmKReYhF5iEWmYdYZB5ikXmIReYhluy1sawDga+SW3trvSd8l+w5Ns7rgK8i8xBL9poZrtMAX2XFtbG09PBFjPMQi34eYpF5iMX38xCLdXgQi/X2EIvMQywyD7HIPMQi8xCLzEMsMg+xyDzEkl2Tk12RU2txrvU/cIbltbdr7s26936y76zzheMtnGOz5t6sRfdv7duXhDufBw6XPa/uGNMd1mUezpa9ZsY++z4IcH0OONyKa2P9NHRNmhujt5t7X9DSw8G2jvP3oJbPw43z8Au29/NTK14+0s9m3iAPx9qa+duzFSbhMg+/IHdtrHfjON9cLk36vOH6ycLTdpp5ON7yOrwzWYcHZ7DeHmKReYhF5iEWmYdYZB5ikXmIReYhFpmHWGQeYvkH84dcj4+pPLcAAAAASUVORK5CYII=" alt="" />
我们可以使用{} 或者dict() 来创建一个字典对象。
但字典里面写的是什么?下面我来分析一下。
首先,字典是以键值对的形式来储存对象的,即 key:value ,键和值之间用冒号分隔,而每个键值对之间用逗号分隔。
这个是时候可以联想一下序列的形式,在序列中,对象之间也是用逗号分隔的,但不同的是,序列是使用索引作为key,且索引是提取定义好的,按照一定顺序的。但是字典中,key是自定义的,相当于我们为对象起一个变量名,只不过变量名在字典的key中是以字符串的形式存在的,就像 c 中的创建方式,变量名为 a ,最终得到的键为 'a'。
那是不是字典的键就只能是字符串类型呢?并不是,我们也可以像序列一样用数字作为键。
a = {666:'scolia',}
print a
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAcEAAAAoCAIAAAAJ5dZBAAAGn0lEQVR4nO2dwXXkKBCGKyxC2ESaMNQRLLe+7G4Gupp7R6HD6m0CnYT2IJAoKBDCkrrH839vnp8tIygK+ClKag/9BwAAoBX6CwAAQCs0AQAAaAUaCgAA7UBDAQCgnT0aOj6Veo7fbXHQ1BH19rv1SHy+hddgezrAD4e09TKq7MxBb7u6psw7qPXz4TPqUIecN1uunIfvY9HQea7fie6kh0zhl1EPE7jEan8L3fWOIW2aAeNTUcfbCmx2QxVb2MqnLdqa0WGFtXWjo+00mkfdjY2G+eHIOa3szJe5bS6zmjLXE/Z9k2Nn1Muoo2rb1YsPqfmzcBo6mocyr2mapmnQ+a0jKDa5wvPMGJ+qOEVG8wx+2zCfglVke7+lD8bbbH11iYW1fNvCE6kcHce6+S+9OK07YaAxPhWJa+YiZ/IRPPeuadobZL1/Rsk95b040ofn1fxhOA0Nw7fA7mTg48NypYZG9TTMp/CW+Hbm6Mbj/PctPI9X7ei4wmlgeFJ3eKBhe9K9FqLda5zZ1kqzbXuDrLfPqM3Zkivza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1/MYmEuUH27hcdy5bt4v8t7fxIH9n3zoJpn61XCLX7MO6EfsbqP/aznsD4X+wzcA1wWt36WheD3JJmZNSxpxEM+SPLL8yGrG5+XBwCAdqChAADQDjQUAADagYYCAEA70FAAAGgHGgoAAO1AQwEAoJ1YQ60morPfr7L69CZ2tX5JpwEAP5FIQ60mdf7ru1Yf0choFFEqfpsCLbU+GgURBQDsJ9XQVUq8SFHwxz62r2wxGnWgTKeKOZpbofpc6+8NjQEAvyp5DV1DUn+x5srVHNUuNBQA0ALX0PVIm8ZrNVdmrtSjAzX0giQGAOCnEWpo+GjFatKGndNrrrRjNTkWKcsmDkKx4xrqauF/2bLSQqREAQD7ycWhVi9aNRqlzL8VV74RxklpTbUqNQuNWVviQ/bwyvJjOWJFHAoAaCGXDx2NCv7DGNJfFVeOiEQXISuIY/irTQ0NQ9yyhiIIBQDsJvtMaQ3MfNxXc6UVa9zdoYgv35kD4tCNozo0FADQQuH90CWRuOvK1KRHSz3EQsyoap4PDe4J3g5IY04X4N5uirLndSRDAQBNvOVzSp/Gb9lpAMAR4PPyAADQDjQUAADagYYCAEA70FAAAGgHGgoAAO1AQwEAoJ3/AQhdMIIW9o8VAAAAAElFTkSuQmCC" alt="" />
当然,为了提高代码可读性,我在只有单个对象的时候还是用了逗号。
元祖和列表也可以作为键吗?
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAeUAAABTCAIAAAAutojpAAAMaklEQVR4nO2dzXHzOBKGkc2mgBA2ESGLkWsCWNx8ms2AZxymag9K4nNNDSMYJ4E9EAABNP4IgZIgv0+5XDKN36bwqtUk0UwDAACYAfbsAQAAAGjizfR6vV3k97MH8bPBKQDgLB6m12ph7MrE18FqX+JArS/Bb+vhkb0Z35J/hKamR1pq9bPKT6HubwYAEBHr9ZdgH8z8XAevOrX0yEFzLcjEDjVaixn7TlACfHACcAaxXt+kcuv2S479YnuuXn8JtoyQ628ph2jWU3t/qF4nxqzE6A97AAD1r5Xe161SQ5XrVL0e5Rs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6PcqAj+z9lb9JADAxyfh1tKe+3eHTJqz6U7ArE4so7RfqKpoAqEt2ZYqJX+TIF20nV6aG28xve/HLxpRvkntjVkt8JJF/i+T6aqmlFmofqmKJDGHRedh3WHWz6DoX3dQzlsX2CccM1QZgIFW91v71IhvZUMumGvvu+y5M6QpT/1otLNrEmR6h+/rTMi3QfC5qMYpWPpJ7EYy5oVZkH9oUtVh5IvbPnnPRTXvGsnItAMAAWvTaJkJUwvcfqUKFXpXXiImrNKS/alLMFlokONVFMq1BOtdXoVZL9i9qsfJEDF3nopvGU1bMIwwAGESTXm8pVEIJi7xg7zaAdQ2zbbk7CloWf9K/vkOvlchraP7Fnp+F5vpqqkXsQydCLVaeiLuW0HEuuqn718Q+yTEDAAZA9dpkt4pDEEqEoVj+ycPMVWFylvh+Pi+sSdNfBYlxSfw6UaZBxM3tw0IsjP12+X0JR/Ib+/d/wyNBnN3m3/onzvWllqZa6ibVzbNPIvsXsVgGezr8MgfPRaPFkgasZSxL5UJLjhkAcDetz8t8iWrUEjwInAsAfiZVvfbuSbCY3Il4aO3R4FwA8KP5sc+jAwDAZECvAQBgDqDXAAAwB9BrAACYA+g1AADMAfQaAADmAHoNAABzAL0GAIA5gF4DAMAcQK8BAGAOoNcAADAH0GsAAJiD/wOREjUiuQ3KlQAAAABJRU5ErkJggg==" alt="" />
答案是:NO!
虽说几乎所有类型的python对象都能作为键(只要该对象是可哈希的),但是一般还是以数字或者字符串最为常用。
看到这里,我们可能会想字典无非就是高级一点的列表而已,为什么又不是序列呢?
因为字典是无序的。
我们看变量 a 中的字典:
a = {'a':1,'b':2,'c':2}
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMMAAAAeCAIAAAAevT0MAAABlklEQVR4nO2YW7KDIBBEZ1lsSJbDblgNi+F+gGgZw7NNILfPl1Uh2MJxmFI8IQjk2wHIj0CTCAaaRDDQJIKBJhEMNIlgoEkEA00iGGgSwUCTCAaaRDBUmWS1iGgbL8NFfnhxDIr/nOe0LRNQY5LVoozbr9NlZnx5DIrJ81gtr7uNy+OMmkalSpNCXGdUcQ1qxmTu1LQuT+ZxRkmkOtPtvS4VCJvnk+W2QJNJTxLe3lnWxZntqMJjqSCr9y7PWiZhSuhaDc3518FUlX/vy3NuPL5M0aRyW5fq7rAFIJNwgfzgae19eCitRQSz59c807RK4zUpbf+4B6CDIMYFzDaskT9OI8RcL3OsVJN8eUtiizNeBCAmwY5I0C7B3rS7PGv1SZVHOKDMXm7Ut0xHDmdMdx5nVLr3Pk9fnl2AsZp0l6c70iO0fk9687uIqG1TmV6gvrClcb3fXfapSqHzr8eJOFFfHmuMDY1b6flb80zUJPn2b9wfA9FWIJktj1/xGzchZWgSwUCTCAaaRDDQJIKBJhEMNIlg+ANd47w5a2J1YgAAAABJRU5ErkJggg==" alt="" />
我们在创建的时候明明是按一定顺序排列的,为什么输出的时候顺序却乱了?
这正是字典无序性的体现。
首先序列之所以被称为序列:正如其名,有序的、队列式的。我们在序列中逐一放入元素的时候,会自动的按照从左到右进行编号,也就是索引,而每一个索引对应一个对象。而字典却失去了索引的约束,用了自定义的键来取代,当我们在获取对象时也是用键名来获取,只要知道了键名,那么对象在字典中的哪个位置也无所谓了,所以字典是无序的,也就不能称为序列。
但我们依然可以将其想象为是一种高级的列表,只不过这个列表的索引是自定义的,无序的。
另外,当字典中的出现了同名的键会怎么办?
a = {'scolia': 1,'scolia':2,'scolia':3}
print a
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAawAAAAnCAIAAADcqutUAAAGaklEQVR4nO2cy5XjKhCGCYsQbiImDDmCYefVvRlo2+wdhTbKwEnoLgQSBcVDMpI9rf87c/q03TyKoiiKEhoxAQDAhRGfFgAAAD4JnCAA4NJscYLjU8rn+G6PgxKdEL15tx2O75fwHEwvGuihSV8vLfPKHFRZ1TVlPkGtnptbVFOFHGctZ9rhGyxOcDbWuxB3oYZE4ZeWD+2NyShXRdzVhjnZNYXjU4qO9uXJbHUdSriXb1t1NbNDCitjZ0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I41zmgltTBnrPbPlmMrVXSC+IlM8dyznTVclPsSZnoZsrqQa1k1ADeTczuQI/DL//bNuHXcu7cVKaNUkpcsUHCjhss4pJDuznHcGLieYv5xUmh3uYWWYBWOv2vi9e4kYTz9RLT4MJNlSM7nIS/VKdEL+cVkz+9NLhIU5ZaLVsExiFP4M8qNYcpT9TXbenaqO04afE6TtcHo2qjfTyxivr3BOB60HTeyQWmZKZj5fTKc+th/XGjOJ/AxyZcKWE9Z7Wsv8xlazugvse2MkDlwJ3BH1ZBYJU6HixyVsy5l3sv6a+18H0HDsxbNemtKNtBK/5m5gm9Xd9rW5YX068x3Yx4gkcvwuCcE1iSyzhiWV1uSNgL+eVqsb7w4DAC4NnCAA4NLACQIALg2cIADg0sAJAgAuDZwgAODSwAkCAC5N6ASNEkLM92yMEh+8Tteqd29AAAAQEThBo4R09zCNkg2uZI5aChH7oaKPa9O7kwBeEADAEztB9+a1bOWEJs7ljfqWaX5v70YJh9ffZ0NaAMBXk3KCbTnHDXnBI4n+4AQBAEmoEzzq5NgwwVfXDinon/EBAIDgO8HyM4T1uEliLnr+tN/4foc6L9sK/f/oomb2wo0CaUEAQIJNkSCX2rMVomQiSevFFYNvlo+NYkZEggCAOrbmBOcoznuCnPRu/p+KTtAPMpsfnJETBAAk2eQEjbbOz4/7lt90g0jwrXOr92CEBH9wggCAJJl7gjFL4k6QMIsmCYOcoFfHfcVeZLEh5u0mRVqEkjtjw0kkBAEAaTJvjPwOft2AAABNwbvDAIBLAycIALg0cIIAgEsDJwgAuDRwggCAS2Od4Hyv5LOiAADA+cAJAgAuzf8zkZRK0LwzSwAAAABJRU5ErkJggg==" alt="" />
就像变量命名一样,前面的都被冲突掉了。
这时又有同学问:不是说字典是无序的吗?我怎么知道谁在前谁在后?
我曾经把索引比作是特殊的变量名,只不过普通的变量名不能只是数字,而索引则是通过数字去内存取值。同理,字典里的键也可以看作是变量名,在字典里的元素打包成一个字典之前,先进行了变量的赋值操作,而对同一个变量进行多次赋值相当于切换其在内存的引用,只有最后一个赋值有效,这里也是一样的。在键值对创建的时候,按照我们写时候的顺序先进行赋值操作,然后保存在字典中,保存之后才是无序的。
那么值相同的时候是否也是同一个对象呢?
a = {'a':300,'b':300}
print id(a['a']),id(a['b'])
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAa0AAAAyCAIAAABgcxNCAAAGzklEQVR4nO2dwbHjKBCGCYsQNhEThhzBcvNldjPQ1dwdhQ6j2gSchPYgIWhoEJYlPWn4v5p6NdZroGngV4PkGfEfAADUjfhpBwAA4IeBDgIAakf8AgCAuhEDAADUDXQQAFA70EEAQO2s0sH+JeWr/7bpTolGiNZ8Ww/H+T08BtOKDeKwSVtvLfPB7NRyqEtsfoLSOG8+ozYNyH6z5ch5uIpYB8f5ehfiLlSXKPXW8qG9bhlli4i7+mBYVo1i/5KioW15Pk/hDj1cy9kWXsnoEGNlptFRZuj1o6zgSsfscKSClg/mW98Wl0qJzfH4fV9k2xn11nKr2j7qxUlq3oxQB3v9kPo9DMMwdCot4Z7ZMBmPo9u/ZHaYe/3yfrtiTngrwbT21tpp67Ox1UUelvK1hztSODoT7iY892K37vg3/P4lBTvvDwomHcF9Sw3Dp8nOz88ovqe0F1vGcL+atyPUQT+N8tqOBi/ceBbqYFDPijnhFwmLk2Ct3Bp/7+F+vEtHZzKOE7SdukNv+KYVqlVM1nlMMNe1stq3T5OdH59Ri7MlZXO2mrck1MHy9owKuufvVaePyoxZ212I9jn9ZfzjlqXWDyHuLnebtr3jgp/OU0Kb0TDKSTU18D00qhGiVaqx9RzhIYEp9dA9PTAiNjGfzAZyE2Z00B1l0Dxuat3GxEwX06XCtsaw0/3aWJt8SNH+ox9CPKRshGxvspkiNjfnHI6iGtpw/tARfCZ6MZ0PzOcqzLgXRYP2vVOiUaoV4q5Umx7TTpF5GM3MVFvx3IgDEtvMh0jjbYntaTCCi9FgZ+/xNYfTYGF15xfOeh0cTOudNMVL1G3cjI4X5PyxsX6P123K47K52GYY2G2veYXyEX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r3WiFGMTqYyf4pF073oeAgCuQ6yDo3C4FEkJpcP947ztDITA5VNcqdBG326S7DkdOZW6gIcAgCsR6qBRTlzsX6wGzEvfGoVaMAsIWyq0oeaeTV5izu8hAOBaxP8/CeH27LUkO8Fnem9oxWcYhrAUZ+PjbKjgsJYn9xAAcDGWnxe7Zd9rKfVv/wFBr2VScdhsiyqUDLMtX5zsIV2K83sIALgGnA66V0OCV0Wi901IYhQ8N4hKxTYut7O7WT/TyyRd5/cQAHAd8B41AKB2oIMAgNqBDgIAagc6CACoHeggAKB2oIMAgNqBDgIAagc6CACoHeggAKB2oIMAgNqBDgIAagc6CACoHeggAKB2oIMAgNqBDgIAaud/4JCwz+Z4yZAAAAAASUVORK5CYII=" alt="" />
看来是的,和序列中的一样,也是节省内存的优化。
接下来我们就可以开始学习字典的内置方法了,首先按老规矩先使用 help(dict) 查看其帮助文档。
Help on class dict in module __builtin__: class dict(object)
| dict() -> new empty dictionary
| dict(mapping) -> new dictionary initialized from a mapping object's
| (key, value) pairs
| dict(iterable) -> new dictionary initialized as if via:
| d = {}
| for k, v in iterable:
| d[k] = v
| dict(**kwargs) -> new dictionary initialized with the name=value pairs
| in the keyword argument list. For example: dict(one=1, two=2)
|
| Methods defined here:
|
| __cmp__(...)
| x.__cmp__(y) <==> cmp(x,y)
|
| __contains__(...)
| D.__contains__(k) -> True if D has a key k, else False
|
| __delitem__(...)
| x.__delitem__(y) <==> del x[y]
|
| __eq__(...)
| x.__eq__(y) <==> x==y
|
| __ge__(...)
| x.__ge__(y) <==> x>=y
|
| __getattribute__(...)
| x.__getattribute__('name') <==> x.name
|
| __getitem__(...)
| x.__getitem__(y) <==> x[y]
|
| __gt__(...)
| x.__gt__(y) <==> x>y
|
| __init__(...)
| x.__init__(...) initializes x; see help(type(x)) for signature
|
| __iter__(...)
| x.__iter__() <==> iter(x)
|
| __le__(...)
| x.__le__(y) <==> x<=y
|
| __len__(...)
| x.__len__() <==> len(x)
|
| __lt__(...)
| x.__lt__(y) <==> x<y
|
| __ne__(...)
| x.__ne__(y) <==> x!=y
|
| __repr__(...)
| x.__repr__() <==> repr(x)
|
| __setitem__(...)
| x.__setitem__(i, y) <==> x[i]=y
|
| __sizeof__(...)
| D.__sizeof__() -> size of D in memory, in bytes
|
| clear(...)
| D.clear() -> None. Remove all items from D.
|
| copy(...)
| D.copy() -> a shallow copy of D
|
| fromkeys(...)
| dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v.
| v defaults to None.
|
| get(...)
| D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.
|
| has_key(...)
| D.has_key(k) -> True if D has a key k, else False
|
| items(...)
| D.items() -> list of D's (key, value) pairs, as 2-tuples
|
| iteritems(...)
| D.iteritems() -> an iterator over the (key, value) items of D
|
| iterkeys(...)
| D.iterkeys() -> an iterator over the keys of D
|
| itervalues(...)
| D.itervalues() -> an iterator over the values of D
|
| keys(...)
| D.keys() -> list of D's keys
|
| pop(...)
| D.pop(k[,d]) -> v, remove specified key and return the corresponding value.
| If key is not found, d is returned if given, otherwise KeyError is raised
|
| popitem(...)
| D.popitem() -> (k, v), remove and return some (key, value) pair as a
| 2-tuple; but raise KeyError if D is empty.
|
| setdefault(...)
| D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D
|
| update(...)
| D.update([E, ]**F) -> None. Update D from dict/iterable E and F.
| If E present and has a .keys() method, does: for k in E: D[k] = E[k]
| If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v
| In either case, this is followed by: for k in F: D[k] = F[k]
|
| values(...)
| D.values() -> list of D's values
|
| viewitems(...)
| D.viewitems() -> a set-like object providing a view on D's items
|
| viewkeys(...)
| D.viewkeys() -> a set-like object providing a view on D's keys
|
| viewvalues(...)
| D.viewvalues() -> an object providing a view on D's values
|
| ----------------------------------------------------------------------
| Data and other attributes defined here:
|
| __hash__ = None
|
| __new__ = <built-in method __new__ of type object>
| T.__new__(S, ...) -> a new object with type S, a subtype of T
dict
按照其功能可分为:
1.大于、小于、等于等比较运算符
2.成员判断
3.内置函数相关的(参考这里)
4.增删改查相关操作
1.比较运算符
因为字典不是序列了,所以就不能像序列一样按照索引逐个打擂台了。
不过大小的比较规则还是一样的,只是上场的顺序未知而已。
那到底比较的时候顺序是怎么样的,看下面的例子。
a = {'a':1,'b':2,'c':3}
for x in a:
print x
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAasAAABMCAIAAABcRQ+0AAAGlUlEQVR4nO3dy5WjOBSAYYWlECYRKwwcwWjnVU8GbEt7R8GGDJwEs+ClJ2AbKLfv/50+fbqqhST0uEiyy6U6AJBK/XYFAODXEAEByPV8BGzvWt/bd8ttjKqUqt27+eR8fg3P4Wq1QzvsUtbD6uXGbMx6U29J8xu2tvPuI2rXBjlutJw5Dp8XRcB+pF6VuirTFC55WH2z3g05M16iruaJDnmp/9q7VlVYllfnoaHjGr7q06bclt4JEhs39I5xXWtv2y58sWJjd5QabbkxH/ayOkm2pDmff++r9h1RD6v3yu2pu/iQnPcRRMDW3rR9dF3XdY0ph20vWTck7vu1vevFDm7t3fvfF0aDNwdcPT5OGzvW2Y3ZJTXc6u0aHmhj7wzmB+90F4fdjv+Qb+9aZUf8SY0Z9uCxV3Xdswuc3x9R+TsN72LPNjwu550EEdBfOnmlJt0WbzM3RsAonxdGg39JfHnQTC9uhN+v4XEeW3tnSJwuyg66nfAh72plapNZaZ7TmK+V8nLdnl3g/PqIWh0tpTSflvNuggi4vSRnohvzd6bDl8b1K7WrUvXP8I/+zzwhrb0pdZ3Xa8Mmt5/qw7lJnKZPmKxDbZjAr6EzlVK1MdWYzxk1DGSuutk2PBgK0qSeGQfBgzcTAeeDi3DtNpQ+tokbvlm+Ki6rb/Zwd9bnpm9a1X/sTamb1pXS9UVXQ4tNxc0VTlo1TpOrT9iDP4W7GE4DplOUTL9vao3w3hujKmNqpa7G1OU+bUwwDpORWSorHRtpg6RppiOj/oGUvdOoB1dbIzt6z885HgYrs3th4rwYATtXeydK6eSct2nOplNx+rIaa9x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LlopzScwAvhmvBYMQC4iIAC5iIAA5CICApCLCAhALiIgALmIgADkIgICkIsICEAuIiAAuYiAAOQiAgKQiwgIQC4iIAC5kgjIx+QDECOKgNMno/Ip+QC+X+n3hADA9yutAbvWWkIhgO9W/E1JfEI+gK/Ha8EA5CICApCLCAhALiIgALmIgADkIgICkIsICEAuIiAAuYiAAOQiAgKQK4iAhk/FAiAJnw0DQC4iIAC5iIAA5MpEQGP4dCwAIqQRUBnXda3VhEAA3660C2Y7DOD7pRFQ25Y1IAAR4ghoret/XSYrQABfj58JASAXERCAXERAAHIRAQHIRQQEIBcREIBcREAAcv0P2PmCtWjhbQ4AAAAASUVORK5CYII=" alt="" />
我们使用for循环来看看比较时的顺序,可以看出,顺序是无可预估的,虽然这里只有三个键值对,貌似有一定的规律,但是一旦里面的对象多起来,就得不出规律了,而且不可能每次比较的时候都要自己看一下里面是怎么比的,那样实在太累,所以我们得出一个结论:少用字典间的比较,要比较用序列去。
另外这里补充一点:当我们使用for循环时,我们得到的是键名,如果我们要取到值怎么办?可以像下面这样写:
a = {'a':1,'b':2,'c':3}
for x in a:
print a[x]
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAbsAAABLCAIAAABm7r7kAAAGkklEQVR4nO3cy5XjKBSAYcJSCJNIEYYcwbDzaiYDbZu9o9BGGTgJzUIvHhd07ZLk6p7/O336VFcjQICuAMk2IwBAx3y6AgDw2yBiAoDWWxFzeDTNY/hu0b01rTGd/24+kp9fw2v4zhzQDoeU9XRNvTF7u9/UmjSfoG3nw0fUoQ1y3mi5chyeLI+Y08i+GXMzti8c9XTN3QUN4O1yiLnZFzrwrf4eHo1p47KCOs8dk9bwXT/tEtX0TpTY+rl3rB8Hd9cd+GbFlu4oNVq9MZ/ua/ei0qS5Xnjuu44dUU/XHJXbS2fxQ3L+gDRiDu7euOc4juPY2/JtIUg2zomncTA8muqAGNwj+N83Rk9wzfhuuV33bqmzX7LLaqj17RqeSNk7s+3Gvp7FaacTTiKGR2P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vxLaWw9GDzKGif7Ch5ghnt8PpxmdPZzprWNH8vO33z38HmXboPHrVqmqZwFmEPymex7qt2X00bvI7WSq0R7mPG+Ujt7G3nx6f3QVlpn/bO9S4ah/HILNVZ3uOOuz4fP0tuQifKPSilSXMujN7Lcpbvgpqr+wDf/MxPPn+OSCvli601LE1CP17DY1357tsf9Z7diw48990lZ9ney3x7/ph3MK+7uk/6lGS/Pav6GeaHqtGc9GfVEP9P2cjUWLf/DvmYxm/vyqubz5UDgBYREwC0iJgAoEXEBAAtIiYAaBExAUCLiAkAWkRMANAiYgKAlhwxveWzMACQyiKmt8YYY4iYAJAS55jeEjEBIEPEBAAtIiYAaBExAUCLiAkAWqVn5TwvB4AUb7ADgBYREwC0iJgAoEXEBAAtIiYAaBExAUCLiAkAWkRMANAiYgKAFhETALSImACgxefKAUArjZi2ccP00+AaYiYABMqrcr7yDQBipe/HZE0OACnmmACgVXlWTsgEgEj5yY+3Zv0ZAFB9u4gJJgBEeIMdALSImACgRcQEAC0iJgBoETEBQIuICQBaREwA0CJiAoAWERMAtIiYAKBFxAQArSxiDq7hg+UAIEkjpvsKvruImAkAgezb3rYoyfdjAkCkuI85uIavxwSAkBwxCZcAkBMiJl++DgCiNGIOrlk3Lwfn2McEgFX+5CfAVBMAArzBDgBaREwA0CJiAoAWERMAtIiYAKBFxAQArf8AjUmDuEjuzYAAAAAASUVORK5CYII=" alt="" />
当然还有其他写法,看完内置方法后自己总结吧。
2.成员判断
1. in
在序列中判断里面有没有某个元素时使用的是 in ,但细心的同学会发现,在帮助文档中并没有出现 in ,是不是in不能用了呢?
看下面这个例子:
a = {'a':1,'b':2, 3:30}
b = 2
print b in a
print 2 in a
print 'b' in a
print 3 in a
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAboAAABdCAIAAABcozRcAAAHf0lEQVR4nO3dy5WjOBSAYYVFCJOIFQaOYLTzaiYDtq3dLBwFGzJwEsyCl95cY6DK9v+dPnWq3LIQkrhI8kOqBwAIqJ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muXzndZjpNzbcdvY9N2fMz7pe/ZagLAx5KFy/HPIAzG21Gw1QSAj/VUuMx8v7r3C68QAfhMe4RLqxVbTQD4dLlw2Xn7TzijRneSPa1k6mX1kq0mAHwo3qYOACKESwAQIVwCgAjhEgBECJcAIEK4BAARwiUAiBAuAUCEcAkAIoRLABAhXAKACJtPAIAIm08AgAibTwCACJtPAIAIm08AgAibTwCACJtPAIAIm08AgAhvUwcAEcIlAIgQLgFAhHAJACKESwAQIVwCgMj/r2lNxlpMk8oAAAAASUVORK5CYII=" alt="" />
还可以使用,但是这里 in 只能判断字典里是否有哪个键,而不是键对应的值。
2. D.has_key(k) -> True if D has a key k, else False
和 in 一样,判断里面是否有某个键,返回布尔值。
a = {'a':1,'b':2, 3:30}
print a.has_key('b')
print a.has_key(3)
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAcYAAAA9CAIAAAC4Il4QAAAGe0lEQVR4nO3dzXWzOBSAYZVFCdNIVAauYLTzaqYDttHeVbChAzehWYBAvyA718Tz+X3OnJwknxBCEpcr2RkrBwAQon67AQDw5yCkAoCYx0PqdOu62/TT845a9UoN9qf1lLx/C89hByXQDyLnuptuvzNHfdzVLWV+Q2s/i88o0Q553Ww5cx6+gSSkzlP/otRF6bFyyN10VxP0kNX+EHXRD4zwUxNiunWqj88VtHkZubSFz3q3e7hldKLC2i6jo62bzLXtwCcb5oej1mn7nXk3X4d3XUuZ84XXfkh2Rt1NJ1XbQ1fxJjW/qSikTubambtzzrlR1x8sQTG3FJ4nynTrdmfMZG7Bvz4xvYKbyg7+gT8a32brq8ta2OrHLXyhxtFZbKnBehUvu5wwDZlunSreQid1ZjyCrz3KuUdTsN+fUeUrja9Csg9fV/O7ikJqmNwFl5HNg3Rl3RhSk3qemF7hIenhUb8/ufb/eQtf5946OkvhPG180eXEaYgdlB50IRc+pzOfO8vTbXs0Bfv1GXU4W2pl3q3m9xWF1PamW530VLgYX37Uds4lL0oN38s383/bHW7MVanLllEu6/o5dix7T2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bDVW3d0XsWj/wx70V9r7v7FX+QOm6vuL2H5aXhKKt9rxbiM2Uzs8W6BSny9yz/e+92d/M3/gAghpAKAGIIqQAghpAKAGIIqQAghpAKAGIIqQAghpAKAGIIqQAgJgqpVqsNf1oEAA+K/+d+xjpntdLWOWv4azcAeEycpVq3hlRnLWkqADwk30v1IdWtGwHaTqbzewFWp98459xSoCOzBfDJdkPq8mMSKtcC6zeT6eYSVhNUAXywlpCavFCVh9TwZS1e1QLwuX4QUq1WaZbq3DSRpQL4WElIXfZElV/Hr8lnuKD3O6t62031x5GkAvhkvNUfAMQQUgFADCEVAMQQUgFADCEVAMQQUgFADCEVAMQQUgFADCEVAMQQUgFADCEVAMTwQSkAIIYPSgEAMXxQCgCI4YNSAEAMH5QCAGL4oBQAEMMHpQCAGD4oBQDE8FZ/ABBDSAUAMYRUABBDSAUAMYRUABBDSAUAMYRUABBDSAUAMYRUABBDSAUAMYRUABDzH8LhQhOJS9YxAAAAAElFTkSuQmCC" alt="" />
3. D.__contains__(k) -> True if D has a key k, else False
判断是否有某个键,和上面的一样,就不多说明了。
4.判断字典里面是否有某个对象。
字典中并没有内置的方法能直接判断,但是我们可以自己写一个。
a = {'a':1,'b':2, 3:30}
b = 30
c = a.values() #返回一个由字典的值组成的列表
print c #同样我们无法预估字典里的顺序
print b in c
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAbYAAAA8CAIAAACFNwgtAAAG80lEQVR4nO2dyXXrOBBFERZD6ESMMKgIPnZadWfArbFXFNwwAyXBXnDCUEWCtEjL/vceHx9LxlCYHgpFijK9wNNVtTE3Y27GtlKCIc3ddctrb6cs5ma9kkmgtabZkXyge1SmjusKbK4enWThUQ5ZeCIloxMltn4cHev7zt3LMh40bBoOrdPWO/PpPh5bI1aS5nrCtm/y2hn1dNWrStvVijcp+XRM/lbn7pV79n3f962t1OkYJOvHxMPAd49qdQZ07hH898B0CRaJb8yYvXWTzX4qLrOwlC9beCKFozPim2nDmFtxWnOWusY9TF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7EJGUVrfT16bbaypTfVnitaNv4MAXBrLjno1TaO0IhxBuRVzbLT5qOrgLrFa6o0wFhmXI/Wzt43vn94HdaVj2jrXumgexjNTs1mOU8dDn8+fqTRhEOURlNKkJSuz97KS5W2vZHXvRpTIXeQucYR0+L2Y2ULNzfx2C1/Llfeg/eD73b7MC9u+eYrU2brHbotfcy/kWav76xKp0S5XlN6D8dJn5HW+l4Xwd5LNzBLmEN5LPh/x4zlvdZ8nkQAAPx4kEgBABYkEAFBBIgEAVJBIAAAVJBIAQAWJBABQUSXSW2OMMaZ6x7uuOlcN1nFPIwCciS6R7h21MaZzDokEgBPZIZHe7vTZJkc08PYm92+Xbzr7jKnXiEQCwLmUSeQodrsk0ttZBztXGev7vnPV9ARTWyySnfuYknob24BEAsC5lHuR3h6O/I1ZO1dVi9qVamRYb2IDEgkA53K2RHq7eJ8rYldEKLHjG0gkAJzJlV7klyQy08ceiQSAs7lEIntvjf08eNAe80vJkUgAOJfDErmpmIEITgonXa7ZVt4lVyqKSCQAnMuuK9rhVe0CN3DJFUhcetPPZjlB3endQkgkAJzLsU/XSIHBIxwth0/XAMAl8BltAAAVJBIAQAWJBABQQSIBAFSQSAAAFSQSAEAFiQQAUEEiAQBUkEgAABUkEgBARZDI6EPRfMIPAP5iBIl0bnm240/4ki8AgLOQvEjfL88o8x43EgD+WrRYZPAYx/m7vcbn64wPEE/+6Pv5ATxv+dXbAAD7KZDI8WUifXOC+Y/5yWb7nigOAPC2lEtkcuEml8jwMg9XeQDgN/BliVy+2zp4Pm7X4UUCwC9AlMj46xMC7zA8QE+RSbtEI3kYOAD8Lrh1HABABYkEAFBBIgEAVJBIAAAVJBIAQAWJBABQQSIBAFSQSAAAFSQSAEAFiQQAUEEiAQBUkEgAABUkEgBABYkEAFD5H9kKjnAvLQvnAAAAAElFTkSuQmCC" alt="" />
我们转了一个弯,这就要考验思维的灵活性了。
3.增
1. x.__setitem__(i, y) <==> x[i]=y
这个和列表中的不同,列表的这个写法是修改元素,如果给的索引不存在是会报错的。但在字典中,这个方法除了可以用来修改元素之外,如果给定的键不存在,则会在字典中创建这个键值对。
a = {'a':1,'b':2, 3:30}
a['scolia'] = 'good'
print a
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAbMAAAApCAIAAAAwBVBBAAAHj0lEQVR4nO2cwbXrKAyGKYsSppFQhlPBY5fVTAfehn2q8MYdpAnPwmCDEBhj7OTe939nzj3v5oIQEgghOyMmAAAAIeLTCgAAwNeByAgAAJQ9kXF8Sfkaj444KNEJ0Zujcji+X8NrML1oYIcmY721zBtzUNumLmnzCUrt3HxFNTXIeavlynXYmiUyziv4LsRdqCHR+K3lQ3sTNcp1EXe1w1FVfh1fUnThWJ7O1gFUw1q+bSuWeCdorIz1jjLTqB9lHSsVc+5IGS1vzLe+bW6ekjbX4899k7Yr6q1lK2m7ZvElkq/ARsZRP6R+T9M0TYNKh3mv2WQbz/4eXzLr+FG/vL9WrBJvb5jeHb+DdjobJy7SsJTDGp5IoXcs60G9zOK06fhJwfiSgt0JFxkz9OC5vaZpb0L0+RXFzzScRUsbnif5Emxk9FMtT5vInfS6WhgZiZyKVeJ3od0D81VeqI9reB7vUu/YxnESd9J0wqTA9EL1islMrzFm3SjVuu1NiD6+ojZXS6rNt0m+CBsZyzUwikzYv+HaX5WZM7u7EP3T/mP+b92oWj+EuK/5nb0szyHA1mVom7lhlLfqsIGvoVGdEL1SnZNzhYYBTK+HHsPCU9AmZs/6CA5qJjKuBZAw17OjO5sY+2G6Fx1rNnt4y5ulyYcU/b/6IcRDyk7I/iY7a7FluFXhyKq0DadP6MFnYha2qrBUYxi/F1kjnPugRKdUL8RdqT7t00EF6zBamamx4rURGyRus5Se5oOKnSnx4KY12NV7vWS6DDZ2d3Vg3R0ZJ9N7Fat4067XPaPjLbr82rmZzJ+7tGjN+OI208Rels2LBhSqYadMKPlMDUOWdG8ZdI4dfuEsbkMoj4ypgzqTPMajD0p0gr0Z5MaycoxyUc+3mOif06Ds0p9/pm4Ar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T9+7kRJK/5z50X+DxhZGxzATm5JBd0LZZjG/r5xcHQWDh0+e791E5uNe71+m/a9m+p9YDm+JFxu2i93PwO74DqEn7F2H6vhpHIKKGU2nkvT3P8trlWDoJMOXSY/cQfKrQDl9G383tGaSc+1jD+ZO4l5cZBjlIjqGVXzrhsoePZQauccW+vtpExSkNraSGDKxdad0bl42A89sGu/0lDv6d0zmiY/QQ5IziHnXXG9YT/XGQ8EBqfp9ymj8aLdjvYhClsJuTlT4jok2Z+pyzZqFM61rDkHyyoM4J6dkVG99cGl5RUVpJu7+/30kDC9OKewFSH2jj/2s2qzzSNWh8ItUbT6ax+cpKP5YztL6dsnlucM248s0JkBPVk3mfksEnJ7SYz1bXyxHNpV5C+8WlLRZIbv7VT+Xae1mYWtaVBfvd6WDXq9OFe/3HiV3lhzc7rsw4eeaeR31OdyHDldca5zJsaE0VGcIDMd2Au44w33X7K6DHfps+pnPdoHt+BAYfA96bBJ2GSVgC+AERGAACgIDICAAAFkREAACiIjAAAQEFkBAAACiIjAABQEBkBAICCyAgAAJT/AZPj9ea/XaX4AAAAAElFTkSuQmCC" alt="" />
2. dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v. v defaults to None.
这个方法用于创建新的字典对象,我这里将其归类到这里。
其中,S 为一个键的序列,v为值,将为S所指定的所有键设置同一个值,默认为None。
a = dict.fromkeys(('scolia'),123)
b = dict.fromkeys(('scolia',))
print a
print b
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAcoAAAA6CAIAAAC/G44mAAAIh0lEQVR4nO2dzZW0KhPHCYsQbiJNGHYEl12v3puB22HfUbgxg07CdyEqHwWifLTO/H/nOc+Z6cGiKKAsSlrYBAAAoALs2woAAMDvBO4VAACqcMS9jm/O32NujYNgHWO9ypVDcX0N26B6VsAORer6SB435iD2TZ1S5huk2rn4iCpqkHqjpeU4vCSre52nwZOxJxNDoPBH8pc0rKXEcgl7igO9fWpwjG/OOrsuQ2fdi66GZ7nafE7pHauwULp3hJpG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AgCrAvQIAQBXgXgEAoAqZh8HU4+u1Y/s0ACCLzMNg9qGOCZkSvOe3j+XAl/8AAHnkHAaTju9MzcNOfE7WvsSci0vPagFeXQEAyKLNYTDNXJUS2ysqLvJ6PQDAH6XGYTA+dd9j7xQQShVyr3d86QYA4CocOwyGOIjCP2qFeMmb7ewUcdhJoRNblGBCKSlHo0ZLn7luISW3VCRfTIf0KwAgg3OHwfgXeElbK33qX+h8Uuq98fPlSkoZ1kcJ7Uf9RLP5PA3RKwAgi6O5V+cgiojfjB8g4X1S5piZWewoH5y7asR/YF7tyL0CALI4dhiMdxCFf9RKXvSaux7XckbJWSx6ddyroes44tALAEARDh0GQx1E4R+1YucxiWNCyMNOChwzY+zHMt00kXtlTCgrBUxnkOFeAQDnucthMI2BGQAAueCdAwAAUAW4VwAAqALcKwAAVAHuFQAAqgD3CgAAVYB7BQCAKsC9AgBAFf4PsJa4EWwA4fIAAAAASUVORK5CYII=" alt="" />
注意S的写法,这就是为什么在元祖只有一个元素的时候一定要加一个逗号,否则解释器会将其当作传参,将字符串传进去,而字符串也是一个序列,所以没一个字符都被当做一个键,就造成了a的输出了。将S写成一个列表就不会有这样的错误,但元祖消耗的资源少,具体选择看个人。
a = dict.fromkeys(['scolia'],123)
print a
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAbkAAAAlCAIAAABQ5QDzAAAGiklEQVR4nO2czbXrKAzHKYsSppFQhlPBsMvqTQfehn2q8MYdpAlmYTAIxEcc28m79/87c+65yQMhhBBC9h1hX2V+SPmYX+6WMCkxCDGad+VwfL+G52BGsYMddhnrqWXdmJNqm7qnzSfotfPuHrWrQY7zljP98EgE/bj49FWIq1BToctTy5uOpm6U7yKu6oWl27TS80OKgY4V6eyWJNVwK9+2OXtWhzRWxq2OMnbWt76OGxXzy1EyWt2YT31pbqeeNucTz73Jvh711HIvaS/N4ksknw2JlbO+Sf201lo7qfJREDWzrvHiAfNDVl1h1o/oXzf4TbRbzOiP6El7nY0Xl2nYy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zQfARgoivgOjr8uOuee26uR85iDd0CQWhDd6V+Y/97ulzBuI+ADYTNke3xj57B9vVx5z79mcULTkMpNdeB5zMmuZN7DHB8BmCoTPaDsaD45x8o65Lys0tHkJa3utD4j4Dlhvlh36D+EDYDOr7/GFhO9ZsKwNyMJj7svekuNsnjJf8qN7fM49juqGVbeh5OMhE0HwSd7ABh8AJax+uXErjn1H73/AXRsKgHuDb3UBANUB4QMAVAeEDwBQHRA+AEB1/AfZ/MDg1G+YAAAAAABJRU5ErkJggg==" alt="" />
这里很特殊,创建的对象并没有添加到原字典中,而是所为一个返回值。
3. D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D
这是一个集查询、修改、增加键值对的方法。
a = dict.fromkeys(['scolia'],123)
print a.setdefault('s',123)
print a
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAagAAAA5CAIAAADsnSjFAAAHXUlEQVR4nO2cTZasKBCFWRZL6I0kyzBX0Mxy0t07cFrMcxVOPL2B3IQ9EJWAABF/ymzud96pU+VDCAK4BKiIfwEAoDLEXwAAUBliAACAyoDwAQCqA8IHAKiO7cLXv6V893vL7ZRohGjN3nw47m/hNZhWHOCHQ8r6aJl2ZqfWXZ2T5jfI9fPhPepQh5zXW67sh9l4wjd20KcQT6G6yC0fLV/aqYdR0y3iqTa0Q1Gz9W8pGlqWY7P1r29hKXcbaTmtQxIrY1tHmaHXr7wbCw2bmiPmtLQzP/qxOjZy0lyPW/dVju1RHy2Pym1TLW6S8y6I8PX6JfVnGIZh6FRcpJ1kg008Nmf/lsl27fXb+d+CTuB0fdNOk2enJ5vNlF1gYS67LTyRzNaxLNPsXIvTquNO6f1bCrajX+RM2oLn3jUMW8OZ3+9RfE1pLY704Xk574MInxsoOYUFreWvJTOFz8unoBO4t/i3E+8Urnb3W3gen9zWsYnDEOyk6tAp3bRCtYqJK69xZlkpxbZtDWd+vUet9pZYmrvlvBcifPkFGOXVx11+2j+VGeOypxDtj/1l/LeMQ61fQjyX6My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hsVz5ztRN38+6hAPrvrqOi7P29tga/5t390pG9xmfrHXLU5V7YB//kQjxXhaCOgl6Zg7z1tghb+l/PWWjG9/qAgCqA8IHAKgOCB8AoDogfACA6oDwAQCqA8IHAKgOCB8AoDogfACA6oDwAQCqgxE+o5x3nnsthWU6v0wJ7woAAHwVVPisqDkHoDymr2KMEkL9DEbJ6UqvJaQPAPCFhBGfUYucxX7nLwAAwFeQFr6FXkvpnD+psNAFAHwtWcJHVS+REAAAvoB14TNKMKrHpQQAgK9gRfh6LZ0nHZo83IgrIgAA3Bn2qe78ZNf5Wwgrc8s1hHsAgK8ELzADAKoDwgcAqA4IHwCgOiB8AIDqgPABAKoDwgcAqA4IHwCgOiB8AIDqgPABAKqDFz7n9JWcD3Lv9tHu79pzZek4JgeAEljhc7/CdT7OjZKT5kqOsWc6fNrTlVVdu9YbOA4WgO3EhG8cS/x5VJScNFFOOOZ+lz2MgX7JzqnUR5de5I27hdsAfAFp4TuT7zjm/ipZKfcGhA+AzXDCd4zKbN0cvOcx90cVfJ43cDoYAJthz+NbWVguS669I+66Y+4Zm+e1pBNmSb9W1EI/LuOz2WFjkTewzQfARgoivgOjr8uOuee26uR85iDd0CQWhDd6V+Y/97ulzBuI+ADYTNke3xj57B9vVx5z79mcULTkMpNdeB5zMmuZN7DHB8BmCoTPaDsaD45x8o65Lys0tHkJa3utD4j4Dlhvlh36D+EDYDOr7/GFhO9ZsKwNyMJj7svekuNsnjJf8qN7fM49juqGVbeh5OMhE0HwSd7ABh8AJax+uXErjn1H73/AXRsKgHuDb3UBANUB4QMAVAeEDwBQHRA+AEB1/AfZ/MDg1G+YAAAAAABJRU5ErkJggg==" alt="" />
这里解释这个方法的细节:首先k是必须的参数,如果只给k一个参数,则d默认为None,也可以指定d的值。
a = dict.fromkeys(['scolia'],123)
print a.setdefault('scolia')
print a.setdefault('s')
print a
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAa4AAABKCAIAAABsNSftAAAISklEQVR4nO2dy5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6cOes96Vf+DbyABwSgpSOKx3C15hJIXeZ6D6Hq8QAE6EIIXLMzT+596FWKFS2gbfj58ev8EpBIBzwSPWAABIIQAAUggA4JBCAACHFAIAOKQQAMAhhQAADikEAHBIIQCAK0vhtIerq9ut6/t39PqshUeW7nUdAFRQkEKrl+22ajab+f4NafaxcHq9MFKaVaU7tn38lyYBYI2yFCb7LGSpSSOXcdTHAzZamCEVvt5cCtm/VLrVXlnzi96zGHtbAnnpvt9JB/giaqTwrZz04wFHWevvduFcILhWK6V/fWcz8ATP1Z4AHyYvhfussCqWjaf8eMBe1j4bhM39nB7w4xsAsEJOCtfj7suy7KUZ9+GPBwi1SLcaE8oKZSj23eRsXrBRyCNcckv9RbgQoJrNXuG+btGnPh4ghfyaaMGe2iOeGB1Jt7bd0chM6BGvEGArr8QKB1/oxfn28Y8HRLUoaFxxcSouV3ezMMghL3JlCwEgy2YptGacjS9Oub0+HrDNjLQWizPcG7ODV7jDKjXIuTeNt3A3wW2TQCORQoAnqHyuMCV9piOXSeHP/X4fD9i2UpZqMRWYi0t65yhvBZ80xuhuXi5NwXVeE6w4Z98djlso6AkChQDPUPm2yZez7zODfwDn6TqA74B3kAEAkEIAAKQQAMAhhQAADikEAHDO/R8XjNqSDmpURwAAAABJRU5ErkJggg==" alt="" />
其执行顺序为:
1.先用给定的k看看里面有没有对应的键,有就返回这个键的值。
2.如果没有k这个键,则创建这个键,然后用d作为其值,如果d没给就默认为None(空对象)。
4.删
1. del x[y]
用del关键字删除对象,在列表中讲过,只不过y从索引变成了键名。这里不再重复。
2. D.clear() -> None. Remove all items from D.
删除整个字典里的元素,返回值为None。
a ={'scolia':123}
print a.clear()
print a
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAbcAAAA7CAIAAAB38FOrAAAGjklEQVR4nO2dS3rrKBBGWRZL6I2EZSgruMw86t6BpmHuVWiiHXgT6oFePIqSItuKb3zO11+uYyMoCvgpSnLaDAAAUMf8tAEAAC8NKgkAoHFUJfurtd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" alt="" />
不需要传参,但要注意就算清空了字典里的所以元素,最后得到的还是一个空字典,而不是空对象None。
3. D.pop(k[,d]) -> v, remove specified key and return the corresponding value.
删除指定的键值对,并返回相应的值。如果没找到键返回d,若连d都没给,则触发KeyError错误。
a ={'scolia':123,123:321}
print a.pop(123)
print a.pop(666,'没找到')
print a.pop(999)
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAeUAAACFCAIAAADq0XfbAAASEklEQVR4nO2dy7XrqrKGyeamQAgnEROGHMGm59a+Gai76J2Gk9gadxwiOE6C2xBvCgnJkmzm/L+xhpcn5lmC36WSLJgBAADQA2xDXv3k/KnfbXESbGBsVO/WQ/H9PbwGNbID7HBIWy/Jl405iXVTt+T5BK12PnxGHWqQ82bLlfPwd+D1el5Xd8buTEyVzC/JHzIyvxKuCLuLDdNn12zTT86GtK2oz3Za5D3cy7cJRMvRSTILZY+OUEbLR1vBnR1zh6NmtGVjvuRtdUm35LmeeOyrHDujXpIfVdumUXxJzb8Xq9daPrh8GWOMmUT9KzHKZmzmeRbqJ1+cjlo+o093zN1oxarRuSqTdH1Wrrqih6283cMTaTw6luDU+FGcNpzYgdJPzsj1eZEx0yN4biljtjqPn59R9EjTURxpw/Nq/sVYvY7d0shGxSTLAw6Nep3Vs2PuxkXy4slB3RkSeb+H5/FqPTo2c+nwnjSc1IFSIxOjILz4a4y5r5XdfdvqPH58Rq3Ollqeb6v5V2P1ut0uSmSHIY5R2D+Fmr3gO2PjH/tm/hfkQ8oHY/fgC9twxyxMNt6X55kzFj6+TDPEPVRiYGwUYnD1XNHDBKLUQ+o0oJnkKdkyaxOnhtDrEMJK/WLburOJson1Unlbs9nT8/S5Nv7gbPxbPhh7cD4wPt74YC3mmwsdLqya56H6kx7BP5VR2LiQj6cRx73JGunYJ8EGIUbG7kKM9WM6iWQeFjOz1lY5N0qDlHl88HD++iRHmh3BVWuQs/f6mvNpsLK6f5jcb9Zro8YoElpKSThhV7IUDv/n4Ow7pzsXMnjHZR5j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2o943DsAABhjPqfXn91nqJX42Yw7Oe3JigCA38an9LoHxzOW2nJPo5YUY8whog8AAF8eD3lJ+UEffBL+CTXlnkYtKY7rtkECAPxkvlmvv2dv5vz9Pw0p1zwPFwDwezhYr+ftc0bhnjron3xoH2BoHyl7X93pJ3qa4sKDE6lHNS72ZyZ9miKdJxHZfE+jfxpSoNcAgGM53L92TyUPzwJXY/bUbJu4utPP1qeSN/anfFp5maeos9zTqCWldSAAALDOGXq9sKFOma1WqlYwq0as+eC79/6pXCcsg9GrKbhFBABwBGfodem9vqfXSq1ocdgls6U/pH+dpcxFhQuPlHsataRYXpLXwjUAANDOKXr94C6mHIWhH9GWR/emnX7cDmQLzqndhf42cjZw+V96A7qoPzP5bkBUHmNM5GKXgfKWFNsWnGsAwBGcFw/5Elr6s5AHv28EAHwLx98fcieuLn6Mlv58W58BAIDkm++/BgAAEIBeAwBAH0CvAQCgD6DXAADQB9BrAADoA+g1AAD0AfQaAAD6AHoNAAB9AL0GAIA+sHptf+MnlH8adctvyudSd7b2lI+TeGOPLgAA6A7vX7c8664kPHljkhf/nvu9PboAAKA3Ur1WYqMfOuv1Sc94Wq72zT26AACgM2K9zjbEyp47ajP4p6QOVhCjaEOx+9c/5H5gWc21PCu8uUcXAAB0RqzX/Kn1k8ebFjql9s/1dw+xc9GP0r8ud/8qU+gduTY/Ie+tPboAAKAzivh1iPNa3zm+WPeSfN5F1z2SP1JquwsMuWNALaVlA8YGduzRBQAAvVHodXCr/RtjtHaer37ybGtzV+pW2/2rTCH96616/dYeXQAA0B3F/Xzxjlz+ZrjYMw27Ghb385W7f5H7gRkifk3kWRPx/Xt0AQBAh2z+vcwkEAUGAIAP0K7X0f0hAAAALge/RwcAgD6AXgMAQB9ArwEAoA+g1wAA0AfQawAA6APoNQAA9AH0GgAA+uD/AaeHI1rp5BuPAAAAAElFTkSuQmCC" alt="" />
4. D.popitem() -> (k, v), remove and return some (key, value) pair as a 2-tuple; but raise KeyError if D is empty.
删除字典中的第一个元素,并将其按一个元祖返回,如果字典为空则触发KeyError错误
a ={'scolia':123,123:321,555:777}
print a
print a.popitem()
print a
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAeQAAABLCAIAAAAu8WMhAAAKDUlEQVR4nO2dy5WruhKGFZZCOIlYYbgjOJp5dE8GTFtzR8GgycBJcAe89CiJkgwYmf9be/WytfVCKn6KQkaiz6J7Svns8sqEtErchWjMu/VQnL+Hx2AascE4bNLWS8v0YLZqfag5eT4Bd5w3t6hNB2Q/aznSDr8dYX0eTqofIX6EaiP5X1o+tDX2Rk1FxI/KsJ0iU+ueUtzdtqw+jzbh97CUs6kDZ3aczMqMs6NM3+kHr2Bhx6bpiA1aejBf+rZ6PnPyHI997Ktsa1EvLbeqLesoTlLzFVnEutMPqV993/d9q+IXQytbP2YeTLB7yqQtdvpp/W+B4Vqnq2kmJ6XVU5/NVF3QQy5v93BHmLMzsrgz81Hsdji269Q9pSBPzoMG053BfUv1fa7b+HmLoo/UPYotx3C/mi/JIta2Q2oNUGBhfpyBKdZePQWGaxfxizszWhgJeb+H+/Hizs6YOXR1dzoc13UyjVCNIvz3YwazrJXivuW6jR+3qFVrieU5W80XZRFr/qAY5c2BHZoYvyoz+L8/QjS/44fh36IdWj+E+Fm84DHKMajSGOPz8wwZA+9euxnsHhp1F6JR6j7Vc0QPHYhSD925QUwnT0iOyTruDCHWS+TK9YjH1qcxMWNivJTf1jDs7u35UJt8SNH8Tz+EeEh5F7K5yfs4YnNzS4eDUfXzUP1xZ/A3chRjOGgOoxHzzhoN99hbJe5KNUL8KNXE57RVjh0GlhlrK7SNcEDCPHPMcLh2kkfqzeDqaJDWe3zNvhmsnN1fo/UlYt2bxop+hjqy3KcbHarG/PU+De6QPjmPi18c5ul7Msphnr7G+T28K+PWvGcPXWaneG50kDM7CBvm8eCLdcydSbjYYeutEndB3j+l2hrrMWoSYnvERPPbt2o8G4e/sfskclQ59z1EPZGjSNRMz3IQ7nCO3ajhwBvTtaZLjapvh1SK21b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HCs7ZkcnLgOCkZHKfVMYWzA/TTJgjLUaxucjZ/fd8HL9s+DZ41AFchEbNOJ3K31k3gPFJjpxQxeMX2/jSxzqf3VqeCEuFjv8IeFmyfhpg1AFchIdb+5lt/jJQcJ6/gAlC4zeP8oNJe6DJ90ht41hvEIgq2TyN7CAD4TpLrrMNYLydlTM16pMZMKVvFHK6DW7q91OfGrO01icvWkqEXPbrst5tMBO3XRqNw+zQErAG4EBX9gnHXDcNq5KwTBQDYAbwbBAAAKgBiDQAAFQCxBgCACoBYAwBABUCsAQCgAiDWAABQARBrAACoAIg1AABUwP8BhuaEr6nEcqcAAAAASUVORK5CYII=" alt="" />
但还是那句话,字典中元素的顺序是未知的,谁知道第一个是什么,所以多配合迭代循环使用,逐个删除掉。
5.查
1. D.keys() -> list of D's keys
返回一个由键组成的列表
a ={'scolia':123,123:321,555:777}
print a.keys()
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAbkAAAApCAIAAAAnJ8CIAAAG20lEQVR4nO2cy5XjKhCGCYsQbiImDDmCYefVvRlo2+wdhRajDJyEZiEQFBQPyZJszf2/M6dPW42K4vWrKOEREwAAgBri0w4AAMAFeE8rx6eUz/FdHwYlOiF6864dju/38BxML3boh13qemlZ7sxB1bu6pcwnaO3n3WfUrh1y3Gw5cx7uTUEr5zl9F+Iu1JAv89BB041yt4i7WjF0m0Z6fErR0boCn+2QxB5u5dsWZ8vokMLK2NFRZhr1o+3GjY654ch1WrkzX/pWXU4tZc4nbHuVfWfUS8u9rK1qxZdYPoOsVo76IfVrmqZpGlT+URAUm2zheQaMT1mcCqN+Bn/dMG+C1WJ694getPPZOHOJh6287eGBNI6OxT/Ml1Yc1pwwcBifUrBr46TOpCN47F3TtDZo+vyM4ltKW7FnHx5n+RSyWhmGY4F/yQDHm9xGrYzsbJg34S3x7aRDN27D3/fwOF6to2MLp4HeQc2hgYPpheoVE72e05nbatns29qg6eMzqjpbcmW+zfJJZLWy3Sejoi4I98X2ozJz9HcXov+x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4f93BfzjzLdu1zc++xY9urG8w8tSN6Nf6aM5UHru6jv+M4+PdQ34F9YUri0O/yEPw/SWZmC0uab5cvXFyeQ1c3vg8OAAB1oJUAAFAHWgkAAHWglQAAUAdaCQAAdaCVAABQB1oJAAB1KlpplBBCCCEve3rLKPHBo5NJ7aOWc4/iPCcAl6KmlfqyImkxag+ddxIXKVxViHO1j1pDKwG4Equ10qhAHZYoaVERF4iuiZwCK0IIqX83XGmTv1HLHSPiVBlHfSuYL9QOrQTgYqzRSquDwX/usyiFUUKonzCMGrVsVcvRmPh/eGi4cj47buehlQBcjLVxZagXud/5C3mglQCAb+cdrfTQ3aZR695dGH27SbK7brmyHZ8nIFFwHDTLuC7a9jjK5s1kgFYCcDF20Eo+LbcmCPO1GDWrU8uVrXBpR5svWP7k2kTalt4YXVk+VhsPrQTgYryrlXnh2rZjrerRZsuRhTBmLFRRzDOwmYemN1vQSgAuxltaOWoZ7Fk1ebfjRXTNwRqjhNS/G660WS63KYwil9/0DnFl/b0WtBKAi7HhPfgSNQWffV4xDawaTjimGcSWKxvPTqYHnbgz9zRfGR5Z8kKdxpA2YL3dZDmtCq0E4GIc/b2dfU84nmP5SPC9HQCuCb4PDgAAdaCVAABQB1oJAAB1oJUAAFAHWgkAAHWglQAAUAdaCQAAdaCVAABQB1oJAAB1oJUAAFDnD2Mx+0qHlegfAAAAAElFTkSuQmCC" alt="" />
2. D.values() -> list of D's values
返回一个由值组成的列表
a ={'scolia':123,123:321,555:777}
print a.values()
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAa8AAAAqCAIAAACLA0OJAAAGl0lEQVR4nO2cTbqrKBCGWRZL6I2EZZgVXGYZde/AaZhnFU7cQTZBDwAFqkA0atLt9z73OT8eLIoCPgo0V1gAAADWim87AAAAPwHUEAAArN1BDceXlK/xUyuDEp0QvfnUDsfve3gOphc7xGGXut5a1oM5qOVQt5T5Bq1x3n1E7RqQ40bLmeNwJXU1dKP2LsRdqKFc5qGjxhkVbhF3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t9yCxnJxxZ3dVerke507pbIDRqFLLLXW154bxGPMeMjNlbik99yxEw1ZG75mWKY2zm6HxDZvFLcm0FQ230P2d6dMkLpRUw7wsqCHZ2zPnCHGZf/6aF5M7dzTGeugKPKQMpwkHejhN+JTkBGfaCg3cuWH93aal3uEeceYnZeybOnHt0WFNFB9yF58YJieqxoZcTPVKdEL+CSdr/mt0WJafOydRzcsUWhH3IN+K6Ryzv8kueiWr46IRnxumdrg4G9Ub+zYmqivv00HrQSfjMB2ZJZ/5M+W06+n4CdaYTuR7kCuTWy6M3tMs8ytcy+xm2PGzKDSnTeB2ryczeVhKHr/u4b6c+W7XT79HdjA7tn1xG1hm6YW2Jf437xhun90nfDJvmJ/p/Ab+4WOSS/6Wh+CakJHZwnTctstHDP7zfDK78TllAACwFmoIAAAOqCEAAFgLNQQAAAfUEAAArIUaAgCAA2oIAADWtqihUUIIIYS81ttMo5au3XgLEYBr0KCG+loymDJqDTUE4BKsVMOQKM4505RDpVmUUWtzqsiQT0NbrrSS+EN9pu2aikINAbgGq9TQqEmDRi2FetpR38IVo7ySeGVZq4ZTPSb8D/4tV1oMZ/7kPj9JuyLbUEMArsLWnbJRQpnpW3yJ/LwOKj8tV5Zo9jO/ADUE4CpsUEOjuMxv1DLavG5WQ6q9LVeWzbL+EJ9pu6CGAFyFD3NDTyor5M/tHCSGnD/EZ7Yg1BCAq7D5mfIsG0bRRxob1ZA+e2m50mQ49YfzmS0JNQTgKmx9ihLkZNQyejyhC+dxjeJIiy1e2WI58/nJtWsuCzUE4BpsfsPG6Un0Zsr01gvztkokN9WqtqjhouXcn7rPWXVQQwCuwgmfReEP6PbgOMv4LAoAlwOfUwYAAGuhhgAA4IAaAgCAtVBDAABwQA0BAMBaqCEAADighgAAYC3UEAAAHFBDAACwFmoIAACOfwED0+KBbzVQSAAAAABJRU5ErkJggg==" alt="" />
3. D.items() -> list of D's (key, value) pairs, as 2-tuples
返回一个由键值组成的元祖的列表。
a ={'scolia':123,123:321,555:777}
print a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jI9wraESBew0N1i2J/R5pmsPSKnuUwpHcJlXdq20f7PKMkpPzupaWidRiG+NCbLhX0ITL3esGPQoaVfJKgW8V9Hy5ezX6dpPkcJ5Tcmy48FSQU1LDvW4HaxID+iGj9GdKe+ZOEte9TCq2MbhX8DFquVc+zXYqeo31XMO9OrM2anYpOSVHRivyrlXcK+eA5Jp8sX+CedY60f6uJawfz8e5xTYG9wo+Rlnu1bNqxqIj3uasf+V65voseskeeXGU8zg5PC/uddx+yaXzolVIZJqYYDotwx3Vj7/UjEhYZGOMxuBeQSOKbg74+5UYtGvPo9bktcO2J47c4LGtvJ4TuVe6p7J93zamP/qvjJIjt0oLXfbpefH/vL5Gc2LV9Td9QfSaeqVZorE8G/NrbmVwr6AJp++9+hdinM9bljIMYbIjqq1VuufI5sscKzWvIyW5YxW510vmxcDddwu/TEJzr+6ttO3JEq7FEhbfbjKWoD5kCYdsDPdewYf5yLe2rr19GXROBDs6FubVkpYaw7e2QGvwNwcAAKAKcK8AAFAFuFcAAKgC3CsAAFQB7hUAAKoA9woAAFWAewUAgCrAvQIAQBX+A1c7lKW8tT3jAAAAAElFTkSuQmCC" alt="" />
4. D.iterkeys() -> an iterator over the keys of D
返回一个包含所有键的迭代对象
a ={'scolia':123,123:321,555:777}
b = a.iterkeys()
for x in b:
print x
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAa0AAABOCAIAAAAck974AAAHZ0lEQVR4nO2dzZnzJhRGKYsS0ogpQ1NB2HmTpANth72r0CLqwE0oC/3xL2zLshXOefLMN2YQXC7w6oJkIv4CAKgbMQAA1A06CAC1gw4CQO28poP9Tcpb/6oNnRKNEK15tZwY32/hMZhW7OCHXeq6a5l3Zqe2XV2S5xOU+nn3EbWrQ943Wo4ch4+Q0cFxvP4I8SNUl85z1VazjJovET/qgW55qhf7mxSNW5dl8+Ru38Jn+baJV9I7TmZlpt5RZuj1tezCJw2buyPltLwz7/qyOVVK8hyP3fZN9h1Rdy33Ku2hVnxJya+S1MF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SI50h8APgzfJwGA2kEHAaB20EEAqB10EABqBx0EgNpBBwGgdtBBAKgddBAAauc/q7PtKps7sEIAAAAASUVORK5CYII=" alt="" />
5. D.itervalues() -> an iterator over the values of D
返回一个包含所有值的迭代对象,原理同上,不再详细说明。
6. D.iteritems() -> an iterator over the (key, value) items of D
返回一个包含键值对元祖的迭代对象,同理。另外注意下元祖的迭代循环技巧。
7. D.viewkeys() -> a set-like object providing a view on D's keys
返回一个键的类似集合的对象。集合的作用是去除重复,但字典对于键的重复已经有相应处理,感觉意义不大。
a = {'scolia': 1,'scolia':2,1:123}
print a.viewkeys()
print type(a.viewkeys())
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAawAAAA8CAIAAAC1uxkMAAAIfElEQVR4nO2dy7HrKBCGCYsQJhEThlwTwLDzZmYy0PawdxTaqCYBJ6FZCEnQ3SCsl+Wj/6tbp+7xRTwa+GlamKv+AwCAC6P+BgCAC6M6AAC4MBBBAMClgQgCAC7NIhFsn1o/27VFN0ZVStVubT4S56/hMbhabWCHTcp6WZ03ZmPmTV2S5hOU2nnzEbWpQfYbLUeOw/fhItgP1rtSd2WaxFMvqx82aJMzwyPqbt7ok0Vd2D61quKygjp7W9MaLuVss66kd6LExvneMa5r7aPswYUVG7ojZbS8MV/2NjtPStIcT9j2WbYdUS+rt8rtrVacJOdtoCLY2oe2r67ruq4xafEOknU+cd+17VNn+7i1z+BfFwyIYBq4elhUGzvU2Q3ZsRqWsrqGO1LYO55p+R1bsVtzwqW+fWolDvqDjBn34L5Pdd27bs7nR5Tc0rgVW9pwv5w3gopg6EAFBbOeo/vNQhEk+SwYEOEj9PHIUgt3xOtruB+v0t7xiblrtlNz4qXe1crURvA3jzHmslIW1+1dN+fjI2p2tKTSnC3nzaAiWF6YM6Rt4RbV/2pc76/dlap//F/6P9OctPah1H3y2vxut5/tPoZC0/QJmTdq4wRhDZ2plKqNqYZ8jqhhhPDUw7ZxkChKw3lnKETLryCCUwQj9uB86YNNnP8w/R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AIALg1EEABwaSCCAIBLAxEEAFwaiCAA4NL8DziIjqU8cXLwAAAAAElFTkSuQmCC" alt="" />
8. D.viewvalues() -> an object providing a view on D's values
返回一个值的视图对象,注意不是集合对象,没有去重作用。
a = {'scolia': 1,'good':1,1:123}
print a.viewvalues()
print type(a.viewvalues())
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAbEAAABBCAIAAADQVw1LAAAImklEQVR4nO2dy5HrthKGERZCcCJCGFQ5AGOnzb3OgNvBzgtFwQ3LCSgJekG8GmiAEEVSlOb/6tSpGQ0ejdfPRhMixb8AAAAc4t0GAADAiRD/AwAA4BATAAAABzQRAAAC0EQAAAi8rInjXcr7+GopgxKdEL15tRyO81t4DKYXG/TDJnU9tKx35qCWu7olzTto7efNZ9SmHbLfbDlyHq6ironz3L0KcRVqKKe56aiJRrks4qqeGKJVIzrepehoXZHNtutTC9dytkXYMjoksTJ2dJSZRn1ry7jSMDccpU6rd+ZDXxaXTUua44nbvsi2M+qh5ValPdWKk5S8GTVNHPVN6sc0TdM0qLK0R8kmm3ge6fEuq0M+6nv01xXzI1oVpneX3EE7m40rLrOwlZct3JHG0bGEi7NvxW7NiR2B8S4FuwYO6kw6gvvmmqZnnaD3zyi+pbQVW/bhfiVvR00TY/cqsiMbyHRz2qiJSTkr5kecJc1OOm7l9vl1C/fj0To6NnHuuO3UHOoImF6oXjHe6DGdua6W1bY96wS9fUYtzpZSmrOVvCU1TWyv26ikqfF+1v6qzOzNXYXof+wP87+wRLW+CXENPp3dGs+L38Zf0jRzwsxX1TRBbKFRnRC9Up0r5wgLCUyumx5pgImkyXlmZpCLM6OJIdxB/Ttbu+sTYz8s50rrmrud7unm0uRNiv7/+ibETcpOyP4iO9tjvrpgcNaraRrOHjqCP4VW2BiCj70w497UG7TtgxKdUr0QV6X68pgOiszDbGaW6srnRt4heRofaJovUWxLkxFc7A129h5fcjoNFlZ3feFso4mT6aPIVL5cw+bO6Hxx+l8714b5c+cKBS8vTzNN7NbY3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E4jNq2W5VWvKLubJ+PchrB+C3sJMmbrVSdywn8xPjNKWf1xm1rhVsrvxaA1EEYEuWNHF2WKS0nhHrvyTh/2iPWfCP5hTK2J9sGuYuAl3wPnGcy2ULW8hQP/nsKU0kjZPJjZmnJQiaCMDHUNdEJwhkt5gsQv+3qm+VFWz3pkGZfJZKOXkaL1l+b8tWvVITqSBWEtaBJgLwMdQ18SmdYjOWmNWGrvDg4LVrYuyUxrnSQzb5vYhFTTSqdE7nmEhCmybi1jMAm9LmJxpV1qngQ0br1aUxprhe022oy1LzSX2x3p7IkxtH6nnSvJko1jUxlppRa3KPJYhlo9i1XEUWc3lb0rgFJBGADWmLJyplBYw9j8Kcfm45XJid6bHu3eUihZD6n3AYRpBbvrE9Ux6GzA/R+OLL5xPTdpGYqHC7c8YhXTwJw/TYulyuA3A+EYA9wfdYVp/v44ON++TC91gAOIgDNJH6XEveIwAAvJFf5ScCAMAC0EQAAAhAEwEAIABNBACAADQRAAAC0EQAAAhAEwEAIHAeTfzWZxl8a7sA+E7OpIn89zs+/c0HeBI2AJ9E8zNl9xWi+jfePvfNB6WHjcFzBOCktGhiKg0bvVegnY9/88GBJQMAXmJRE/NHrxy/nj/9zQeFwrGnBuB8LGhiunDpewV+4ncRuB//VP5hXtEzZ7I3BDTxJW8+KIAnfQFwPhqfs135MMimeyKhe+JrCKblbwho4WvefFACO2gATsfi3pl5CWgWXpRSj9GNg8Y3BCzyPW8+qLYOAHAiVtxjyd8rkB+Xcf5d6if6NwS08EVvPmCqhyACcEZWnMXhju6RPbFRQkqZPhM6D9e1VCt8cPLD33xAq4IgAnBStjmzTRc5nCAAwKfyoiYyd4LtR2c5aII3HwAAnuA83+0DAID3A00EAIAANBEAAALQRAAACEATAQAgAE0EAIAANBEAAALQRAAACEATAQAgAE0EAIDAf0fYZD3sogMTAAAAAElFTkSuQmCC" alt="" />
9. D.viewitems() -> a set-like object providing a view on D's items
返回一个由键和值组成的元祖的类集合对象。
a = {'scolia': 1,'good':1,1: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/uFSbLdQEAbsXaA0/cC0KcxbpR/Tai9/qEDY9s/tpXmLi1QUMBuDkFjt+7SoHgCwDwe5Ejo8y98+Gjq5zvwStMAAC7c5EvgwIAwLcCGQUAgCwgowAAkAVkFAAAsoCMAgBAFpBRAADIAjIKAABZQEYBACALyCgAAGQBGQUAgCwgowAAkMX//T2mtXJ8UakAAAAASUVORK5CYII=" alt="" />
同样感觉意义不大,原因同上。
6.改
1. x[i]=y
和列表中的类似,只是 i 不是索引而是键名,当键名不存在时,就相当于新增键值对了。这里不重复说明了。
2. D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D
上面讲过了,不再重复
3. D.update([E, ]**F) -> None.
从字典或可迭代对象E和F中,更新元素到字典D中。这里其实也可以归类到增加对象那里去,但字典的增操作和改操作其实很多方法是可以相通的,这里就放到这里了,让这里看起来不会太单薄。
这里的**F的写法其实是解决冗余参数的写法,详细我们会在函数中讲。
当E .key() 有方法时。
for k in E:
D[k] = E[k]
即当E也是字典时,我们可以将一个字典添加到另一个字典中:
a = {'scolia': 1,'good':1}
b = {'scolia':2, 666:999}
a.update(b)
print a
print b
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAZ8AAAA/CAIAAACw61KZAAAIW0lEQVR4nO2dy5HkKBCGMQsT1pHCDJUFy60uu+uBrs29rNBhFetAOaE9CCQeCUIqpKLo/4uJiW6GR5JAKkkhhv0HAAAtwv4CAIAWYRMAALQIrBsAoE1g3QAAbZJh3cYn58/x3YYGwTrGevVuPRT1S3gNqmcF9FCkrZfkaWUOYlvVOXk+Qa6ei8+oogo5b7ZcOQ+TMMk7xu6M3ZkYInlekj+kJawSpgi7ix3KPjQ245Ozzm3rtcqslehLeJTaltMrY3SczELp0RFqGuUjr+BBwcxwxJSWVuZL3jYXQE6e67H7vknZGfWSvFRtu3pRSc27YVy+pmmapkHEze0oHybbpDPPYzY+eXLwRvm0/vXASFvzW/XmMThII7My1QUS5vK2hCdidSo1Opr1gbn04rTu2A/n8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BEA5iK/o67vJerPeZu4Z//iHbgC0Q2DdarzJ+kquaTh2zzisGwDF8G6vrPMm6yv57D3j3x4iAKAivJvHa73Jej/fes84Qm8AFMLz3eq8yfoAX3rPOHw3AIoR/n+mFd5kfYxvvGcccTcAiuG/VajvJuvfc8842ToA4CDhebfabrL+PfeMI+gGQEkq/1bhS2/fPkatgwDAd4LvTAEAbQLrBgBoE1g3AECbwLoBANoE1g0A0CawbgCANoF1AwC0CawbAKBNYN0AAG0C6wYAaJP/AW9QecB0AEGTAAAAAElFTkSuQmCC" alt="" />
a中的同名键会被b的覆盖掉,且不会影响b本身。
当E没有.key()方法时。
for (k, v) in E:
D[k] = v
即E为二元元祖序列,以二元元祖中的第一个元素为键,第二个元素为值,添加到字典中。
a = {'scolia': 1,'good':1,1:0}
b = [('a',123),('b',321),(1,1)]
a.update(b)
print a
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAesAAAAwCAIAAAC+H8WuAAAIDElEQVR4nO2cTZKrOAzHfSwfYS4SH4Oc4HmXzczcgG28zynYUHOBXIJZgMEfsjHGEJz8f/XqVXfayJIsC1nQzf4DAABQJ+zTCgAAAMgEGRwAAGoFGRwAAGqF/Q0AAKBO2AAAAKBOkMEBAKBWkMEBAKBWcjN4/+L81e+dvROsYaxVe+VQXF/Dc1AtK+CHInO9JY87sxPrrk4Z8wlS/Vw8ooo65LhoOTMOfwkyg4877c7YnYkucOFb8oc0FkQJfQm7iw0BlRV//Yuzxp7L0HkKFFfDXK6WMlJWxxos1LQ6Qg29fKRdmKmYXo6Q0+LOfMvb6iZPGXM+pu2rlI2ot+SlpG2y4iKSfx0ig/fyweV7GIZh6ET4tmkMG6bBY1z2Lx4N0F6+jJ9mRLOxh1Wry5lOap2VFudpmMpuDQ8kcXUmlsJntuIwc8wiq39xRu7Yk5xpr+CxVw3D1gLz8xFFW2pbUdKHx0n+eYgMbpauhte8sHPbFIkZ3JGTEc3mJe7l1jJnNlL2a3gc79TVmQb7RfFB5thFlm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AA1AoyOAAA1AoyOAAA1AoyOAAA1Mr/GATcb9UCwooAAAAASUVORK5CYII=" alt="" />
所谓二元元祖序列,即里面放多个元祖,每个元祖只有两个元素,而其外层为序列,元祖、列表都行。
注意:有些同学在学完函数后可能会纠结,**F其实也算是一个字典,那我传进去的字典究竟是传给了谁?
首先**F是处理冗余参数的,要想有作用,首先参数要超过需要的,而当我们传一个字典或其他对象的时候,是传给了E,当传多个对象的时候,多出来的就到了**F中了,而**F中的对象这里并没有做处理,就相当于多出来的就扔到垃圾桶里去了。