【发布时间】:2021-01-25 22:36:42
【问题描述】:
如果元素在另一个元素中,我需要删除它
def c_counter_check(new_filters): #new filters is an list
for i in new_filters_splited[:]:
index = new_filters_splited.index(i) #get index of an i
mylist = new_filters_splited[:index] + new_filters_splited[index + 1:] # create a compare list without i
for a in mylist[:]:
if a.__contains__(i): #check if an element from mylist contains an i
new_filters_splited.remove(i) #if yes than remove i from new_filters_splited
return new_filters_splited
如果我的列表是['a', 'ab', 'ac', 'ab', 'abac'],我需要删除'a' and 'ab' and 'abac' and 'ac',结果应该只是['a']
【问题讨论】:
-
您对
['a', 'ab', 'ac', 'ab']的输入是['a', 'ab']吗?使用输入从列表中删除元素? -
如果
ab在查找列表中,您是要从源列表中仅删除第一次出现的ab还是删除所有出现的ab? -
请重复how to ask 和intro tour。您的描述不清楚:您没有指定任务。您没有指定当前的问题。
-
请提供预期的minimal, reproducible example。显示中间结果与您的预期不同的地方。我们应该能够复制和粘贴您的代码的连续块,执行该文件,并重现您的问题以及跟踪问题点的输出。这让我们可以根据您的测试数据和所需的输出来测试我们的建议。
-
@JoeFerndz 只是其中之一
标签: python list loops for-loop nested-loops