Search code examples
pythoncounting

Given a list of urls, print out the top 3 frequent filenames


Given a list of urls, print out the top 3 frequent filenames.

url = [
        "http://www.google.com/a.txt",
        "http://www.google.com.tw/a.txt",
        "http://www.google.com/download/c.jpg",
        "http://www.google.co.jp/a.txt",
        "http://www.google.com/b.txt",
        "http://facebook.com/movie/b.txt",
        "http://yahoo.com/123/000/c.jpg",
        "http://gliacloud.com/haha.png",
    ]

The program should print out

a.txt 3  
b.txt 2  
c.jpg 2

Solution

  • How about this with collections.Counterand top 3 with counter.most_common(3)?

    import collections
    url = [
            "http://www.google.com/a.txt",
            "http://www.google.com.tw/a.txt",
            "http://www.google.com/download/c.jpg",
            "http://www.google.co.jp/a.txt",
            "http://www.google.com/b.txt",
            "http://facebook.com/movie/b.txt",
            "http://yahoo.com/123/000/c.jpg",
            "http://gliacloud.com/haha.png",
        ]
    
    splited_url = [i.split('/')[-1] for i in url]
    counter = collections.Counter(splited_url)
    counter = counter.most_common(3)
    for p in counter:
        print('{} {}'.format(p[0], p[1]))
    

    WORKING DEMO: https://rextester.com/EGJX25593