我正在尝试创建一个列表过滤器,如果包含重复字母的单词将删除该单词。例如(已删除{aPPle,AAron,iRRational}) 我不确定从哪里开始甚至开始。任何提示将不胜感激。
q = 0
x = 0
j = 0
def filter(original, q, x,j ):
for x in range len(orignal):
for l in original:
for x in l:
for j in l:
if j == x:
input_string = input("Enter names that are separated by space: ")
original = input_string.split()
print("original list:", original)
filter(original, q, x,j)
restricted = {"aa", "bb", "cc", "dd", "ee", "ff", "gg", "hh", "ii", "jj",
"kk", "ll", "mm", "nn", "oo", "pp", "qq", "rr", "ss", "tt", "uu", "vv",
"ww", "xx", "yy", "zz"}
if original == restricted:
#remove???
答案 0 :(得分:1)
您可以使用any
来检测相邻的相等字母,然后在列表理解中过滤掉
>>> words = ['apple', 'bob', 'aaron', 'foo', 'bar']
>>> [word for word in words if not any(i == j for i,j in zip(word[:-1], word[1:]))]
['bob', 'bar']
答案 1 :(得分:0)
您可以使用itertools.groupby
来检查是否有任何相邻的char
重复出现,
>>> import itertools
>>> words
['apple', 'bob', 'aaron', 'foo', 'bar']
>>> for word in words:
... x = itertools.groupby(word)
... if not any(len(tuple(v)) > 1 for k,v in x):
... print(word)
...
bob
bar