【代码】python 输出dict的value、key和排序

1 输出dict的value

代码:

test_dict = {"3":"happy", "5":"sad", "2":16, "10":"smile"}
for value in test_dict.values():
    print(value)

 输出:

 

2 输出dict的key

代码:

test_dict = {"3":"happy", "5":"sad", "2":16, "10":"smile"}
for key in test_dict.keys():
    print(key)

输出:

 

3 按照key排序

代码:

test_dict = {"3":"happy", "5":"sad", "2":16, "10":"smile"}
test_dict = sorted(test_dict.items(), key=lambda k: int(k[0]))
print(test_dict)

输出:

 

 

 

 

 

おすすめ

転載: blog.csdn.net/wss794/article/details/105876007