Python 如何基于给定的元组列表删除列表列表?

Python 如何基于给定的元组列表删除列表列表?,python,python-3.x,pyspark,Python,Python 3.x,Pyspark,我有两个清单如下 l=[['A', 'B', 'C'], ['A', 'C'], ['A', 'B', 'C'], ['A', 'B'],['B','C']] x=[('A', 'B'), ('A', 'C')] 我想删除l中的列表,它不在x中的元组列表中 例如,l['B','c']列表存在,但我们在x中没有(B,c)组合,因此我们需要删除i.e B,c这两个元素在x中没有任何一个元组。 我的预期产出将是: [['A', 'B', 'C'], ['A', 'C'], ['A', 'B', 'C

我有两个清单如下

l=[['A', 'B', 'C'], ['A', 'C'], ['A', 'B', 'C'], ['A', 'B'],['B','C']]
x=[('A', 'B'), ('A', 'C')]
我想删除l中的列表,它不在x中的元组列表中

例如,
l['B','c']
列表存在,但我们在x中没有
(B,c)
组合,因此我们需要删除i.e B,c这两个元素在x中没有任何一个元组。 我的预期产出将是:

[['A', 'B', 'C'], ['A', 'C'], ['A', 'B', 'C'], ['A', 'B']
您可以将条件与和一起使用:

您可以将条件与和一起使用:


使用如下过滤器:

l=[['A', 'B', 'C'], ['A', 'C'], ['A', 'B', 'C'], ['A', 'B'],['B','C']]
x=[('A', 'B'), ('A', 'C')]

l = list(filter(lambda m: any(all(e in m for e in y) for y in x), l))

print(l)

使用如下过滤器:

l=[['A', 'B', 'C'], ['A', 'C'], ['A', 'B', 'C'], ['A', 'B'],['B','C']]
x=[('A', 'B'), ('A', 'C')]

l = list(filter(lambda m: any(all(e in m for e in y) for y in x), l))

print(l)

对我来说,这是一个复杂的单班轮

new_list = []
for i in l:
    # Must use a flag, because we have 2 items that are the same in l (['A', 'B', 'C'])
    # so can't use append if i not new_list
    is_i_added = False
    for z in x:
        if is_i_added:
            continue

        j_not_in_i = False
        for j in z:
            if j not in i:
                j_not_in_i = True

        if not j_not_in_i:
            new_list.append(i)
            is_i_added = True

print(new_list)

对我来说,这是一个复杂的单班轮

new_list = []
for i in l:
    # Must use a flag, because we have 2 items that are the same in l (['A', 'B', 'C'])
    # so can't use append if i not new_list
    is_i_added = False
    for z in x:
        if is_i_added:
            continue

        j_not_in_i = False
        for j in z:
            if j not in i:
                j_not_in_i = True

        if not j_not_in_i:
            new_list.append(i)
            is_i_added = True

print(new_list)

尽管上述所有工作都可以完成,但速度要快得多。 以下是每种溶液10万次运行的测量时间(以秒为单位)

  • 施沃巴塞格:0.80720812
  • 循环的长循环:0.70316080100000001
  • 什洛米兰0.2439321199997
  • Miklos_Horvath 0.683809444(尚未转换回列表)
如上所述,shlomiLan的速度几乎是其他溶液的3倍。 用于获取结果的代码可以在此处看到:

import timeit
setup = """
l = [['A', 'B', 'C'], ['A', 'C'], ['A', 'B', 'C'], ['A', 'B'],['B','C']]
x = [('A', 'B'), ('A', 'C')]"""

schwobaseggl = "[l_ for l_ in l if any(all(e in l_ for e in x_) for x_ in x)]"

long_for_loop = '''
c = []
for l_ in l:
    d = []
    for x_ in x:
        a = []
        for e in x_:
            if e in l_:
                a.append(True)
            else:
                a.append(False)
                break
        if all(a):
            d.append(True)
    if any(d):
        c.append(l_)
'''

shlomiLan = """new_list = []
for i in l:
    # Must use a flag, because we have 2 items that are the same in l (['A', 'B', 'C'])
    # so can't use append if i not new_list
    is_i_added = False
    for z in x:
        if is_i_added:
            continue

        j_not_in_i = False
        for j in z:
            if j not in i:
                j_not_in_i = True

        if not j_not_in_i:
            new_list.append(i)
            is_i_added = True"""

miklos_Horvath = "l = list(filter(lambda m: any(all(e in m for e in y) for y in x), l))"

a = timeit.timeit(setup=setup, stmt=schwobaseggl, number=100000)
b = timeit.timeit(setup=setup, stmt=long_for_loop, number=100000)
c = timeit.timeit(setup=setup, stmt=shlomiLan, number=100000)
d = timeit.timeit(setup=setup, stmt=miklos_Horvath, number=100000)

尽管上述所有工作都可以完成,但速度要快得多。 以下是每种溶液10万次运行的测量时间(以秒为单位)

  • 施沃巴塞格:0.80720812
  • 循环的长循环:0.70316080100000001
  • 什洛米兰0.2439321199997
  • Miklos_Horvath 0.683809444(尚未转换回列表)
如上所述,shlomiLan的速度几乎是其他溶液的3倍。 用于获取结果的代码可以在此处看到:

import timeit
setup = """
l = [['A', 'B', 'C'], ['A', 'C'], ['A', 'B', 'C'], ['A', 'B'],['B','C']]
x = [('A', 'B'), ('A', 'C')]"""

schwobaseggl = "[l_ for l_ in l if any(all(e in l_ for e in x_) for x_ in x)]"

long_for_loop = '''
c = []
for l_ in l:
    d = []
    for x_ in x:
        a = []
        for e in x_:
            if e in l_:
                a.append(True)
            else:
                a.append(False)
                break
        if all(a):
            d.append(True)
    if any(d):
        c.append(l_)
'''

shlomiLan = """new_list = []
for i in l:
    # Must use a flag, because we have 2 items that are the same in l (['A', 'B', 'C'])
    # so can't use append if i not new_list
    is_i_added = False
    for z in x:
        if is_i_added:
            continue

        j_not_in_i = False
        for j in z:
            if j not in i:
                j_not_in_i = True

        if not j_not_in_i:
            new_list.append(i)
            is_i_added = True"""

miklos_Horvath = "l = list(filter(lambda m: any(all(e in m for e in y) for y in x), l))"

a = timeit.timeit(setup=setup, stmt=schwobaseggl, number=100000)
b = timeit.timeit(setup=setup, stmt=long_for_loop, number=100000)
c = timeit.timeit(setup=setup, stmt=shlomiLan, number=100000)
d = timeit.timeit(setup=setup, stmt=miklos_Horvath, number=100000)

你能更明确地说明一下规则吗?例如,
x
既不包含
('B','C')
也不包含
('A','B','C')
,但不知何故,您想删除一个,保留另一个?还有,您迄今为止做了哪些尝试?@MartinFrodl可能是因为
'B'
'C'
都通过
('A','B')
('A','C')与
相关
分别是元组?只是猜测而已..@Ev。科尼斯,你说得对。@Martin Frodl'B'和'C'与'A'通过('A','B')和('A','C')相关。以x元组表示的变量应表示为l。例如,l是['A','B','C'],x是('A','B'),('A','C'),在我们的例子中(A,B)在l中呈现,而(A,C)呈现。至少有一个呈现也很好。但是如果['B','C']在X all变量中没有元组,那么我们需要从我的最终列表中删除,['B','C'],你能更明确地解释一下规则吗?例如,
x
既不包含
('B','C')
也不包含
('A','B','C')
,但不知何故,您想删除一个,保留另一个?还有,您迄今为止做了哪些尝试?@MartinFrodl可能是因为
'B'
'C'
都通过
('A','B')
('A','C')与
相关
分别是元组?只是猜测而已..@Ev。科尼斯,你说得对。@Martin Frodl'B'和'C'与'A'通过('A','B')和('A','C')相关。以x元组表示的变量应表示为l。例如,l是['A','B','C'],x是('A','B'),('A','C'),在我们的例子中(A,B)在l中呈现,而(A,C)呈现。至少有一个呈现也很好。但是在['B','C']的情况下,X all变量中没有元组,所以我们需要删除,['B','C']从我的最终列表。它比SchowBasegl编写的列表压缩略慢,但如果不需要将筛选器转换回列表,它将更快:)它比SchowBasegl编写的列表压缩略慢,但如果不需要将筛选器转换回列表,它将更快:)