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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM