Python-列表的替代品。删除(x)?
我想比较两份清单。通常这不是问题,因为我通常使用嵌套的for循环并将交叉点附加到新列表中。在这种情况下,我需要从A中删除A和B的交点Python-列表的替代品。删除(x)?,python,comparison,list,Python,Comparison,List,我想比较两份清单。通常这不是问题,因为我通常使用嵌套的for循环并将交叉点附加到新列表中。在这种情况下,我需要从A中删除A和B的交点 A = [['ab', 'cd', 'ef', '0', '567'], ['ghy5'], ['pop', 'eye']] B = [['ab'], ['hi'], ['op'], ['ej']] 我的目标是比较A和B,并从A中删除交叉点B,即在本例中删除A[0][0] 我试过: def match(): for i in A:
A = [['ab', 'cd', 'ef', '0', '567'], ['ghy5'], ['pop', 'eye']]
B = [['ab'], ['hi'], ['op'], ['ej']]
我的目标是比较A和B,并从A中删除交叉点B,即在本例中删除A[0][0]
我试过:
def match():
for i in A:
for j in i:
for k in B:
for v in k:
if j == v:
A.remove(j)
list.remove(x)抛出一个ValueError。如果可能的话(意思是如果顺序和您拥有“子列表”的事实不重要),我会首先创建,然后您可以轻松地从a
中删除B
中的元素:
>>> from itertools import chain
>>> A = [['ab', 'cd', 'ef', '0', '567'], ['ghy5'], ['pop', 'eye']]
>>> B = [['ab'], ['hi'], ['op'], ['ej']]
>>> A = set(chain(*A))
>>> B = set(chain(*B))
>>> A-B
set(['ghy5', 'eye', 'ef', 'pop', 'cd', '0', '567'])
或者,如果
A
的顺序和结构很重要,您可以这样做(感谢并感谢):
但请注意:这仅在假设
A
和B
始终是列表的情况下起作用。如果可能(意味着如果顺序和您拥有“子列表”的事实无关紧要),我首先,创建,然后您可以轻松地从A
中删除B
中的元素:
>>> from itertools import chain
>>> A = [['ab', 'cd', 'ef', '0', '567'], ['ghy5'], ['pop', 'eye']]
>>> B = [['ab'], ['hi'], ['op'], ['ej']]
>>> A = set(chain(*A))
>>> B = set(chain(*B))
>>> A-B
set(['ghy5', 'eye', 'ef', 'pop', 'cd', '0', '567'])
或者,如果
A
的顺序和结构很重要,您可以这样做(感谢并感谢):
但请注意:这仅在假设
A
和B
始终是列表的情况下起作用。编辑:要在这种情况下使用remove,您不能直接从列表A中删除j('ab'),因为它是嵌套列表。您必须使用A.remove(['ab'])或A.remove([j])来完成此任务
另一种可能性是pop(int)方法。所以A.pop(索引)实际上也应该起作用
来源:编辑:要在这种情况下使用remove,您不能直接从列表a中删除j('ab'),因为它是一个嵌套列表。您必须使用A.remove(['ab'])或A.remove([j])来完成此任务 另一种可能性是pop(int)方法。所以A.pop(索引)实际上也应该起作用
来源:一种使用集合和itertools的天真方法。您可以根据自己的要求对此进行进一步调整:
#!/usr/bin/env python
a = [['ab', 'cd', 'ef', '0', '567'], ['ghy5'], ['pop', 'eye']]
b = [['ab'], ['hi'], ['op'], ['ej']]
from itertools import chain
# this results in the intersection, here => 'ab'
intersection = set(chain.from_iterable(a)).intersection(
set(chain.from_iterable(b)))
def nested_loop(iterable):
"""
Loop over arbitrary nested lists.
"""
for e in iterable:
if isinstance(e, list):
nested_loop(e)
else:
if e in intersection:
iterable.remove(e)
return iterable
print nested_loop(a)
# =>
# [['cd', 'ef', '0', '567'], ['ghy5'], ['pop', 'eye']]
使用集合和itertools的简单方法。您可以根据自己的要求对此进行进一步调整:
#!/usr/bin/env python
a = [['ab', 'cd', 'ef', '0', '567'], ['ghy5'], ['pop', 'eye']]
b = [['ab'], ['hi'], ['op'], ['ej']]
from itertools import chain
# this results in the intersection, here => 'ab'
intersection = set(chain.from_iterable(a)).intersection(
set(chain.from_iterable(b)))
def nested_loop(iterable):
"""
Loop over arbitrary nested lists.
"""
for e in iterable:
if isinstance(e, list):
nested_loop(e)
else:
if e in intersection:
iterable.remove(e)
return iterable
print nested_loop(a)
# =>
# [['cd', 'ef', '0', '567'], ['ghy5'], ['pop', 'eye']]
@THC4k:如果你想提供你的评论作为答案,我会将我的答案改回原来的答案。不,你的答案很好,我只是“希望”有一个很好的理由列出清单;-)@THC4k:如果你想提供你的评论作为答案,我会把我的答案改回原来的答案。不,你的答案很好,我只是“希望”有一个很好的理由列出这些列表;-)
j
始终是A
中列表的一个元素。它不是索引j
始终是A
中列表的一个元素。它不是一个索引。