python例题——文件独特行数

统计附件文件中与其他任何其他行都不同的行的数量,即独特行的数量。


【参考代码】

f = open("latex.log")
ls = f.readlines()
s = set(ls)
for i in s:
    ls.remove(i)
t = set(ls)
print("共{}独特行".format(len(s)-len(t)))

记住:如果需要"去重"功能,请使用集合类型。

ls.remove()可以去掉某一个元素,如果该行是独特行,去掉该元素后将不在集合t中出现。

例题来源:python123网站

猜你喜欢

转载自blog.csdn.net/panlan7/article/details/124108413