1. 程式人生 > >Python3 實現字典的遍歷常用操作

Python3 實現字典的遍歷常用操作

 

# 建立30個隨機數字的自典
from random import randint

data = {x: randint(60,100) for x in range(1,31)}

# 遍歷字典中key

for key in data.keys():
    print(key)

for key in data:
    print(str(key)+':'+str(data[key]))


#遍歷字典中value

for value in data.values():
    print(value)

#遍歷字典中的項
for item in data.items():
    print(item)

#遍歷key-value
for key,value in data.items():
    print(key,value)