在Python中,可以通过以下方法避免对文本文件进行重复输入:
lines = set()
with open('file.txt', 'r') as file:
for line in file:
lines.add(line.strip())
lines = {}
with open('file.txt', 'r') as file:
for line in file:
lines[line.strip()] = None
with open('file.txt', 'r') as file:
lines = list({line.strip() for line in file})
无论使用哪种方法,最终得到的lines变量将只包含不重复的文本行。这样可以避免在Python中对文本文件进行重复输入。
领取专属 10元无门槛券
手把手带您无忧上云