count objects in a list and random numb gen

Paul Rubin http
Thu Jan 8 18:31:46 EST 2004


Bart Nessux <bart_nessux at hotmail.com> writes:
> New to Python... trying to figure out how to count the objects in a
> list and then map the count to the objects or convert the list to a
> dict... I think the latter would be better as I need a number
> associated with each entry. Any pointers?

I'm sorry but I just can't understand the above description.  To count
the objects in a list L, use len(L).  To find the k'th element of L,
list, use L[k].  

> Also, does this bit of code look to be truely random?
> 
> def random_number_gen():
>     winner = []	
>     winner.append(random.sample(xrange(100000), 1))	
>     print winner	

If you want to choose one random winner out of a list, you can say
print random.randint(100000).  

Note that Python's random function doesn't try to be really seriously
random, but only to have reasonable statistical properties.  If you
need random numbers that can stand up to an adversary (e.g. you're
using it in an online game where the winners get substantial prizes),
you shouldn't generate them with the random module.



More information about the Python-list mailing list