Python3 Development question (counter in collections) 6.7

Source: Internet
Author: User
Tags python script

" " write a Python script, analyze the Xx.log file, count the number of visits by domain name Xx.log file contents as follows: Https://www.sogo.com/ale.htmlhttps://www.qq.com/3asd.htmlhttps ://www.sogo.com/teoans.htmlhttps://www.bilibili.com/2https://www.sogo.com/asd_sa.htmlhttps://y.qq.com/https:// www.bilibili.com/1https://dig.chouti.com/https://www.bilibili.com/imd.htmlhttps://www.bilibili.com/Output: 4 Www.bilibili.com3 www.sogo.com1 www.qq.com1 y.qq.com1 dig.chouti.com" '

First we get the topic for demand analysis:

1, first get the data is the domain name

Get the data we can use the regular, or the domain name or the same point can be split

2. Count the number of domain name visits

Can be counted using Python's built-in modules,

3, then the output required format

Sorted built-in functions for sorting

Then start the most relaxed live, start the code word:

#The first wayImportRe fromCollectionsImportCounterwith Open ("Xx.log","R", encoding="Utf-8") as F:data=F.read () Res=re.findall (R"https://(. *?) /.*?", data) dic=Counter (res) RET=sorted (Dic.items (), key=LambdaX:x[1],reverse=True) forKvinchret:Print(v,k)#The second wayDic={}with open ("Xx.log","R", encoding="Utf-8") as F: forLineinchF:line=line.split ("/") [2]        ifLine not inchDic:data[line]=1Else: Data[line]+=1ret=sorted (Data.items (), key=LambdaX:x[1],reverse=True) forKvinchret:Print(v,k)

This topic tests these knowledge points, re module, anonymous function, built-in function sorted,collections in the counter

These can be found in the basic article of the corresponding blog,

Let's just say counter in collections.

We open the source code directly

The purpose of the counter class is to track the number of occurrences of a value. It is an unordered container type, stored in the form of a dictionary key-value pair, where the element is counted as the key and its count as value. The count value can be any Interger (including 0 and negative numbers)

Then look at the source of the use of the method:

>>> C = Counter (' Abcdeabcdabcaba ') # count elements from a string generation count object

>>> C.most_common (3) # Three most common elements here's 3 is looking for 3 most common elements
[(' A ', 5), (' B ', 4), (' C ', 3)]

>>> C.most_common (4) Here's 4 is looking for 4 most common elements
[(' A ', 5), (' B ', 4), (' C ', 3), (' d ', 2)]

>>> Sorted (c) # List all unique elements list of all distinct elements
[' A ', ' B ', ' C ', ' d ', ' e ']
>>> ". Join (Sorted (c.elements ())) # list elements with repetitions
' Aaaaabbbbcccdde '

The elements here don't know what it is? Then continue to see the source code:

def elements (self):
"' Iterator over elements repeating" as many times as its count.

Iterators traverse elements with the same number of repetitions per iteration


>>> sum (C.values ()) # Total of all counts count sum
15

>>> c[' A '] # Count of letters ' a ' letter ' a '

5


>>> for Elem in ' Shazam ': # update counts from an iterable updated iteration count in new iteration object
... c[elem] + = 1 # By adding 1 to each element ' s count increased by 1 in each count of elements
>>> c[' A '] # now there is seven ' a ' to see the count of ' a ', plus the top 2, a total of 7 "a"
7
>>> del c[' B '] # remove all ' B ' to delete all ' B ' counts
>>> c[' B '] # now there is zero ' B '
0

>>> d = Counter (' Simsalabim ') # make another Counter
>>> C.update (d) # Add in the second counter added in the second counter

>>> c[' A '] # now there is nine ' a '
9

>>> c.clear () # Empty the counter Qingg
>>> C
Counter ()

Note:if a count is set to zero or reduced to zero, it'll remain
In the counter until the entry are deleted or the counter is cleared:

If the count is set to zero or reduced to zero, it will remain unchanged

In the counter until the entry is deleted or the counter is cleared:


>>> C = Counter (' AAABBC ')
>>> c[' B ']-= 2 # Reduce the count of ' B ' by
>>> C.most_common () # ' B ' is still in, but it's count is zero
[(' A ', 3), (' C ', 1), (' B ', 0)]

About these several usages: we can explore the source code

Python3 Development question (counter in collections) 6.7

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.