[英]Python: how to replace an element in a list with all elements from another list?
[英]How do I remove all elements from a list which is a subsequence of another bigger element in the same list in python?
我有一个列表列表,如下所示:
[['rest'],['look'],['rest','look'],['resting','look],['apple','mango'],['apple','man' ],['apple','banana','mango'],['rest','resting','look','looked','it','spit']]
必须删除属于另一个元素的子串/子序列的所有元素。 例如元素['rest']
和['look']
已经存在于列表元素['rest','look']
和['rest','resting','look','looked','it','spit']
,因此必须将它们从最终列表中删除。 此外,元素['rest','look']
是 ['rest','resting','look','looked','it','spit'] 的子序列, so it should be removed. Similarly,
, so it should be removed. Similarly,
['resting','look'] is a substring of
,因此它也必须被删除。元素 ['apple','mango'] 因为它是 ['apple','banana','mango'] 的子序列,所以应该删除,但是 ['apple','man'] 不应该删除,因为它不是一个公共子序列。output 必须是列表而不是集合。
我试过这个:
x = [['rest'],['look'],['rest','look'],['resting','look'],['apple','mango'],['apple','man'],['apple','banana','mango'],['rest', 'resting', 'look', 'looked', 'it', 'spit']]
res=[]
found=0
for i in x:
for item in i:
for j in x:
for item1 in j:
if item == item1:
found=1
if found==0:
res.append(item)
print res
我得到的 output 是一个空列表。 所需的 output 是:
[['apple','man'],['apple','banana','mango'],['rest','resting','look','looked','it','spit']]
您可以通过将列表展平为一个集合来使用集合推导式。
x = [['rest'],['look'],['rest','look'],['resting','look'],['rest', 'resting', 'look', 'looked', 'it', 'spit']]
In [2]: results = {s_ for s in x for s_ in s}
In [2]: results
Out[3]: {'it', 'look', 'looked', 'rest', 'resting', 'spit'}
我认为这就是您尝试实施的内容。 我不认为你需要found
变量,你可以只检查子列表中的每个项目(例如['rest', 'look']
已经append编辑到res
。
x = [['rest'], ['look'], ['rest', 'look'], ['resting', 'look'], ['rest', 'resting', 'look', 'looked', 'it', 'spit']]
res = []
for L in x:
for item in L:
if item not in res:
res.append(item)
print(res)
# ['rest', 'look', 'resting', 'looked', 'it', 'spit']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.