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? 11 7177
-----Original Message-----
From: py************* *************** ****@python.org [mailto:python-
li************* ************@py thon.org] On Behalf Of breal
Sent: Wednesday, January 16, 2008 2:15 PM
To: py*********@pyt hon.org
Subject: 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?
http://www.python.org/dev/peps/pep-0202/
On Jan 16, 11:33 am, "Reedick, Andrew" <jr9...@ATT.COM wrote:
-----Original Message-----
From: python-list-bounces+jr9445= att....@python. org [mailto:python-
list-bounces+jr9445= att....@python. org] On Behalf Of breal
Sent: Wednesday, January 16, 2008 2:15 PM
To: python-l...@python.org
Subject: 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?
http://www.python.org/dev/peps/pep-0202/
Thanks for the reply. I never realized you could use list
comprehension like this... AWESOME!
I could do nested for ... in loops, but was looking for a Pythonic way
to do this. Ideas?
I find nested for loops very Pythonic. Explicit is better than implicit,
and simple is better than complex.
Regards,
Martin
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?
You can use a recursive generator:
def iterall(*iterab les):
if iterables:
for head in iterables[0]:
for remainder in iterall(*iterab les[1:]):
yield [head] + remainder
else:
yield []
for thing in iterall(
['big', 'medium', 'small'],
['old', 'new'],
['blue', 'green'],
):
print thing
The two for-loops plus recursion should handle any number of
parameters, so if you were so inclined, you could do
for thing in iterall(
['big', 'medium', 'small'],
['old', 'new'],
['blue', 'green'],
['smelly', 'fragrant'],
['spatula', 'avocado'],
):
print thing
and get all 3*2*2*2*2 items. Or count in binary:
for i, bitstream in enumerate(itera ll(
[0, 1],
[0, 1],
[0, 1],
[0, 1],
[0, 1],
[0, 1],
)):
print ''.join(map(str , bitstream)), '=', i
When you're iterating over combinations of items in groups of
lists, I prefer the clarity of this over something like
[(a,b,c,d,e) for a in [0,1] for b in [0,1] for c in [0,1] for
d in [0,1] for e in [0,1]]
-tkc
On Jan 16, 11:15 am, breal <hacker.steven. ..@gmail.comwro te:
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?
I would probably just create a generator:
def permute(a,b,c):
for x in a:
for y in b:
for z in c:
yield [x,y,z]
all_combos = list(permute(
['big', 'small', 'medium'],
['old', 'new'],
['blue', 'green']))
print all_combos
I'm using nested for loops, but I sure find it easy to read that way.
Though, using list comprehension does pretty much the same thing. It
appears that Tim Chase has posted a more generic version of the above.
Matt
On Wed, 16 Jan 2008 11:15:16 -0800, breal wrote:
I could do nested for ... in loops, but was looking for a Pythonic way
to do this. Ideas?
What makes you think nested loops aren't Pythonic?
--
Steven
>I could do nested for ... in loops, but was looking for a Pythonic way
>to do this. Ideas?
What makes you think nested loops aren't Pythonic?
On their own, nested loops aren't a bad thing. I suspect they
become un-Pythonic when they make code look ugly and show a
broken model of the problem. There's a big diffence between:
# iterate over a 10x10 grid
for i in xrange(10):
for j in xrange(10):
print i,j
which is pretty manageable, but quickly becomes very unpythonic
if the problem is poorly defined:
for a in range(5):
for b in range(5):
for c in range(5):
for d in range(5):
for e in range(5):
for f in range(5):
for g in range(5):
for h in range(5):
for i in range(5):
for j in range(5):
for k in range(5):
for l in range(5):
for m in range(5):
for n in range(5):
for o in range(5):
for p in range(5):
for q in range(5):
for r in range(5):
for s in range(5):
for t in range(5):
for u in range(5):
for v in range(5):
for w in range(5):
for x in range(5):
for y in range(5):
for z in range(5):
print a,b,c,d,e,f,g,
print h,i,j,k,l,m,n,
print o,p,q,r,s,t,u,
print v,w,x,y,z
It gets even worse if your loop nesting is based on something
external. You wouldn't want code that looks like
if len(input) == 2:
for a in range(5):
for b in range(5):
whatever(a,b)
elif len(input) == 3:
for a in range(5):
for b in range(5):
for c in range(5):
whatever(a,b,c)
elif len(input) == 4:
...
Contributing to the unpythonic'ness (unpythonicity? ) of it is
that something is clearly happening at a higher level than just
for-loops so other Python constructs should be used to express
them instead of abusing your code to do your dirty work.
-tkc
>
for a in range(5):
....
for z in range(5):
means the inner loop runs 5**26 times so perhaps it's not only
unpythonic but also uncomputable...
> for a in range(5):
...
> for z in range(5):
means the inner loop runs 5**26 times so perhaps it's not only
unpythonic but also uncomputable...
only if you're impatient ;)
yes, it was a contrived pessimal example. It could be range(2)
to generate boolean-number sequences. I've done 2**26 loops in
code before (well, it was on the way to 2**32, just to see how
long it took to roll over and hit an error condition).
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.
-tkc This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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
|
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):
|
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")
|
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)]
| |
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...
|
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
|
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...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
| |
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |