【发布时间】:2021-03-04 13:30:06
【问题描述】:
我正在尝试创建一个函数,它返回字典中所有值的列表,这些值也是键。但是,我不断在终端中打印出'TypeError:'int'类型的参数不可迭代'。为什么这段代码不起作用?
def values_that_are_keys(my_dictionary):
for element, numbers in my_dictionary.items():
if numbers in element:
print(numbers)
values_that_are_keys({1:100, 2:1, 3:4, 4:10})
【问题讨论】:
-
因为
element是一个整数。您无法检查某事物是否 in 为整数。
标签: python dictionary for-loop