1

Beginner here. I am writing a basic algorithm that involves taking a list of x-values and a list of y-values and then storing all possible combinations of coordinates.

Here are the x and y value lists:

x_line = [6,5,9,3,7,5,9]
y_line = [10,3,5,7,1,7,3,1]

I would ideally want to store all possible coordinates in variable xy because I will be comparing the coordinates in this variable to other sets of coordinates.

I tried to use zip() to do this but I am unsure if I can zip() iterators. Here is that code:

for i in x_line:
   for j in y_line:
       xy = zip(i,j)

This yields an "int object not iterable" error.

I tried even this:

xy = zip(x_line,y_line)

but obviously this does not get every possible combination.

Again, I want to store all possible coordinates given a list of x and y value lists in one variable. I would appreciate any help or guidance. Thanks

1 Answers1

2

you are asking for itertools.product

import itertools
x_line = [6,5,9,3,7,5,9]
y_line = [10,3,5,7,1,7,3,1]
print(list(itertools.product(x_line,y_line)))

you could easily program this your self (probably easiest as a generator)

def get_permutations(x_list,y_list):
    for x in x_list:
        for y in y_list:
            yield (x,y)

print(list(get_permutations(x_line,y_line)))
Joran Beasley
  • 110,522
  • 12
  • 160
  • 179