【发布时间】:2021-08-07 15:43:03
【问题描述】:
例如下面的代码:
dict = {
"foo": "a",
"bar": "b",
"see": "c",
}
string = "foo in the city"
if any(x in string for x in dict):
print(dict[x])
有没有办法得到x? (或者通过其他方法获取dict[x]?)
【问题讨论】:
-
这能回答你的问题吗? How to pick just one item from a generator?
-
即
next(x for x in dict if x in string).
标签: python