469,330 Members | 1,335 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,330 developers. It's quick & easy.

count objects in a list and random numb gen

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

Jul 18 '05 #1
4 2599
Bart Nessux <ba*********@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.
Jul 18 '05 #2
Paul Rubin wrote:
Bart Nessux <ba*********@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


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).

Jul 18 '05 #3
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[i] = alist[i]
adict2[alist[i]] = 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.
adict1 {0: 'one', 1: 'three', 2: 'four'} adict2 {'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.

-- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Bestiaria Home Page: http://www.beastie.dm.net/ <
Home Page: http://www.dm.net/~wulfraed/ <


Jul 18 '05 #4
Bart Nessux <ba*********@hotmail.com> wrote:

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.
--
- Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Jul 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

22 posts views Thread by Ling Lee | last post: by
3 posts views Thread by phil | last post: by
7 posts views Thread by Fred Hedges | last post: by
16 posts views Thread by Kittyhawk | last post: by
26 posts views Thread by Ping | last post: by
9 posts views Thread by smarttechie | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by Purva khokhar | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.