[英]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.