基于python字典中的值获取密钥

基于python字典中的值获取密钥,python,python-2.7,Python,Python 2.7,我需要根据键的值从字典中取出键 我想获取在下面的dict中值为1的键 >>> a = {'random': 0, 'fixed': 1} >>> for k, v in a.items(): ... if 1 in v: ... print k ... Traceback (most recent call last): File "<stdin>", line 2, in <module> TypeError: argum

我需要根据键的值从字典中取出键

我想获取在下面的dict中值为1的键

>>> a = {'random': 0, 'fixed': 1}

>>> for k, v in a.items():
...  if 1 in v:
...   print k
...
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
TypeError: argument of type 'int' is not iterable

>>> a.items()
[('random', 0), ('fixed', 1)]

>>> a.keys()
['random', 'fixed']

>>> a.values()
[0, 1]
>a={'random':0',fixed':1}
>>>对于a.items()中的k,v:
...  如果在v中为1:
...   打印k
...
回溯(最近一次呼叫最后一次):
文件“”,第2行,在
TypeError:类型为“int”的参数不可编辑
>>>a.项目()
[('random',0),('fixed',1)]
>>>a.钥匙()
['random','fixed']
>>>a.价值观()
[0, 1]
有人能帮我理解我在这里遗漏了什么吗?

您遇到了错误“TypeError:类型为'int'的参数不可iterable”<代码>如果v中有1:是问题所在

相反,如果1==v:您得到错误“TypeError:type'int'的参数不可iterable”,则应该编写
<代码>如果v中有1:
是问题所在


相反,如果1==v:是票证,您应该编写
如果1==v:
是票证。
如果1==v:
是票证。