Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 简化了列表中的条件_Python_Python 2.7_List_If Statement - Fatal编程技术网

Python 简化了列表中的条件

Python 简化了列表中的条件,python,python-2.7,list,if-statement,Python,Python 2.7,List,If Statement,下面的代码需要三行。可以简化为一行吗 code = [x for x in code if not 'W' in x] code = [x for x in code if not '06501' in x] code = [x for x in code if not '06502' in x] 我试过了,但没有成功 code = [x for x in code if not 'W' or if not '06501' or if not '06502' in x] 这应该行得通 cod

下面的代码需要三行。可以简化为一行吗

code = [x for x in code if not 'W' in x]
code = [x for x in code if not '06501' in x]
code = [x for x in code if not '06502' in x]
我试过了,但没有成功

code = [x for x in code if not 'W' or if not '06501' or if not '06502' in x]
这应该行得通

code = [x for x in code if not ('W' in x or '06501' in x or '06502' in x)]
这适用于以下输入:

code = [['W'],['06501'],['06502'],["hi"]]
code = [x for x in code if not ('W' in x or '06501' in x or '06502' in x)]
# >> code: [['hi']]
您也可以使用这样的数组:

disallowed_values = ['W', '06501', '06502']

code = [ x for x in code if not any(y in x for y in disallowed_values)]
这应该行得通

code = [x for x in code if not ('W' in x or '06501' in x or '06502' in x)]
这适用于以下输入:

code = [['W'],['06501'],['06502'],["hi"]]
code = [x for x in code if not ('W' in x or '06501' in x or '06502' in x)]
# >> code: [['hi']]
您也可以使用这样的数组:

disallowed_values = ['W', '06501', '06502']

code = [ x for x in code if not any(y in x for y in disallowed_values)]

你最好这样做:

remove_item = ['w', '06501', '06502']
code = [x for x in code if x not in remove_item]

你最好这样做:

remove_item = ['w', '06501', '06502']
code = [x for x in code if x not in remove_item]

这是一种方法

[ x for x in code if not any( i in x for i in ['W', '06501', '06502'])]
替代版本

[ x for x in code if not any(map(lambda i: i in x, remove_items)) ]
修改相同的设置以使用过滤器

filter(lambda x: not any(map(lambda i: i in x, remove_items)), code)

注意:选择使用哪种语义主要是个人偏好。但是,最后一个版本可能更好,因为它返回一个生成器对象进行惰性计算(在python3中)

这是一种方法

[ x for x in code if not any( i in x for i in ['W', '06501', '06502'])]
替代版本

[ x for x in code if not any(map(lambda i: i in x, remove_items)) ]
修改相同的设置以使用过滤器

filter(lambda x: not any(map(lambda i: i in x, remove_items)), code)

注意:选择使用哪种语义主要是个人偏好。但是,最后一个版本可能更好,因为它返回一个生成器对象进行惰性计算(在python3中)

可能重复的can you update sample of code variable,它是什么样子的?可能重复的can you update sample of code variable,它是什么样子的?这与OP要求不同。您正在检查
x
是否与
remove\u item
中的任何项匹配。OP的要求如果
remove\u item
中的任何项目出现在
x
中,并且在我的回答中似乎对测试用例不起作用,则返回所有项目。我在回答中做了。我不是OP,所以我的测试用例可能是错误的,但它使用OP提供的原始代码工作。(
code=[['W']、['06501']、['06502']、[“hi”]]
)这与OP要求不同。您正在检查
x
是否与
remove\u item
中的任何项匹配。OP的要求如果
remove\u item
中的任何项目出现在
x
中,并且在我的回答中似乎对测试用例不起作用,则返回所有项目。我在回答中做了。我不是OP,所以我的测试用例可能是错误的,但它使用OP提供的原始代码工作。(
code=[['W']、['06501']、['06502']、['hi']]