当前位置: 代码迷 >> 综合 >> Python items()方法
  详细解决方案

Python items()方法

热度:60   发布时间:2024-02-09 02:20:51.0

items() 方法的遍历:items() 方法把字典中每对 key 和 value 组成一个元组,并把这些元组放在列表中返回。

d = {'one': 1, 'two': 2, 'three': 3}
>>> d.items()
dict_items([('one', 1), ('two', 2), ('three', 3)])
>>> type(d.items())
<class 'dict_items'>>>> for key,value in d.items():#当两个参数时print(key + ':' + str(value))
one:1
two:2
three:3>>> for i in d.items():#当参数只有一个时print(i)
('one', 1)
('two', 2)
('three', 3)
  相关解决方案