Python statistics array element occurrences and sorting

Source: Internet
Author: User
Tags in python

The final completion of my world phase, joined the statistics hot up Main module: Statistics all the video of up to the number of times, get the top 10 of the up master.

The main process is:

All the author of the database are put into an array and counted;

The number of times to order, of course, the corresponding up owners also need to follow, so take into account the use of dictionaries;

Gets the top 10 owners.

In the process I have been helped by:

How many times a duplicate item appears in the Python statistics list _python_ script House http://www.jb51.net/article/53911.htm

Python deletes the elements in the list https://www.douban.com/note/277146395/

Dictionary sorting, list sorting in Python-Duhaizhang's personal page https://my.oschina.net/duhaizhang/blog/71123

Statistics Count I finally wrote it myself, and I didn't use the built-in functions, because I needed a dictionary to sort it, so the built-in functions could be understood.

In fact, it is not difficult to write their own, the main grasp of the changes in the subscript and the length of changes can be.

From collections import Counter

str = [' Tom ', ' Sim ', ' Jack ', ' Tom ', ' Sleep ', ' we ', ' Tom ', ' Tom ', ' Sim ', ' we ', ' Tom ']
  dic = Counter (str)

for I in Dic.iteritems ():
    print i[0], i[1]

count = 1
l = len (str)-1
x = {}
t = 0 while

T-<= l:
    th = str[t]
    i = t + 1 while
    i <= l:
        if th = = Str[i]:
            count + 1
            Del Str[i]
            i-= 1
            L-= 1
        i = 1
    x[th] = count
    count = 1
    t = 1

print x
y = sorted ( X.iteritems (), Key=lambda x:x[1], reverse=true)
print y

three = (y[0], y[1], y[2] for
T in three:
    PR int t[0]

item = x.items ()
item.sort () for
k,v in item:
    print K,v
Run Result:

We 2
Sleep 1
Jack 1.
Sim 2
Tom 5
{' We ': 2, ' Sleep ': 1, ' Jack ': 1, ' Sim ': 2, ' Tom ': 5}
[(' Tom ', 5], (' We ', 2), (' Sim ', 2), (' Sleep ', 1), (' Jack ', 1)]
Tom
We
Sim
Jack 1.
Sim 2
Sleep 1
Tom 5
We 2

The above code is the process that I solve the statistics times.

And when the concrete application to the actual module there, there are two more steps, here is not to put the code:

Change STR to data obtained from the database

Finally, only the first 10 elements of the dictionary are needed.

Can.


---------------August 8, 17 update---------------

Words not much, a few days ago wrote a blog

I think I can--python the new http://blog.csdn.net/zy_dream/article/details/76571974

Give yourself a look in your eyes

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.