1

i have the following function

import numpy as np

a = [['a','b','c'],['d','e','f'],['g','h','i'],['j','k','l']]
b = np.array(a)

def func01(matrix):
    m,n = np.shape(matrix)
    for jump in range (m-1):
        for row in range (jump):
            for col in range (n):
                print (matrix[row][col],matrix[row+jump][col])

func01(b)

this results in:

('a', 'd') ('b', 'e') ('c', 'f') ('a', 'g') ('b', 'h') ('c', 'i') ('d', 'j') ('e', 'k') ('f', 'l')

however i want my result to look like this:

('a', 'd') ('b', 'e') ('c', 'f') ('a', 'g') ('b', 'h') ('c', 'i') ('a', 'j') ('b', 'k') ('c', 'l') ('d', 'g') ('e', 'h') ('f', 'i') ('d', 'j') ('e', 'k') ('f', 'l') ('g', 'j') ('h', 'k') ('i', 'l')

What have I done wrong? Sorry for my bad English

4
  • Make it do like you want? What's the algorithm that you want. Seems like there is a missing 3rd loop somewhere? Commented Nov 27, 2018 at 17:14
  • I want the first to the last value of the row 0 to be paired with first to the last value of the row 1 respectively, then row 0 with row 2, row 0 with row 3, after that row 1 with row 2, and so on. Without repeating any pair, (a,d) is the same with (d,a) Commented Nov 27, 2018 at 17:22
  • @SurakriKarto See my answer or Christian Sloper's answer below. Should be what you want =) Commented Nov 27, 2018 at 17:28
  • yes, thank you very much :) Commented Nov 27, 2018 at 17:31

3 Answers 3

1

How about this:

a = [['a','b','c'],['d','e','f'],['g','h','i'],['j','k','l']]

new_list = []   
for i in range(len(a)): # Loop over the first subgroup
    for j in range(i+1, len(a)): # Loop over the second subgroup
        for k in range(len(a[0])): # Loop through the elements of each subgroup
            new_list.append((a[i][k], a[j][k]))


new_list
[('a', 'd'), ('b', 'e'), ('c', 'f'),
 ('a', 'g'), ('b', 'h'), ('c', 'i'),
 ('a', 'j'), ('b', 'k'), ('c', 'l'),
 ('d', 'g'), ('e', 'h'), ('f', 'i'),
 ('d', 'j'), ('e', 'k'), ('f', 'l'),
 ('g', 'j'), ('h', 'k'), ('i', 'l')]

This could be more concise with a list-comprehension:

new_list = [(a[i][k], a[j][k]) for i in range(len(a)) 
            for j in range(i+1, len(a)) 
            for k in range(len(a[0])]
Sign up to request clarification or add additional context in comments.

Comments

1

Using combinations from itertools and some numpy slicing:

import numpy as np
import itertools


a = [['a','b','c'],['d','e','f'],['g','h','i'],['j','k','l']]
b = np.array(a)
m,n = b.shape

res = sorted([k for i in range(n) for k in itertools.combinations(b[:,i],2) ])

yields:

[('a', 'd'), ('a', 'g'), ('a', 'j'), ('b', 'e'), ('b', 'h'), ('b', 'k'), ('c', 'f'), ('c', 'i'), ('c', 'l'), ('d', 'g'), ('d', 'j'), ('e', 'h'), ('e', 'k'), ('f', 'i'), ('f', 'l'), ('g', 'j'), ('h', 'k'), ('i', 'l')]

5 Comments

Dunno if OP cares: Although these are the same combinations, the ordering is different than OP expected output... Though it's admittedly much cleaner than my posted answer :P
@StephenCowley ok, good catch. I'll see if i can add some sorting.
ah, indeed this is clean, only the matter of order is a bit off. thank you!
@ChristianSloper Still not quite right. OP's goes [a b c a b c... I've been playing with your solution a little as well and haven't figured out how to get that order either... shrug
@StephenCowley yeah, i guess it is not obvious how to get the correct order with this. a pity :-)
0

This is exactly the use case that zip was made for:

from itertools import combinations

a = [['a','b','c'], ['d','e','f'], ['g','h','i'], ['j','k','l']]

result = [list(zip(*x)) for x in combinations(a, 2)]

# If you want to flatten the result:
flat_result = [item for row in result for item in row]

print(flat_result)

result:

[[('a', 'd'), ('b', 'e'), ('c', 'f')],
 [('a', 'g'), ('b', 'h'), ('c', 'i')],
 [('a', 'j'), ('b', 'k'), ('c', 'l')],
 [('d', 'g'), ('e', 'h'), ('f', 'i')],
 [('d', 'j'), ('e', 'k'), ('f', 'l')],
 [('g', 'j'), ('h', 'k'), ('i', 'l')]]

flat_result:

[('a', 'd'), ('b', 'e'), ('c', 'f'), ('a', 'g'), ('b', 'h'), ('c', 'i'), ('a', 'j'), ('b', 'k'), ('c', 'l'), ('d', 'g'), ('e', 'h'), ('f', 'i'), ('d', 'j'), ('e', 'k'), ('f', 'l'), ('g', 'j'), ('h', 'k'), ('i', 'l')]

Or if you directly want to compute the flat version:

result = []
for x in combinations(a, 2):
    result += list(zip(*x))

2 Comments

woah, thank you! I'm still not familiar with all the Python libraries and stuff, hehe
Yeah, I guessed. That's why I linked the doc to zip :-). Python has a rich library which will usually be more efficient and easier to use than anything that you could write (at least as a beginner). So before coding it yourself, browse the docs to see if you can find a built-in functionality which does what you wanna do.

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.