8

I have a huge group of lists within lists that I want to combine: It looks sort of like this:

[[1,2,3,4,5], [6,7,8,9,0], [2,5,7,9,4], [4,7,8,43,6]...]

up to about 20 of these lists in a list. I now want to combine the first list and second list to look like this:

[[1,6], [2,7], [3,8], [4,9], [5,0]]

And then I want to do it again with the 1st and 3rd, all the way until the end. And then do it again starting with the second list to 3rd,4th...last line (but not the first one because that was already done with 1st to 2nd list). How can I write code that will do this?

Here is what I have so far:

xcols = the column with all the lists like I showed above

def MakeLists(xcols):
    multilist = []
    for i in xcols:
        for j in xcols[index(i):]:
            currentlist = map(list.__add__, i, j)
            multilist.append(currentlist)

Gives me an error when I run it though, probably at the map part because I don't know how to first convert each element into a list and then map them. Any help would be great. Thanks!

5 Answers 5

7

How about something like this:

>>> import itertools
>>> foo = [[1, 2, 3], [4, 5, 6], [7, 8, 8]]
>>> for p in itertools.permutations(foo, 2):
...     print zip(*p)
... 
[(1, 4), (2, 5), (3, 6)]
[(1, 7), (2, 8), (3, 8)]
[(4, 1), (5, 2), (6, 3)]
[(4, 7), (5, 8), (6, 8)]
[(7, 1), (8, 2), (8, 3)]
[(7, 4), (8, 5), (8, 6)]

Edit: In case you only want to zip a list with those after it, as people in comments are explaining:

>>> import itertools
>>> for p in itertools.combinations(foo, 2):
...     print zip(*p)
... 
[(1, 4), (2, 5), (3, 6)]
[(1, 7), (2, 8), (3, 8)]
[(4, 7), (5, 8), (6, 8)]
Sign up to request clarification or add additional context in comments.

7 Comments

This would be a very good solution, but this does not work in a way OP wants it to work - it zips the second list with the first etc. (but it should zip only with lists following the second list etc.).
I think OP must've asked for combinations, rather than permutations: "(but not the first one because that was already done with 1st to 2nd list)"
Thanks, guys. I guess I didn't understand correctly or pay enough attention. I've updated my answer.
It looks like there are tuples within the major list? Is that any different? Is there a way to use this method to force the tuples to become lists?
You can use [list(x) for x in zip(*p)]. But only do it if you actually need lists.
|
2

You will only be able to accomplish your intended result if you have an even number of lists. This code will produce the intended result. There may be something more "pythonic", however.

foo = [[1,2,3,4,5],[6,7,8,9,0],[2,5,7,9,4],[4,7,8,43,6]]
newlist = []

for i in xrange(len(foo)):
    if i % 2 == 0:
        list1 = foo[i]
        list2 = foo[i + 1]
        for n in xrange(len(list1)):
            newlist.append([list1[n],list2[n]])

print newlist

Result:

[[1, 6], [2, 7], [3, 8], [4, 9], [5, 0], [2, 4], [5, 7], [7, 8], [9, 43], [4, 6]]

Comments

2
def foo(li):
  for element in li[1:]:
    for pair in zip(li[0], element): 
      yield pair

>>> from test import foo
>>> bar = [[1, 2, 3, 5, 6], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15]]
>>> foo(bar)
<generator object foo at 0x10592df50>
>>> [e for e in foo(bar)]
[(1, 6), (2, 7), (3, 8), (5, 9), (6, 10), (1, 11), (2, 12), (3, 13), (5, 14), (6, 15)]

Comments

2
a=[[1,2,3,4,5],[6,7,8,9,0],[2,5,7,9,4],[4,7,8,43,6]]

i=0

for l in a[i:]:
    for inner in a[i+1:]:
        print [list(b) for b in zip(l, inner)]
    i += 1

prints

[[1, 6], [2, 7], [3, 8], [4, 9], [5, 0]]
[[1, 2], [2, 5], [3, 7], [4, 9], [5, 4]]
[[1, 4], [2, 7], [3, 8], [4, 43], [5, 6]]
[[6, 2], [7, 5], [8, 7], [9, 9], [0, 4]]
[[6, 4], [7, 7], [8, 8], [9, 43], [0, 6]]
[[2, 4], [5, 7], [7, 8], [9, 43], [4, 6]]

1 Comment

Generally you don't want to use c-style loops in Python. Sometimes necessary, but it's unpythonic.
1

The shortest solution is (lsts is a list of the lists you have):

[zip(lsts[i],lsts[j]) for i in xrange(len(lsts)) for j in xrange(i,len(lsts)) if i!=j]

It will do exactly what you said. Try this out.

Is it what you expected?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.