Python example - the number of unique lines in a file

Counts the number of lines in the attached file that are different from any other line, that is, the number of unique lines.


【参考代码】

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)))

Remember: if you need "de-duplication" functionality, use a collection type.

ls.remove() can remove an element. If the row is a unique row, it will not appear in the set t after removing the element.

Example source: python123 website

Guess you like

Origin blog.csdn.net/panlan7/article/details/124108413