【发布时间】:2016-06-07 04:56:02
【问题描述】:
我正在为我的 GCSE 部分学习,其中要求我打印一个按字母顺序排序的字典,并且打印应该包含相关的值。
我花了几个小时试图找到这个问题的答案,并查看了这个论坛上的各种帖子,但对于我有限的知识来说,大多数帖子都太复杂了。
我可以打印按字母顺序排序的键,我可以打印已排序的值,但不能打印按字母顺序排序的键和附加的值。
这是我的简单测试代码
class1 = { 'Ethan':'9','Ian':'3','Helen':'8','Holly':'6' } # create dictionary
print(sorted(class1)) # prints sorted Keys
print(sorted(class1.values())) # Prints sorted values
我需要打印带有值的排序键 - 怎么做?
for k,v in class1.items():
print(k,v) # prints out in the format I want but not alphabetically sorted
【问题讨论】:
标签: python python-3.x dictionary printing