Dictonary(Python)(一)

基本用法:

  • .keys
  • .values
  • .items
>>> D = dict(a=1,b=2,c=3)
>>> D
{'a': 1, 'b': 2, 'c': 3}
>>> D.keys
<built-in method keys of dict object at 0x1022ceaf8>
>>> D.keys()
dict_keys(['a', 'b', 'c'])
>>> D.values()
dict_values([1, 2, 3])
>>> D.items()
dict_items([('a', 1), ('b', 2), ('c', 3)])
>>> for (k,v) in D.items(): print(k, v)
... 
a 1
b 2
c 3
>>> 

Dictionary 使用.keys()不能返回一个list,需要使用list(D)sorted(D),会返回 Dictionary 的 keys,并存入 list。

原文地址:https://www.cnblogs.com/yaos/p/6985687.html