count objects in a list and random numb gen

B

Bart Nessux

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?

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

TIA, Bart
 
P

Paul Rubin

Bart Nessux said:
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.
 
B

Bart Nessux

Paul said:
I'm sorry but I just can't understand the above description

I want to count the objects in a list. len works well for this. Once I
have a count, I want to map that count to the items in the list like this:

entry one is 1
entry two is 2
entry three is 3
....

This is why I thought a dictionary may be better suited for this.
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.

I think random.sample is better than you think:

sample( population, k)

Returns a new list containing elements from the population while leaving
the original population unchanged. The resulting list is in selection
order so that all sub-slices will also be *valid random samples*. This
allows raffle winners (the sample) to be partitioned into grand prize
and second place winners (the subslices).
 
D

Dennis Lee Bieber

Bart Nessux fed this fish to the penguins on Thursday 08 January 2004
15:57 pm:
I want to count the objects in a list. len works well for this. Once I
have a count, I want to map that count to the items in the list like
this:

entry one is 1
entry two is 2
entry three is 3
...

Other than the fact that lists are indexed from zero...

alist = [ 'one', 'three', 'four' ] #since "one", "two" etc. is not clear

alist[0] is "one", alist[1] is "three", etc.

Unless you edit the list, that association remains -- the first item
is index 0, etc. You would have to delete something /in/ the list to
change the associations.

If you went with a dictionary, you need to clarify what you intend to
have as the key...

adict1 = {}
adict2 = {}
for i in xrange(len(alist)):
adict1 = alist
adict2[alist] = i

Which do you want, items as keys with the "lookup" the original index,
or index as key with the lookup to the item. NOTE: the first will have
problems in two items are identical ( ['same', 'same'] ) as you have
two indices for the value. The second is just adding a bunch of
overhead to produce the same result as indexing into the list.
{'four': 2, 'three': 1, 'one': 0}
adict1[1] 'three'
alist[1]
'three'
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

Ignoring the randomness of random.sample(), this procedure is doing
the sequence:

Create empty list
append one item to the list
print the one-element LIST

on EVERY call... So why use a list? Just print the item itself.

--
 
T

Tim Roberts

Bart Nessux said:
Also, does this bit of code look to be truely random?

The code doesn't look random at all, but of course that's not really the
question you meant to ask.
def random_number_gen():
winner = []
winner.append(random.sample(xrange(100000), 1))
print winner

It depends entirely on your definition of "truly random". There is no
single definition of that phrase.

However, that specific example provides no benefit over this simpler and
more efficient code:

def random_number_gen():
print int(random.uniform(0,100000))

which is itself just a shortcut for:

def random_number_gen():
print int(random.random()*100000))

What are you using the random numbers for? Tell us what you want to do
with them, and we'll suggest the right method.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top