How does python calculate the number of repeated lines in a text file?

Source: Internet
Author: User
This article describes how to count the number of repeated rows in a text in python, which involves the use of dict objects in Python and related operations in this article, for more information, see the following example. Share it with you for your reference. The specific implementation method is as follows:

For example, the following file
2
3
1
2
We expect
2, 2
3, 1
1, 1

Solution:

The text that appears is used as the key, the number that appears is used as the value, and then output after the value is excluded
It is best to output data from large to small according to the value. for details, refer:

The code is as follows:

In recent Python 2.7, we have new OrderedDict type, which remembers the order in which the items were added.
>>> D = {"third": 3, "first": 1, "fourth": 4, "second": 2}
>>> For k, v in d. items ():
... Print "% s: % s" % (k, v)
...
Second: 2
Fourth: 4
Third: 3
First: 1
>>> D
{'Second': 2, 'fourthths': 4, 'third': 3, 'first': 1} To make a new ordered dictionary from the original, sorting by the values:
>>> From collections import OrderedDict
>>> D_sorted_by_value = OrderedDict (sorted (d. items (), key = lambda x: x [1]) The OrderedDict behaves like a normal dict:
>>> For k, v in d_sorted_by_value.items ():
... Print "% s: % s" % (k, v)
...
First: 1
Second: 2
Third: 3
Fourth: 4
>>> D_sorted_by_value
OrderedDict ([('first': 1), ('second': 2), ('third': 3), ('fourthth': 4)])


The code is as follows:

The code is as follows:

# Coding = UTF-8
Import operator
F = open ("f.txt ")
Count_dict = {}
For line in f. readlines ():
Line = line. strip ()
Count = count_dict.setdefault (line, 0)
Count + = 1
Count_dict [line] = count
Sorted_count_dict = sorted (count_dict.iteritems (), key = operator. itemgetter (1), reverse = True)
For item in sorted_count_dict:
Print "% s, % d" % (item [0], item [1])

Note:
1. two methods for python dict objects:

The items method returns all Dictionary items as a list. Each of these list items comes from (key, value)
The iteritems method works roughly the same as the items method, but returns an iterator object instead of a list.

2. python built-in function sorted

The code is as follows:

>>> Help (sorted)
Help on built-in function sorted in module _ builtin __:
Sorted (...)
Sorted (iterable, cmp = None, key = None, reverse = False) --> new sorted list

I hope this article will help you with Python programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.