The number of times repeated items appear in the Python statistics list.
This example shows how many times repeated items appear in the Python statistics list. It is a very practical function and is suitable for beginners of Python to learn from. The specific method is as follows:
For a list, such as [1, 2, 2, 2, 3, 3, 4, 4], we need to count the repeated items in this list and count the repeated items several times.
Method 1:
Mylist = [,] myset = set (mylist) # myset is another list, the content in mylist contains no repeated items for item in myset: print ("the % d has found % d" % (item, mylist. count (item )))
Method 2:
List=[1,2,2,2,2,3,3,3,4,4,4,4]a = {}for i in List: if List.count(i)>1: a[i] = List.count(i)print (a)
Use the features of the dictionary.
Method 3:
>>> from collections import Counter>>> Counter([1,2,2,2,2,3,3,3,4,4,4,4])Counter({1: 5, 2: 3, 3: 2})
Here we will add a method that only uses the List Implementation:
l=[1,4,2,4,2,2,5,2,6,3,3,6,3,6,6,3,3,3,7,8,9,8,7,0,7,1,2,4,7,8,9]count_times = []for i in l : count_times.append(l.count(i))m = max(count_times)n = l.index(m)print (l[n])
The implementation principle is to record the number of occurrences of each number in the list at its corresponding position, and then use max to find the location with the most occurrences.
If you only use this code, there is one drawback. If there are multiple results, the final realistic results will only appear on the far left, but the solution is also very simple.
Interested readers can practice the code described in this article, and improve the deficiencies to improve the functions.
How does python count the number of repeated rows in a txt file?
F_test1_open('test.txt ', 'R ')
Li = f_test.readlines ()
Special_dna = 'dna = 334455 '# the row to be counted
Count = 0 # count
For I in li:
If I. strip ('\ n') = special_dna:
Count = count + 1
Print (count)
How does python remove duplicate rows and calculate the number of repeated rows separately? Existing Code to remove duplicates
Python has low speed and efficiency. If you have high speed requirements, we recommend that you use c for writing.
I wrote this code, and python2.6 + windows xp passed the test.
Hope to help you ~
========================================================== ==============
A = [] # list a for initialization, used to record original row Information
B = [] # List B to be used for initialization. It is used to record result data, which consists of two items. The first item is the row information such as "James: 90", and the last item is the number of occurrences of the row, as shown in figure 2.
F1 = file ("1.txt"," r ") Open the 1.txt file
For line in f1:
A. append (line) uploads each line of the 1.txt file as an element and stores it in list.
F1.close
For n in a: each item in a is recorded as nbytes, that is, every line in 1.txt.
Flag = 1
For I in range (0, len (B )):
If n = B [I] [0]: # if n is equal to each item in List B:
B [I] [1] = B [I] [1] + 1 # then the corresponding count plus 1
Flag = 0
Break
If flag = 1: # if none of the preceding comparisons are equal, this row appears for the first time:
B. append ([n, 1]) # Add a new entry in list B.
F2 = file ("2.txt"," w ") Open the 2.txt file for output
For n in B: # output format: number of occurrences of Line Information (tab) (Press ENTER)
F2.write (str (n [0] [0:-1]) + "\ t ")
F2.write (str (n [1]) + "\ n ")
F2.close
Print "Finished" # complete