我需要检查文件是否有重复的行?
结果应为“ true”或“ false”
with open('user_data_dump') as f:
seen = set()
for line in f:
line_lower = line.lower()
if line_lower in seen:
print('true')
else:
print('false')
答案 0 :(得分:1)
如果只想打印一次,则还需要更改循环的行为:MessageModel.create()
找到一个重复项后,最后才打印False。
break
答案 1 :(得分:0)
尝试一下:
def func(filename):
with open(filename) as f:
seen = set()
for line in f:
line_lower = line.lower()
if line_lower in seen:
return True
else:
seen.add(line_lower)
return False
答案 2 :(得分:0)
您需要添加在集合seen
中已经看到的项目,并且第一次遇到重复值时就中断循环
def check_dups():
#Flag to track if duplicate lines are seen or not
has_dups = False
with open('user_data_dump') as f:
seen = set()
for line in f:
line_lower = line.lower()
#If a duplicate line is encounter, break the loop
if line_lower in seen:
has_dups = True
break
#Else add the line to set
else:
seen.add(line_lower)
return has_dups