4 回答

TA貢獻(xiàn)1804條經(jīng)驗 獲得超2個贊
無需顯式創(chuàng)建bad_words列表,您repeater也可以將其設(shè)置為變量
repeater = 3
newlist = []
with open('input.txt') as f:
x = f.readlines()
for val in x:
word = val.split('\n')[0]
flag = True
for letter in word:
if letter.upper() * repeater in word:
flag = False
break
if flag:
newlist.append(word)
newlist = list(set(newlist))
with open('output.txt', mode='w', encoding='utf-8') as newfile:
for value in newlist:
newfile.writelines(value+"\n")

TA貢獻(xiàn)1811條經(jīng)驗 獲得超5個贊
您可以創(chuàng)建一個函數(shù)來檢查某個字符是否出現(xiàn)超過 3 次,然后在代碼中調(diào)用它:
def letter_count(str):
counts = dict()
for l in str:
if l in counts:
counts[l] += 1
else:
counts[l] = 1
counts[max(counts, key=lambda x : counts[x])]
return counts[max(counts, key=lambda x : counts[x])] > 3
并在您的代碼中這樣調(diào)用它:
with open('7.csv') as oldfile, open('new7.csv', 'w') as newfile:
for line in oldfile:
if if(letter_count(line)):
newfile.write(line)

TA貢獻(xiàn)1824條經(jīng)驗 獲得超6個贊
您可以使用 aCounter
檢查每行中不同字母的頻率,然后僅在它們未通過閾值時才寫入此行:
from collections import Counter
threshold = 3
with open('7.csv') as oldfile, open('new7.csv', 'w') as newfile:
? ? for line in oldfile:
? ? ? ? counts = Counter(line)
? ? ? ? if all(count < threshold for count in counts.values()):
? ? ? ? ? ? newfile.write(line)
這使用該all()
函數(shù)來確保沒有字母超過閾值。

TA貢獻(xiàn)1963條經(jīng)驗 獲得超6個贊
使用單個字符而不是三元組和 的列表string.count()。制作一個小函數(shù)來封裝過濾邏輯可能也是一個不錯的選擇。
def f(line, chars, limit):
for char in chars:
if line.count(char) > limit:
return False
return True
bad_chars = ['A','B', ...]
with open('7.csv', 'r') as oldfile, open('new7.csv', 'w') as newfile:
for line in oldfile:
if f(line, bad_chars, 3):
newfile.write(line)
添加回答
舉報