473,385 Members | 1,620 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

set & random.choice question

I want to do something like this:

from random import choice
x = set(("jenny", "jacqui", "claire", "chris", "tracy"))
somebody = random.choice(x)

but I bet a "TypeError: unindexable object" error. Any suggestions for
an elegant workaround?

I'm using set because I want to know that I have a collection of unique
objects.

steve

Dec 14 '05 #1
4 2515
Il 2005-12-14, st***********@yahoo.com <st***********@yahoo.com> ha scritto:
I want to do something like this:

from random import choice
x = set(("jenny", "jacqui", "claire", "chris", "tracy"))
somebody = random.choice(x)


import random
x = set(("jenny", "jacqui", "claire", "chris", "tracy"))
somebody = random.choice(list(x))

You must turn back it into a list, set has no notion of indexing

--
Lawrence - http://www.oluyede.org/blog
"Anyone can freely use whatever he wants but the light at the end
of the tunnel for most of his problems is Python"
Dec 14 '05 #2
Hello,

On 14 Dec 2005 12:16:22 -0800, st***********@yahoo.com wrote:
I want to do something like this:

from random import choice
x = set(("jenny", "jacqui", "claire", "chris", "tracy"))
somebody = random.choice(x)

but I bet a "TypeError: unindexable object" error. Any suggestions for
an elegant workaround?


What about somebody = random.choice(list(x)) ?
Christophe.
Dec 14 '05 #3
st***********@yahoo.com schrieb:
I want to do something like this:

from random import choice
x = set(("jenny", "jacqui", "claire", "chris", "tracy"))
somebody = random.choice(x)

but I bet a "TypeError: unindexable object" error. Any suggestions for
an elegant workaround?

I'm using set because I want to know that I have a collection of unique
objects.

steve


import random

x = set(("jenny", "jacqui", "claire", "chris", "tracy"))
def draw_from_set(a_set):
random_index = random.randint(0, len(x) - 1)

for i, name in enumerate(x):
if i == random_index:
return name

somebody = draw_from_set(x)

print somebody
Bye,
Dennis
Dec 14 '05 #4
Oh duh. :)

Thanks for pointing out the obvious without mocking...

sc

Dec 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Brandon Michael Moore | last post by:
I'm trying to test a web application using a tool written in python. I would like to be able to generate random values to put in fields. I would like to be able to generate random dates (in a...
9
by: Bart Nessux | last post by:
I am using method 'a' below to pick 25 names from a pool of 225. A co-worker is using method 'b' by running it 25 times and throwing out the winning name (names are associated with numbers) after...
21
by: Andreas Lobinger | last post by:
Aloha, i wanted to ask another problem, but as i started to build an example... How to generate (memory and time)-efficient a string containing random characters? I have never worked with...
10
by: Virus | last post by:
Ok well what I am trying to do is have 1.) the background color to change randomly with 5 different colors.(change on page load) 2,) 10 different quotes randomly fadeing in and out in random...
5
by: flamesrock | last post by:
Hi, It's been a while since I've played with python. My question is... whats the best way to pop a random item from a list?? -Thanks
2
by: Brendon Towle | last post by:
I need to simulate scenarios like the following: "You have a deck of 3 orange cards, 5 yellow cards, and 2 blue cards. You draw a card, replace it, and repeat N times." So, I wrote the...
15
by: caca | last post by:
Hello, This is a question for the best method (in terms of performance only) to choose a random element from a list among those that satisfy a certain property. This is the setting: I need to...
4
by: philly_bob | last post by:
In the sample program below, I want to send a random method to a class instance. In other words, I don't know which method to send until run-time. How can I send ch, which is my random choice, to...
2
by: blaine | last post by:
Hey everyone, Just a friendly question about an efficient way to do this. I have a graph with nodes and edges (networkx is am amazing library, check it out!). I also have a lookup table with...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.