는 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 : 당신이 언급 할 때 "추세"나는 당신이 마지막 1000 (정도) 색상을보고 사람들이 가장있는보고 싶은 것을 의미하는 것 같아요 공유지?
당신이 사용할 수있는 dequeue
(이 목록처럼) 주변의 마지막 항목을 유지하고 그들을 계산하는 카운터를 업데이트 :
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
왜 이것이 목록이어야합니까? 그것은 잘못된 도구입니다 ... –
나는 모든 선택을 저장하는 다양한 솔루션을 보았습니다 - 무엇을 상기 할 수있는 해결책은 목록이 모든 선택을 저장하지 못했던 것입니다 - 단지 가장 인기가 있거나 트 렌딩입니까? 그래서 상점 10 트렌드 색상을 말할 수있었습니다. 그러나 나는 내가 redbull에 꿈꾸고 있을지도 모른다라고 말했다. – spidee
아무 이유없이, 위의 내 의견을 볼 수 있습니다 - 그것은 모든 선택을 저장하는 경우가 아니 었습니다 - 그래서 아마도 추세였습니까? – spidee