Loading

python 遍历字典

起因

for循环遍历出 对象(字典、列表、字符串···) 的 键/值

For循环

  • 遍历键
dict = {'Name': 'Runoob', 'Age': 7}
for i in dict:
    print(i)
  • 遍历值
dict = {'Name': 'Runoob', 'Age': 7}
for i in dict.values()
    print(i)
  • 遍历键和值
dict.items()
for key,value in dict.items():
	print(key)
	print(value)
  • 取列表中的第一个大字典 中 各个小字典的 键和值
    latest_list = latest_platform[0].to_dict()      # 取列表第一个值.转换成字典
    for key1, value1 in latest_list.items():        # items()以列表返回可遍历的(键, 值) 元组数组
        # print(key1) 
        # print(value1)
        if key1 == 'id':
            platform_id = value1
        if key1 == "task_category_id":
            task_category_id = value1
        if key1 == "target_image_category_id":
            target_image_category_id = value1
        if key1 == "platform_key":
            platform_key = value1
posted @ 2020-07-13 17:45  Unixcs  阅读(1060)  评论(2编辑  收藏  举报