简体   繁体   English

如何从列表中删除具有三个重复字母的单词?

[英]How to remove words out of a list that have three repeating letters from a list?

So I have two lists:所以我有两个列表:

list1 = ["sch", "ggg", "bbb", "hello", "bye"]
list2 = ["s","c","h","b","g"]

and the output should look like this: output 应该如下所示:

["hello","bye"]

Assuming you mean consecutive repeating characters - using a regular expression:假设您的意思是连续的重复字符 - 使用正则表达式:

import re

words = ["sch", "ggg", "bbb", "hello", "bye"]
chars = ["s","c","h","b","g"]

pattern = "[{}]{{3,}}".format("".join(chars))

filtered = [word for word in words if not re.search(pattern, word)]

print(filtered)

Output: Output:

['hello', 'bye']
>>> 
list1 = ["sch", "ggg", "bbb", "hello", "bye"]
list2 = ["s","c","h","b","g"]

list3=list1[3:]
print(list3)

Output: ['hello', 'bye'] Output: ['hello', 'bye']

Depending on whether you want to exclude words that have 3 consecutive instances of the letters or simply 3 instances anywhere in the word you can use one of these list comprehensions to filter list1:根据您是要排除具有 3 个连续字母实例的单词还是仅排除单词中任意位置的 3 个实例,您可以使用以下列表推导之一来过滤 list1:

list1 = ["sch", "ggg", "bbb", "hello", "bye","bags"]
list2 = ["s","c","h","b","g"]

# exclude 3 non-consecutive
list3 = [ w for w in list1 if sum(c in list2 for c in w)<3 ]
print(list3) # ['hello', 'bye']

# exclude 3 consecutive
list3 = [ w for w in list1 if "111" not in "".join("01"[c in list2] for c in w) ]
print(list3) # ['hello', 'bye', 'bags']

note: I added "bags" to your example data to illustrate the distinction注意:我在您的示例数据中添加了“包”以说明区别

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从列表中删除字母 - remove letters from a list 如何从单词列表中删除相似的单词? - How to remove similar words from a list of words? 如何忽略字符串列表中只有 1/2 个字母的单词 - How to ignore words that have only 1/2 letters in a list of strings 如何从单词列表到Python中的不同字母列表 - How to go from list of words to a list of distinct letters in Python 如何从字符串列表中删除单词列表 - How to remove list of words from a list of strings 如何从字符串列表中删除单词列表? - How to remove list of words from a list of strings? 如何从5个字母或更少的Python 2.7的单词列表中打印单词? - How to print words from a word list that have 5 letters or less Python 2.7? 如何从列表中删除重复值但使用循环让其中一个列表存在..我已尽力弄清楚但无法 - how to remove repeating value from a list but let reside one of them list using a loop..i have tried my best to figure it out but couldn't 仅使用正则表达式从列表中提取不包含重复字母的单词 - Only extract those words from a list that include no repeating letters, using regex 从列表字母中打印所有可能的长度为 10 的单词的组合,其中重复“A”两次 - Print all possible combination of words of length 10 from a list letters with repeating 'A' exactly twice
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM