473,385 Members | 1,309 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.

Combining arbitrary lists

Given that

n = [ [1, 2, 3], [4, 5, 6], [7, 8] ]

then the following code produces what I expect

for x in n[0]:
for y in n[1]:
for z in n[2]:
print [x, y, z]

--output--
[1, 4, 7]
[1, 4, 8]
[1, 5, 7]
[1, 5, 8]
...
[3, 6, 8]
-- --

How can I do this for an arbirary length of n? This reminds me of those
horrible fraction questions in 1st year comp. sci.
x = 1 + 1/(1 + 1/(1 + 1/(..... )))))... which leads me to suspect that
the simplest solution is recursive... hmmm... I'll take any suggestions.

Muchos Gracias
Nick.
Jul 18 '05 #1
7 1377
Nick wrote:
Given that

n = [ [1, 2, 3], [4, 5, 6], [7, 8] ]

then the following code produces what I expect

for x in n[0]:
for y in n[1]:
for z in n[2]:
print [x, y, z]
....
How can I do this for an arbirary length of n?
I think this is what you're looking for:
http://aspn.activestate.com/ASPN/Coo.../Recipe/302478
Muchos Gracias


De nada ;)

--
Mariano

Jul 18 '05 #2
Mariano Draghi wrote:
Nick wrote:
Given that

n = [ [1, 2, 3], [4, 5, 6], [7, 8] ]

then the following code produces what I expect

for x in n[0]:
for y in n[1]:
for z in n[2]:
print [x, y, z]

...

How can I do this for an arbirary length of n?

I think this is what you're looking for:
http://aspn.activestate.com/ASPN/Coo.../Recipe/302478
Muchos Gracias

De nada ;)

Okay... a) THANK-YOU, and b) HOW did you find that?

Nick.
Jul 18 '05 #3
Nick wrote:
....
I think this is what you're looking for:
http://aspn.activestate.com/ASPN/Coo.../Recipe/302478
.... Okay... a) THANK-YOU, and b) HOW did you find that?


The ASPN Python Cookbook has plenty of these kind of things.
I looked for "combination" there... I think (don't remember exactly).

You can also do a search in the comp.lang.python group using Google.
Try this:
http://groups.google.com/groups?q=co....lang.python.*

Regards,

--
Mariano

Jul 18 '05 #4
On Mon, 15 Nov 2004 00:57:03 -0300, Mariano Draghi <md*****@prosud.com> wrote:
Nick wrote:
Given that

n = [ [1, 2, 3], [4, 5, 6], [7, 8] ]

then the following code produces what I expect

for x in n[0]:
for y in n[1]:
for z in n[2]:
print [x, y, z]


...

How can I do this for an arbirary length of n?


I think this is what you're looking for:
http://aspn.activestate.com/ASPN/Coo.../Recipe/302478

Following is a slightly more compact generator (unless I goofed ;-)
(untested beyoud what you see here; probably traded a little speed for code lines)
n [[1, 2, 3], [4, 5, 6], [7, 8]]
def doit(LOL): ... if not LOL: yield []; return
... for h in LOL[0]:
... for t in doit(LOL[1:]):
... yield [h] + t
... for row in doit(n): print row ...
[1, 4, 7]
[1, 4, 8]
[1, 5, 7]
[1, 5, 8]
[1, 6, 7]
[1, 6, 8]
[2, 4, 7]
[2, 4, 8]
[2, 5, 7]
[2, 5, 8]
[2, 6, 7]
[2, 6, 8]
[3, 4, 7]
[3, 4, 8]
[3, 5, 7]
[3, 5, 8]
[3, 6, 7]
[3, 6, 8]
for row in doit([[1,2],[3,4,5]]): print row

...
[1, 3]
[1, 4]
[1, 5]
[2, 3]
[2, 4]
[2, 5]
Regards,
Bengt Richter
Jul 18 '05 #5
* Nick (2004-11-15 04:28 +0100)
Given that

n = [ [1, 2, 3], [4, 5, 6], [7, 8] ]

then the following code produces what I expect

for x in n[0]:
for y in n[1]:
for z in n[2]:
print [x, y, z]

--output--
[1, 4, 7]
[1, 4, 8]
[1, 5, 7]
[1, 5, 8]
...
[3, 6, 8]
-- --

How can I do this for an arbirary length of n?


This is the Cartesian product of three sets. Have a look at
http://www.thorstenkampe.de/python/utils.py:

def cartes(seq0, seq1, modus = 'pair'):
""" return the Cartesian Product of two sequences """
if modus == 'pair':
return [[item0, item1] for item0 in seq0 for item1 in seq1]
elif modus == 'triple':
return [item0 + [item1] for item0 in seq0 for item1 in seq1]

Then you would generate your output like this:
cartes(cartes([1, 2, 3], [4, 5, 6]), [7, 8], 'triple')

Jul 18 '05 #6
Thorsten Kampe wrote:
* Nick (2004-11-15 04:28 +0100)
Given that

n = [ [1, 2, 3], [4, 5, 6], [7, 8] ]

then the following code produces what I expect

for x in n[0]:
for y in n[1]:
for z in n[2]:
print [x, y, z]

--output--
[1, 4, 7]
[1, 4, 8]
[1, 5, 7]
[1, 5, 8]
...
[3, 6, 8]
-- --

How can I do this for an arbirary length of n?

This is the Cartesian product of three sets. Have a look at
http://www.thorstenkampe.de/python/utils.py:

def cartes(seq0, seq1, modus = 'pair'):
""" return the Cartesian Product of two sequences """
if modus == 'pair':
return [[item0, item1] for item0 in seq0 for item1 in seq1]
elif modus == 'triple':
return [item0 + [item1] for item0 in seq0 for item1 in seq1]

Then you would generate your output like this:
cartes(cartes([1, 2, 3], [4, 5, 6]), [7, 8], 'triple')


I think the point of the question was to act on an arbitrary number of
length-3 lists.

regards
Steve
--
http://www.holdenweb.com
http://pydish.holdenweb.com
Holden Web LLC +1 800 494 3119
Jul 18 '05 #7
Thanks to all who replied; very helpful. Bengt Richter: that's a
lovely generator--I'm new to the whole idea of generators, but that's
very clear. I will experiment and test it.

Nick.
Jul 18 '05 #8

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

Similar topics

7
by: Paddy McCarthy | last post by:
Hi, I am trying to use eval as little as possible but solve this problem. #If given:two or more lambda equations x=lambda : A < B y=lambda : C+6 >= 7 .... How do I create another lambda...
8
by: mikea_59 | last post by:
I am having trouble combining similar elements. Elements can be combined if they have the same name and the same attribute values. I can handle single level elements but am having problems with...
2
by: Chris Mullins | last post by:
I've spent a bit of time over the last year trying to implement RFC 3454 (Preparation of Internationalized Strings, aka 'StringPrep'). This RFC is also a dependency for RFC 3491...
3
by: Randy Yates | last post by:
Hi, We know we can build arrays of variables of the same type and arrays of functions of the same "type" (i.e., same return value and same parameters), but is there a way to automate the calling...
12
by: Chadwick Boggs | last post by:
I need to perform modulo operations on extremely large numbers. The % operator is giving me number out of range errors and the mod(x, y) function simply seems to return the wrong results. Also,...
0
by: Greg Collins [Microsoft MVP] | last post by:
I have written and posted the following articles which will prove useful to some: Display Lists in Columns Horizontally Using One Cell per Column...
28
by: walterbyrd | last post by:
Python seems to have a log of ways to do collections of arbitrary objects: lists, tuples, dictionaries. But what if I want a collection of non-arbitrary objects? A list of records, or something...
5
by: TokiDoki | last post by:
Hi! I have a Python problem which is my last problem to solve to finish up a Django application. This is amazingly simple but I have been stuck now for a couple of days. It is embarrisingly...
0
by: John O'Hagan | last post by:
On Tue Sep 30 11:32:41 CEST 2008, Steven D'Aprano Thanks, both to you and Bruno for pointing this out, I'll certainly be using it in future.
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: 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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.