473,804 Members | 2,986 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Creating unique combinations from lists

I have three lists... for instance

a = ['big', 'small', 'medium'];
b = ['old', 'new'];
c = ['blue', 'green'];

I want to take those and end up with all of the combinations they
create like the following lists
['big', 'old', 'blue']
['small', 'old', 'blue']
['medium', 'old', 'blue']
['big', 'old', 'green']
['small', 'old', 'green']
['medium', 'small', 'green']
['big', 'new', 'blue']
['small', 'new', 'blue']
['medium', 'new', 'blue']
['big', 'new', 'green']
['small', 'new', 'green']
['medium', 'new', 'green' ]

I could do nested for ... in loops, but was looking for a Pythonic way
to do this. Ideas?
Jan 16 '08
11 7182
The main emphasis was to show that there was a pattern unfolding that
should have been translated into more pythonic code than just
hard-coding nested loops.
Practicality beats purity. That you would solve a more general problem
in a more general way doesn't mean that you shouldn't solve the more
specific problem (combinations from three sets) in a specific,
easy-to-read way. Readability counts.

I find your solution (with nested generators) *very* unpythonic. It
is much more complicated than necessary to solve the problem at hand,
and it doesn't get Pythonic just by using the latest language features.
It may be a smart solution, but not a Pythonic one.

Regards,
Martin

P.S. To solve the general problem, I like

http://aspn.activestate.com/ASPN/Coo.../Recipe/496807
Jan 17 '08 #11
On Thu, 17 Jan 2008 10:44:51 -0600, Reedick, Andrew wrote:
>-----Original Message-----
From: Tim Chase [mailto:py****** ***@tim.thechas es.com] Sent: Thursday,
January 17, 2008 10:30 AM To: Reedick, Andrew
Cc: breal; py*********@pyt hon.org; ma****@v.loewis .de Subject: Re:
Creating unique combinations from lists

Yick...a nice demo of the power of eval, but definitely filed under the
"Hack" heading

You hurt my feeling. *sniffle* Given how late python
compiles/evaluates code blocks, I'm thinking that eval() is less hack
and more paradigm ..err.. pythonic. ;-)
I see your smiley, but even so, do you have any idea how many times eval
is used in the standard library? Not very often.

$ pwd
/usr/lib/python2.5
$ grep -r "eval(.*)" *.py | wc -l
20

Some of those twenty matches are false positives. I manually inspected
them, and by my count there are just ten actual uses of eval:

bdb.py: return eval(expr, globals, locals)
dumbdbm.py: key, pos_and_siz_pai r = eval(line)
gettext.py: return eval('lambda n: int(%s)' % plural)
gopherlib.py: _type_to_name_m ap[eval(name)] = name[2:]
mhlib.py: def do(s): print s; print eval(s)
os.py: eval(name)
pdb.py: x = eval(arg, {}, {})
rexec.py: return eval(code, m.__dict__)
rlcompleter.py: object = eval(expr, self.namespace)
warnings.py: cat = eval(category)
I haven't made any effort to determine how many of them are gaping great
big security holes.

--
Steven
Jan 18 '08 #12

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

Similar topics

2
2660
by: Phil Longworth | last post by:
Im very new to Access 97 and Im sure I should be able to do this but cant work out how. Im bulding a database for my stamp collection. I have two tables; one with details of all the individual stamps, and one with details of covers/envelopes the stamps are fixed to. Each indivdual stamp has a specific reference number (not an auto-number). Rather than manually enter the reference number into my 'covers/envelopes' table I want Access to...
3
8027
by: Thomas McK | last post by:
Hi all, I'm not sure how to get around this, I was hoping someone could provide me with some help. Let's take this simple data set of 2 columns in a table: Tom - id1 Bob - id2 Jane - id1 John - id8 Fred - id2 John - id8
5
2887
by: Greg Corradini | last post by:
Hello All, I'm attempting to create multiple dictionaries at once, each with unique variable names. The number of dictionaries i need to create depends on the length of a list, which was returned from a previous function. The pseudo code for this problem would be: returnedlist = count = 0 for i in returnedlist: if count < len(returnedlist):
3
14580
by: travis.downs | last post by:
Hi, I'm trying to use a macro to create a unique temporary variable name, such as #define TEMP_OBJ(string) MyType obj_ <some magic here(string); So something like TEMP_OBJ("foo")
19
8615
by: Dr Mephesto | last post by:
Hi! I would like to create a pretty big list of lists; a list 3,000,000 long, each entry containing 5 empty lists. My application will append data each of the 5 sublists, so they will be of varying lengths (so no arrays!). Does anyone know the most efficient way to do this? I have tried: list = ,,,,] for _ in xrange(3000000)]
4
2624
by: mopiforu | last post by:
HI, I am creating a web server in which user will upload a file, in my cgi script using the upload method i am getting the file handle and writing the file in my hard drive, later am using the written file to do some process (in which two other files will be created and the names of those files are hard coded) and will list the output for the end user. my question is if i have released this server globally, i may get lots of files at a...
13
3715
by: mliptak | last post by:
I'm trying to implement logging in my application, so that each log message has its unique identifier, e.g. log(identifier, text) What I want to achieve is that the compiler screams if the log() with 'identifier' is also used in some other place in the code which would make the 'identifier' not unique. Is that something that can be achieved in C++? Thanks
3
4131
by: RonBon | last post by:
I need to create unique team versus team match ups for tournaments that I hold. For a given number of teams I need to find a given number games. Example: 10 Teams 5 games each would mean each team would play games against 5 unique opponents. I can load a table with unique combinations but struggle with the logic when it comes to picking each team's match ups. What happens is you run out of match ups when you loop through the table...
0
10077
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9152
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7620
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6853
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5522
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3820
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2991
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.