Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/352.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_List_Loops - Fatal编程技术网

Python 列表理解中的嵌套循环

Python 列表理解中的嵌套循环,python,list,loops,Python,List,Loops,我在l中有一个单词列表,如果在l2中每个元组的第一个索引中存在任何单词,则删除整个元组 我的代码: l = ['hi', 'thanks', 'thank', 'bye', 'ok', 'yes', 'okay'] l2 = [('hi how are u', 'doing great'), ('looking for me', 'please hold')] l3 = [k for k in l2 if not any(i in k[0] for i in l) ] 不知何故,代码不起作用,

我在l中有一个单词列表,如果在l2中每个元组的第一个索引中存在任何单词,则删除整个元组

我的代码:

l = ['hi', 'thanks', 'thank', 'bye', 'ok', 'yes', 'okay']
l2 = [('hi how are u', 'doing great'), ('looking for me', 'please hold')]
l3 = [k for k in l2 if not any(i in k[0] for i in l) ]
不知何故,代码不起作用,我得到了一个空的l3列表

我想要

l3 = [('looking for me', 'please hold')]
拆分k[0]以获取单词列表:

[k for k in l2 if not any(i in k[0].split() for i in l)]
这样,它会检查我是否与一个单词完全匹配

也可以将其解释为如果k[0]不以l中的任何一个开头,则可以执行以下操作:

[k for k in l2 if not k[0].startswith(tuple(l))]
拆分k[0]以获取单词列表:

[k for k in l2 if not any(i in k[0].split() for i in l)]
这样,它会检查我是否与一个单词完全匹配

也可以将其解释为如果k[0]不以l中的任何一个开头,则可以执行以下操作:

[k for k in l2 if not k[0].startswith(tuple(l))]

集合使成员资格测试变得容易。使用函数筛选列表

import operator
first = operator.itemgetter(0

l = ['hi', 'thanks', 'thank', 'bye', 'ok', 'yes', 'okay']
l2 = [('hi how are u', 'doing great'), ('looking for me', 'please hold')]

def foo(t, l = set(l)):
    t = set(first(t).split())
    return bool(l & t)

l3 = [thing for thing in l2 if not foo(thing)]

集合使成员资格测试变得容易。使用函数筛选列表

import operator
first = operator.itemgetter(0

l = ['hi', 'thanks', 'thank', 'bye', 'ok', 'yes', 'okay']
l2 = [('hi how are u', 'doing great'), ('looking for me', 'please hold')]

def foo(t, l = set(l)):
    t = set(first(t).split())
    return bool(l & t)

l3 = [thing for thing in l2 if not foo(thing)]
你在找我吗