如何从列表列表或元组列表中删除包含某些单词的列表?
How to remove lists with certain words from a list of lists or list of tuples?
列表列表
[['b'],['d'],['e'],['b','d','e'],['b','e'],['e','f'],['b','d']]
元组列表
[('b',),('d',),('e'),('b','d','e'),('b','e'),('e','f'),('b','d')]
我想删除同时包含字符 'b' 和 'd' 的任何列表或元组。所以输出应该如下所示。
删除列表 = [['b','d']]
删除元组 = [('b','d')]
修改列表列表
[['b'],['d'],['e'],['b','e'],['e','f']]
修改元组列表
[('b',),('d',),('e'),('b','e'),('e','f')]
最好我想对 lists/tuples 的多个组合执行此操作说 remove lists = [[e,f],[b,d]] 以删除包含字符 e 和 f 的任何列表以及 b 和 d。
我非常感谢可以提供的任何帮助。
我为您制作了一个易于阅读和理解的代码片段,它使用辅助函数从列表列表或元组列表中删除 strings/chars/letters 列表
有问题请评论,如果你知道enumerate方法是做什么的,就上网查查吧。如果您设法理解解决方案,请告诉我
def remove_chars(lists, list_of_chars):
result = []
for idx, list in enumerate(lists):
if all(letter in list for letter in list_of_chars):
pass
else:
result.append(list)
return result
list_of_lists = [['b'],['d'],['e'], ['b','d','e'],['b','e'],['e','f'],['b','d']]
list_of_tuples = [('b',), ('d',), ('e'), ('b', 'd', 'e'), ('b', 'e'), ('e', 'f'), ('b', 'd')]
letters_to_remove = ['b']
filtered_lists = remove_chars(list_of_lists, letters_to_remove)
print(filtered_lists)
filtered_tuples = remove_chars(list_of_tuples, letters_to_remove)
print(filtered_tuples)
# remove list of list of chars:
list_of_list_of_letters = [['b'],['b','d']]
for list_of_letters in list_of_list_of_letters:
filtered_lists = remove_chars(list_of_lists, list_of_letters)
print(filtered_lists)
filtered_tuples = remove_chars(list_of_tuples, list_of_letters)
print(filtered_tuples)
一个简单的列表理解就足够了。适用于列表列表和元组列表。
abc = [['b'],['d'],['e'], ['b','d','e'],['b','e'],['e','f'],['b','d']]
# Characters to remove
rm = [['e','f'],['b','d']]
# Function takes two list of lists as input
def refine_list(mylist, stuff_to_remove):
for to_remove in stuff_to_remove:
mylist = [x for x in mylist if not set(to_remove).issubset(set(x))]
return mylist
print(refine_list(abc, rm))
输出:
[['b'], ['d'], ['e'], ['b', 'e']]
列表列表 [['b'],['d'],['e'],['b','d','e'],['b','e'],['e','f'],['b','d']]
元组列表 [('b',),('d',),('e'),('b','d','e'),('b','e'),('e','f'),('b','d')]
我想删除同时包含字符 'b' 和 'd' 的任何列表或元组。所以输出应该如下所示。
删除列表 = [['b','d']] 删除元组 = [('b','d')]
修改列表列表 [['b'],['d'],['e'],['b','e'],['e','f']]
修改元组列表 [('b',),('d',),('e'),('b','e'),('e','f')]
最好我想对 lists/tuples 的多个组合执行此操作说 remove lists = [[e,f],[b,d]] 以删除包含字符 e 和 f 的任何列表以及 b 和 d。
我非常感谢可以提供的任何帮助。
我为您制作了一个易于阅读和理解的代码片段,它使用辅助函数从列表列表或元组列表中删除 strings/chars/letters 列表
有问题请评论,如果你知道enumerate方法是做什么的,就上网查查吧。如果您设法理解解决方案,请告诉我
def remove_chars(lists, list_of_chars):
result = []
for idx, list in enumerate(lists):
if all(letter in list for letter in list_of_chars):
pass
else:
result.append(list)
return result
list_of_lists = [['b'],['d'],['e'], ['b','d','e'],['b','e'],['e','f'],['b','d']]
list_of_tuples = [('b',), ('d',), ('e'), ('b', 'd', 'e'), ('b', 'e'), ('e', 'f'), ('b', 'd')]
letters_to_remove = ['b']
filtered_lists = remove_chars(list_of_lists, letters_to_remove)
print(filtered_lists)
filtered_tuples = remove_chars(list_of_tuples, letters_to_remove)
print(filtered_tuples)
# remove list of list of chars:
list_of_list_of_letters = [['b'],['b','d']]
for list_of_letters in list_of_list_of_letters:
filtered_lists = remove_chars(list_of_lists, list_of_letters)
print(filtered_lists)
filtered_tuples = remove_chars(list_of_tuples, list_of_letters)
print(filtered_tuples)
一个简单的列表理解就足够了。适用于列表列表和元组列表。
abc = [['b'],['d'],['e'], ['b','d','e'],['b','e'],['e','f'],['b','d']]
# Characters to remove
rm = [['e','f'],['b','d']]
# Function takes two list of lists as input
def refine_list(mylist, stuff_to_remove):
for to_remove in stuff_to_remove:
mylist = [x for x in mylist if not set(to_remove).issubset(set(x))]
return mylist
print(refine_list(abc, rm))
输出:
[['b'], ['d'], ['e'], ['b', 'e']]