基本用法:

  • .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。

基本用法:

  • .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。

相关文章:

  • 2021-12-19
  • 2021-08-07
  • 2021-07-23
  • 2021-09-01
  • 2022-12-23
  • 2022-12-23
  • 2021-10-25
  • 2021-07-13
猜你喜欢
  • 2021-08-19
  • 2021-09-28
  • 2021-06-04
  • 2022-12-23
  • 2022-12-23
  • 2021-05-13
相关资源
相似解决方案