我有一个像这样的程序创建列表:
["abc a","hello","abc","hello z"]
我的目标是移入列表,如果该元素包含在字符串之一中,则删除该字符串
第一次迭代:
abc a can't be found in any other element
["abc a","hello","abc","hello z"]
第二个:
hello is present in element 4:
["abc a","hello","abc"]
第三名:
abc can be found in element one:
["hello","abc"]
我尝试使用filter()函数没有成功
我希望每个元素都传递给函数,唯一的问题是列表大小在减小,因此这是我不知道如何处理的另一个问题
谢谢
答案 0 :(得分:1)
您首先可以做的是,当您获得列表时,以这种方式进行创建[[element1,status],[element2,status]]。状态将在此处显示或删除。 最初,所有状态都将存在,并且在遍历而不是删除/删除元素时,只需将状态更新为Deleted,并且在每次迭代中,如果找到匹配项,则仅考虑其状态是否存在,这样您的列表大小保持原样。最后,仅选择那些具有状态的元素。希望你能得到它。
init_list = ["abc a","hello","abc","hello z"]
new_list = list()
for i in init_list:
new_list.append([i,"present"])
for i in new_list:
if i[1] == 'present':
for j in new_list:
if j[1] == 'present' and not i == j:
fin = j[0].find(i[0])
if not fin == -1:
j[1] = 'deleted'
fin_list = list()
for i in new_list:
if i[1] == 'present':
fin_list.append(i[0])
print(fin_list)
答案 1 :(得分:1)
一种方法是:
像这样:
lst = ["abc a","hello","abc","hello z"]
words = sorted([set(x.split()) for x in lst],key=len)
result = []
for l in words:
if not result or all(l.isdisjoint(x) for x in result):
result.append(l)
print(result)
打印集合列表:
[{'hello'}, {'abc'}]
此方法会丢失单词顺序,但不会出现单词定界符问题。子字符串方法如下所示:
lst = ["abc a","hello","abc","hello z"]
words = sorted(lst,key=len)
result = []
for l in words:
if not result or all(x not in l for x in result):
result.append(l)
print(result)
打印:
['abc', 'hello']
(这种方法在使用单词定界符时可能会出现问题,但是可以使用此处的all
轻松调整split
条件,以避免这种情况)。例如这样的条件:
if not result or all(set(x.split()).isdisjoint(l.split()) for x in result):
将转弯:
lst = ["abc a","hello","abc","abcd","hello z"]
进入
['abc', 'abcd', 'hello']