1. 程式人生 > >Python中dict的各種列印方式

Python中dict的各種列印方式

dict = {'name':'ngy','age':23,'height':180}
1. 最簡單的方式:
print(dict)

結果:

{'name': ngy, 'age': 23, 'height': 180}

2. 逐個列印key:
for key in dict.keys():
    print('key = {}'.format(key))

結果:

key = name

key = age

key = height

3. 逐個列印value:
for value in dict.values():
    print('value = {}'.format(value))

結果:

value = ngy

value = 23

value = 180

4. 逐個列印key:value:
for key,value in dict.items():
    print('{key}:{value}'.format(key = key, value = value))

結果:

name:ngy

age:23

height:180