python - Using count() to count the amount of times something appears in a list -


well, firstly, hi there. don't believe has appeared yet, correct me if has.

i creating dice rolling simulator returns frequency of numbers rolled numbers rolled. can input amount of sides , amount of rolls. starter project, i've been told. i'm using count() count amount of times each number appears on list.

that's gets bit off.the highest value in list has 1 less should, example:

you have 6 sided die, , roll 6 times. 1, 2, 3, 4, 5 , 6. when displays occurrences display highest value 1. says 6 appears 0 times.

here's code slab:

    a.sort()     topvalue = a.pop()     while topvalue >= 0:         y = a.count(topvalue)         print topvalue, "appears", y< "times."         topvalue = topvalue-1         if topvalue == -1:             break 

going example, due way coded, if 6 didn't appear, wouldn't printed, is. hope , python wisdom can help!

to last element here, instead of:

topvalue = a.pop() 

do:

topvalue = a[-1]  # represents last index. 

this because pop() removes last element, hence one-off error.


although idea, in position do:

from collections import counter  amounts = counter(a) item, num in amounts.items():     print('{0} occurred {1} times'.format(item, num)) 

Comments

Popular posts from this blog

javascript - jquery or ashx not working -

opencv - DataType<cv::detail::deriv_type>::depth what is it used for -

python 3.x - Mapping specific letters onto a list of words -