在Python中,有多种方法可以遍历字典并输出其内容。以下是常见的几种方法:
for key in dict: print(key) for key, value in dict.items(): print(key, value) for value in dict.values(): print(value) iter_dict = iter(dict) while True: try: key = next(iter_dict) print(key) except StopIteration: break 需要注意的是,在Python 3中,字典的键和值是无序的,所以遍历输出的顺序可能与字典的定义顺序不同。如果需要按特定的顺序输出字典的内容,可以使用OrderedDict来保存字典的顺序。