首页 > 解决方案 > 如何从 any() 函数中获取变量

问题描述

我正在寻找满足条件的 list_select 变量并在下一行进行追加。

我怎样才能使它在list_select.append(dupe)行中可用?

if any(list_select in dupe for list_select in pattern_dict):
   list_select.append(dupe)

标签: pythonany

解决方案


你不能 with any,它只返回一个布尔值。改用生成器表达式:

gen = (x for x in pattern_dict if x in dupe)
list_select = next(gen, None)
if list_select is not None:
    ...

推荐阅读