开发者

python list - storing the most popular color

开发者 https://www.devze.com 2023-02-03 11:24 出处:网络
okay say I want to know what the most popular color is can I do it using a list popular.append(\"red\")

okay say I want to know what the most popular color is can I do it using a list

popular.append("red")
popular.append("blue")
popular.append("green")
popular.append("red")
popular.append("yellow")
popular.append("red")
popular.append("blue")
popular.append("red")
popular.append("yellow")

what I want is

red,blue,yellow,green

is there a neat way that this can be done using a Python list - I did seem to recall I saw a post on the web about the list and all the cool things it can be used for - this I recall was one of them.

Lets say i want to store开发者_JS百科 a users most popular pages visit on my site - say the top 5 most visited pages - could i do it with a list or dictionary - and would that be a reasonable approach?


Lets start with the right way:

popular = ['red', 'blue', 'green', 'red', 'yellow', 
           'red', 'blue', 'red', 'yellow']

from collections import Counter
c = Counter(popular) 
# lists the elements and how often they appear
print c.most_common() 
# -> [('red', 4), ('blue', 2), ('yellow', 2), ('green', 1)]

@spidee: When you mention "trending" I guess you mean that you want to look at the last 1000 (or so) colors and see which ones are the most common?

You can use a dequeue (it's like a list) to keep the last items around and update a Counter to count them:

from collections import Counter, deque

def trending(seq, window=1000, n=5):
    """ For every item in `seq`, this yields the `n` most common elements. 
        Only the last `window` elements are stored and counted """
    c = Counter()
    q = deque()
    it = iter(seq)

    # first iterate `window` times:
    for _ in xrange(window):
        item = next(it) # get a item
        c[item]+=1 # count it 
        q.append(item) # store it
        yield c.most_common(n) # give the current counter

    # for all the other items:
    for item in it:
        drop = q.popleft() # remove the oldest item from the store
        c[drop] -=1
        if c[drop]==0:
            # remove it from the counter to save space
            del c[drop]

        # count, store, yield as above
        c[item] +=1  
        q.append(item)
        yield c.most_common(n)


for trend in trending(popular, 5, 3):
    print trend


You can use the Counter class to get information about the number of occurrences in a list.

If you are building the list yourself, instead of already having the list containing the data, you can just use a Dictionary and increment the value with each color being the key.

More detail based on your edit:
The approach you choose depends on what your data model looks like.

If your site statistics are bring handled by some third party module, it may only provide an api that returns a list of site visits for a given user. Since the starting point is a list, it makes sense to just feed it to Counter and then pull the top values from there.

However, if you are keeping a persistent store of this data yourself, it makes sense to just feed the values straight into a dictionary (page is the key, visit count is the value). This way you can quickly access the visit count for each page and find which pages are in the top five with just one iteration over the key-value pairs.


if you're using python < 2.7 which don't have collections.Counter you can do :

>>> popular = ['red', 'green', 'blue', 'red', 'red', 'blue']
>>> sorted(set(popular), key=lambda color: popular.count(color), reverse=True)
['red', 'blue', 'green']


list.count(x) will give you the number of times that x appears in the list: Python Docs

From that ordering things is quite easy.

0

精彩评论

暂无评论...
验证码 换一张
取 消