Python 字典遍历:for 循环输出结果
在 Python 中,当使用 for f1, f2 in students.items(): print(f1) 这样的代码遍历字典 students 时,输出结果是 A. 字典的键。
students.items() 方法返回一个键值对的迭代器。在 for 循环中,f1 代表键,f2 代表值。因此,print(f1) 将输出字典 students 中的每个键。
例如,如果字典 students 为 {'name': 'Alice', 'age': 20},那么输出结果将是:
name
age
总结:
使用 for f1, f2 in students.items(): 遍历字典时,f1 表示键,f2 表示值。print(f1) 将输出字典的键。
原文地址: https://www.cveoy.top/t/topic/lRrH 著作权归作者所有。请勿转载和采集!