Generating combinations of edges under a certain condition [closed] - python

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have edges of a graph represented as (node_from, node_to).
I want to efficiently generate all combinations of 2 edges of the form (0,x), where 0 is node 0 in my graph - in combination with all combinations of 2 edges of the form (x,n), where n is the "final node" (arbitrary i know) in my graph. I already have all the edges sitting around in a set, or also it is the case that every node contains an edge to every other node (so you could directly iterate through the range to generate combinations, for example).
A valid combination could be
(0,1),(0,5),(7,n),(11,n) or
(0,1),(0,5),(5,n),(11,n) or
(0,1),(0,n),(0,n),(11,n)
And, just to make it clear, I want combinations not permutations. I don't want to re-use the same group.
I am generally pretty good at figuring this stuff out, but I am having a bit of trouble with this one.

EDIT: updated to acommodate the requirements of "only two start/end edges"
I'm not sure what interface you have in mind, but from what I understood it looks like you can use filter() to select a subset of edges that "start in 0" or "end in n".
>>> edges = [(0,1), (0,5), (0,2), (5,3), (2,9), (4,6), (6,9), (3,9), (0,9)]
>>> edges_start = filter(lambda e: e[0] == 0, edges)
>>> edges_end = filter(lambda e: e[1] == 9, edges)
>>> edges_end
[(2, 9), (6, 9), (3, 9), (0, 9)]
>>> edges_start
[(0, 1), (0, 5), (0, 2), (0, 9)]
Now you can use itertools.combinations() to generate all possible pairs from each of the lists. Here's an example:
>>> import itertools
>>> list(itertools.combinations(edges_start, 2))
[((0, 1), (0, 5)), ((0, 1), (0, 2)), ((0, 1), (0, 9)), ((0, 5), (0, 2)), ((0, 5), (0, 9)), ((0, 2), (0, 9))]
Now you can plug in itertools.product() to generate all combinations of "pair from one list" and "pair from other list":
>>> edges_start_pairs = list(itertools.combinations(edges_start, 2))
>>> edges_end_pairs = list(itertools.combinations(edges_end, 2))
>>> pairs = list(itertools.product(edges_start_pairs, edges_end_pairs))
That's it! You can "flatten" the data structure if you like, but this is optional:
>>> flat_pairs = [list(p[0]+p[1]) for p in pairs]
Now let's pretty-print the results:
>>> from pprint import pprint
>>> pprint(flat_pairs)
[[(0, 1), (0, 5), (2, 9), (6, 9)],
[(0, 1), (0, 5), (2, 9), (3, 9)],
[(0, 1), (0, 5), (2, 9), (0, 9)],
[(0, 1), (0, 5), (6, 9), (3, 9)],
[(0, 1), (0, 5), (6, 9), (0, 9)],
[(0, 1), (0, 5), (3, 9), (0, 9)],
[(0, 1), (0, 2), (2, 9), (6, 9)],
[(0, 1), (0, 2), (2, 9), (3, 9)],
[(0, 1), (0, 2), (2, 9), (0, 9)],
[(0, 1), (0, 2), (6, 9), (3, 9)],
[(0, 1), (0, 2), (6, 9), (0, 9)],
[(0, 1), (0, 2), (3, 9), (0, 9)],
[(0, 1), (0, 9), (2, 9), (6, 9)],
[(0, 1), (0, 9), (2, 9), (3, 9)],
[(0, 1), (0, 9), (2, 9), (0, 9)],
[(0, 1), (0, 9), (6, 9), (3, 9)],
[(0, 1), (0, 9), (6, 9), (0, 9)],
[(0, 1), (0, 9), (3, 9), (0, 9)],
[(0, 5), (0, 2), (2, 9), (6, 9)],
[(0, 5), (0, 2), (2, 9), (3, 9)],
[(0, 5), (0, 2), (2, 9), (0, 9)],
[(0, 5), (0, 2), (6, 9), (3, 9)],
[(0, 5), (0, 2), (6, 9), (0, 9)],
[(0, 5), (0, 2), (3, 9), (0, 9)],
[(0, 5), (0, 9), (2, 9), (6, 9)],
[(0, 5), (0, 9), (2, 9), (3, 9)],
[(0, 5), (0, 9), (2, 9), (0, 9)],
[(0, 5), (0, 9), (6, 9), (3, 9)],
[(0, 5), (0, 9), (6, 9), (0, 9)],
[(0, 5), (0, 9), (3, 9), (0, 9)],
[(0, 2), (0, 9), (2, 9), (6, 9)],
[(0, 2), (0, 9), (2, 9), (3, 9)],
[(0, 2), (0, 9), (2, 9), (0, 9)],
[(0, 2), (0, 9), (6, 9), (3, 9)],
[(0, 2), (0, 9), (6, 9), (0, 9)],
[(0, 2), (0, 9), (3, 9), (0, 9)]]

Related

Find all combinations of tuples inside of a list

I am trying to find all permutations of the items inside of the tuples while in the list of length 2. The order of the tuples in relation to each other does not matter.
perm = [(3, 6), (6, 8), (4, 1), (7, 4), (5, 3),
(1, 9), (2, 5), (4, 8), (5, 1), (3, 7),
(6, 9), (10, 2), (7, 10), (8, 2), (9, 10)]
An example of one permutation of the above list would be:
perm = [(6, 3), (6, 8), (4, 1), (7, 4), (5, 3),
(1, 9), (2, 5), (4, 8), (5, 1), (3, 7),
(6, 9), (10, 2), (7, 10), (8, 2), (9, 10)]
Another example permutation would be:
perm = [(6, 3), (8, 6), (1, 4), (4, 7), (3, 5),
(9, 1), (5, 2), (8, 4), (1, 5), (7, 3),
(9, 6), (2, 10), (10, 7), (2, 8), (10, 9)]
In the end, the length of the list of permutations should be 32768, because each tuple is either swapped or not swapped, and 2^15 = 32768. I do not care about the order of the tuples in relation to each other, only the permutations of the items inside of the tuples.
I have tried to use itertools permute, combinations, and product, but I haven't been able to get the desired result.
You can use product:
from itertools import product
lst = [(3, 6), (6, 8), (4, 1), (7, 4), (5, 3),
(1, 9), (2, 5), (4, 8), (5, 1), (3, 7),
(6, 9), (10, 2), (7, 10), (8, 2), (9, 10)]
output = product(*([(x, y), (y, x)] for x, y in lst))
output = list(output) # if you want a list, rather than a generator
print(len(output))
# 32768
print(output[0])
# ((3, 6), (6, 8), (4, 1), (7, 4), (5, 3), (1, 9), (2, 5), (4, 8), (5, 1), (3, 7), (6, 9), (10, 2), (7, 10), (8, 2), (9, 10))
print(output[-1])
# ((6, 3), (8, 6), (1, 4), (4, 7), (3, 5), (9, 1), (5, 2), (8, 4), (1, 5), (7, 3), (9, 6), (2, 10), (10, 7), (2, 8), (10, 9))
The key is to write something like
output = product([(3,6), (6,3)], [(6,8), (8,6)], ..., [(9,10), (10,9)])
in a generic way so that any input list would work, which is done by the generator expression and unpacking (*).

Min and Max value in list of tuples

I have a list of tuples for which I'm trying to get the min & max values at each index. Index 1 seems to be working correctly. For index 2, however, I'm expecting the value (-2,11), but I get (5, 5) instead. Can anyone explain what's going wrong please? Thank you.
test_list = [(-2, 5), (-1, 5), (0, 6), (0, 7), (0, 7), (1, 8), (2, 9), (2, 9), (3, 10),
(4, 11), (-1, 4), (0, 5), (0, 5), (0, 6), (1, 7), (2, 7), (2, 8), (3, 9), (4, 9), (5, 10),
(0, 3), (0, 4), (0, 5), (1, 5), (2, 6), (2, 7), (3, 7), (4, 8), (5, 9), (5, 9), (0, 2),
(0, 3), (1, 4), (2, 5), (2, 5), (3, 6), (4, 7), (5, 7), (5, 8), (6, 9), (0, 2), (1, 2),
(2, 3), (2, 4), (3, 5), (4, 5), (5, 6), (5, 7), (6, 7), (7, 8), (1, 1), (2, 2), (2, 2),
(3, 3), (4, 4), (5, 5), (5, 5), (6, 6), (7, 7), (7, 7), (2, 0), (2, 1), (3, 2), (4, 2),
(5, 3), (5, 4), (6, 5), (7, 5), (7, 6), (8, 7), (2, 0), (3, 0), (4, 1), (5, 2), (5, 2),
(6, 3), (7, 4), (7, 5), (8, 5), (9, 6), (3, 0), (4, 0), (5, 0), (5, 1), (6, 2), (7, 2),
(7, 3), (8, 4), (9, 5), (9, 5), (4, -1), (5, 0), (5, 0), (6, 0), (7, 1), (7, 2), (8, 2),
(9, 3), (9, 4), (10, 5)]
res1 = max(test_list)[0],min(test_list)[0]
res2 = max(test_list)[1],min(test_list)[1]
print ("The min of index 1 : " + str(res1))
print ("The min of index 2 : " + str(res2))
#The min of index 1 : (10, -2)
#The min of index 2 : (5, 5)
Just try these list comprehensions:
first_elements = [elem[0] for elem in test_list]
second_elements = [elem[1] for elem in test_list]
print(min(first_elements), max(second_elements))
print(max(first_elements), min(second_elements))
Output:
"-2 11"
"10 -1"
To get the min and max of each element, use list comprehension like so:
test_list_min = [min(y[x] for y in test_list) for x in range(len(test_list[0]))]
test_list_max = [max(y[x] for y in test_list) for x in range(len(test_list[0]))]
print(test_list_min)
# [-2, -1]
print(test_list_max)
# [10, 11]
You can simple iterate over the list and get the tuples with a for loop.
for t in (test_list):
print("Max: ", max(t), "\tMin:", min(t))
Results:
Max: 5 Min: -2
Max: 5 Min: -1
Max: 6 Min: 0
Max: 7 Min: 0
Max: 7 Min: 0
Max: 8 Min: 1
...
You are reading an index of your max and min results with the line of code: max(test_list)[0],min(test_list)[0]
Try moving the index [] to the end of test_list like this: max(test_list[3]),min(test_list[3]).
You code should look like this:
test_list = [(-2, 5), (-1, 5), (0, 6), (0, 7), (0, 7), (1, 8), (2, 9), (2, 9), (3, 10),
(4, 11), (-1, 4), (0, 5), (0, 5), (0, 6), (1, 7), (2, 7), (2, 8), (3, 9), (4, 9), (5, 10),
(0, 3), (0, 4), (0, 5), (1, 5), (2, 6), (2, 7), (3, 7), (4, 8), (5, 9), (5, 9), (0, 2),
(0, 3), (1, 4), (2, 5), (2, 5), (3, 6), (4, 7), (5, 7), (5, 8), (6, 9), (0, 2), (1, 2),
(2, 3), (2, 4), (3, 5), (4, 5), (5, 6), (5, 7), (6, 7), (7, 8), (1, 1), (2, 2), (2, 2),
(3, 3), (4, 4), (5, 5), (5, 5), (6, 6), (7, 7), (7, 7), (2, 0), (2, 1), (3, 2), (4, 2),
(5, 3), (5, 4), (6, 5), (7, 5), (7, 6), (8, 7), (2, 0), (3, 0), (4, 1), (5, 2), (5, 2),
(6, 3), (7, 4), (7, 5), (8, 5), (9, 6), (3, 0), (4, 0), (5, 0), (5, 1), (6, 2), (7, 2),
(7, 3), (8, 4), (9, 5), (9, 5), (4, -1), (5, 0), (5, 0), (6, 0), (7, 1), (7, 2), (8, 2),
(9, 3), (9, 4), (10, 5)]
max1, min1 = max(test_list[0]), min(test_list[0])
max2, min2 = max(test_list[1]), min(test_list[1])
print ("The min of index 1 : " + str(min1))
print ("The min of index 2 : " + str(min2))
print ("The max of index 1 : " + str(max1))
print ("The max of index 2 : " + str(max2))
Results:
The min of index 1 : -2
The min of index 2 : -1
The max of index 1 : 5
The max of index 2 : 5

Sort a list of tuples by the first element of the tuple without removing duplicates [duplicate]

I have a list of tuples:
self.gridKeys = self.gridMap.keys() # The keys of the instance of the GridMap (It returns the product of every possible combination of positions in the specified grid, in tuples.)
print self.gridKeys
self.gridKeys:
[(7, 3), (6, 9), (0, 7), (1, 6), (3, 7), (2, 5), (8, 5), (5, 8), (4, 0), (9, 0), (6, 7), (5, 5), (7, 6), (0, 4), (1, 1), (3, 2), (2, 6), (8, 2), (4, 5), (9, 3), (6, 0), (7, 5), (0, 1), (3, 1), (9, 9), (7, 8), (2, 1), (8, 9), (9, 4), (5, 1), (7, 2), (1, 5), (3, 6), (2, 2), (8, 6), (4, 1), (9, 7), (6, 4), (5, 4), (7, 1), (0, 5), (1, 0), (0, 8), (3, 5), (2, 7), (8, 3), (4, 6), (9, 2), (6, 1), (5, 7), (7, 4), (0, 2), (1, 3), (4, 8), (3, 0), (2, 8), (9, 8), (8, 0), (6, 2), (5, 0), (1, 4), (3, 9), (2, 3), (1, 9), (8, 7), (4, 2), (9, 6), (6, 5), (5, 3), (7, 0), (6, 8), (0, 6), (1, 7), (0, 9), (3, 4), (2, 4), (8, 4), (5, 9), (4, 7), (9, 1), (6, 6), (5, 6), (7, 7), (0, 3), (1, 2), (4, 9), (3, 3), (2, 9), (8, 1), (4, 4), (6, 3), (0, 0), (7, 9), (3, 8), (2, 0), (1, 8), (8, 8), (4, 3), (9, 5), (5, 2)]
After sorting:
self.gridKeys = self.gridMap.keys() # The keys of the instance of the GridMap (It returns the product of every possible combination of positions in the specified grid, in tuples.)
self.gridKeys.sort() # They're dicts, so they need to be properly ordered for further XML-analysis.
print self.gridKeys
self.gridKeys:
[(0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (0, 6), (0, 7), (0, 8), (0, 9), (1, 0), (1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7), (1, 8), (1, 9), (2, 0), (2, 1), (2, 2), (2, 3), (2, 4), (2, 5), (2, 6), (2, 7), (2, 8), (2, 9), (3, 0), (3, 1), (3, 2), (3, 3), (3, 4), (3, 5), (3, 6), (3, 7), (3, 8), (3, 9), (4, 0), (4, 1), (4, 2), (4, 3), (4, 4), (4, 5), (4, 6), (4, 7), (4, 8), (4, 9), (5, 0), (5, 1), (5, 2), (5, 3), (5, 4), (5, 5), (5, 6), (5, 7), (5, 8), (5, 9), (6, 0), (6, 1), (6, 2), (6, 3), (6, 4), (6, 5), (6, 6), (6, 7), (6, 8), (6, 9), (7, 0), (7, 1), (7, 2), (7, 3), (7, 4), (7, 5), (7, 6), (7, 7), (7, 8), (7, 9), (8, 0), (8, 1), (8, 2), (8, 3), (8, 4), (8, 5), (8, 6), (8, 7), (8, 8), (8, 9), (9, 0), (9, 1), (9, 2), (9, 3), (9, 4), (9, 5), (9, 6), (9, 7), (9, 8), (9, 9)]
The first element of each tuple is the "x", and the second the "y". I'm moving objects in a list through iteration and using these keys (So, if I want to move something in the x axis, I have to go through all the column, and that might be causing a horrid problem that I'm not being able to solve).
How can I sort the tuples in this way?:
[(1, 0), (2, 0), (3, 0), (4, 0), (5, 0), ...]
You can use the key parameter of the sort function, to sort the tuples. The function of key parameter, is to come up with a value which has to be used to compare two objects. So, in your case, if you want the sort to use only the first element in the tuple, you can do something like this
self.gridKeys.sort(key=lambda x: x[0])
If you want to use only the second element in the tuple, then
self.gridKeys.sort(key=lambda x: x[1])
sort function will pass each and every element in the list to the lambda function you pass as parameter to key and it will use the value it returns, to compare two objects in the list. So, in your case, lets say you have two items in the list like this
data = [(1, 3), (1, 2)]
and if you want to sort by the second element, then you would do
data.sort(key=lambda x: x[1])
First it passes (1, 3) to the lambda function which returns the element at index 1, which is 3 and that will represent this tuple during the comparison. The same way, 2 will be used for the second tuple.
This should do the trick
import operator
self.gridKeys.sort(key=operator.itemgetter(1))
While thefourtheye's solution is correct in the strict sense that it is exactly what you asked for in the title. It may not be actually what you want. It may be better to take it a bit farther via sorting by the reverse of the tuple instead.
self.gridKeys.sort(key=lambda x:tuple(reversed(x)))
This forces you to have an ordering like:
[(0, 0), (1, 0), (2, 0), (3, 0), (4, 0), ...]
Rather than having the first element be unordered like:
[(4, 0), (9, 0), (6, 0), (1, 0), (3, 0), ...]
Which is what I get when using:
self.gridKeys.sort(key=lambda x: x[1])
By default Python does a lexicographical sort from left to right. Reversing the tuple effectively makes Python do the lexicographical sort from right to left.

How can I find specific values inside a multidimensional array?

I'm making a game that's a lot like checkers, to create the board I used a multidimensional array. I populate the array with 0's on every empty space and then the one set of chips is represented by 1's and the other is represented by 2's. Is there any way in which I can get a list of what spaces are being occupied by each set of chips? This is my gameboard:
matrix = [[1,1,1,1,1,0,0,0,0,0], [1,1,1,1,0,0,0,0,0,0],
[1,1,1,0,0,0,0,0,0,0], [1,1,0,0,0,0,0,0,0,0],
[1,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,2],
[0,0,0,0,0,0,0,0,2,2], [0,0,0,0,0,0,0,2,2,2],
[0,0,0,0,0,0,2,2,2,2], [0,0,0,0,0,2,2,2,2,2]]
print "\n".join(" ".join(str(el) for el in row) for row in matrix)
You can use list comprehension with condition, following code returns all the spaces occupied by 2:
[(y, x) for y in xrange(len(matrix)) for x in xrange(len(matrix[y])) if matrix[y][x] == 2]
Output:
[(5, 9), (6, 8), (6, 9), (7, 7), (7, 8), (7, 9), (8, 6), (8, 7), (8, 8), (8, 9), (9, 5), (9, 6), (9, 7), (9, 8), (9, 9)]
While list-comprehensions are often a nice way to build a single list, you'll have to go through the entire matrix multiple times to find the positions of every possible chip value. Alternatively, you could create lists of which spaces are occupied by each kind of chip in a single pass through the matrix by doing something like the following:
matrix = [[1,1,1,1,1,0,0,0,0,0], [1,1,1,1,0,0,0,0,0,0],
[1,1,1,0,0,0,0,0,0,0], [1,1,0,0,0,0,0,0,0,0],
[1,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,2],
[0,0,0,0,0,0,0,0,2,2], [0,0,0,0,0,0,0,2,2,2],
[0,0,0,0,0,0,2,2,2,2], [0,0,0,0,0,2,2,2,2,2]]
positions = {}
for y in xrange(len(matrix)):
for x in xrange(len(matrix)):
positions.setdefault(matrix[y][x], []).append((y, x))
# show the all positions of each chip value
for chip in sorted(positions):
print('{}: {}'.format(chip, positions[chip]))
Output:
0: [(0, 5), (0, 6), (0, 7), (0, 8), (0, 9), (1, 4), (1, 5), (1, 6), (1, 7), (1, 8), (1, 9), (2, 3), (2, 4), (2, 5), (2, 6), (2, 7), (2, 8), (2, 9), (3, 2), (3, 3), (3, 4), (3, 5), (3, 6), (3, 7), (3, 8), (3, 9), (4, 1), (4, 2), (4, 3), (4, 4), (4, 5), (4, 6), (4, 7), (4, 8), (4, 9), (5, 0), (5, 1), (5, 2), (5, 3), (5, 4), (5, 5), (5, 6), (5, 7), (5, 8), (6, 0), (6, 1), (6, 2), (6, 3), (6, 4), (6, 5), (6, 6), (6, 7), (7, 0), (7, 1), (7, 2), (7, 3), (7, 4), (7, 5), (7, 6), (8, 0), (8, 1), (8, 2), (8, 3), (8, 4), (8, 5), (9, 0), (9, 1), (9, 2), (9, 3), (9, 4)]
1: [(0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (1, 0), (1, 1), (1, 2), (1, 3), (2, 0), (2, 1), (2, 2), (3, 0), (3, 1), (4, 0)]
2: [(5, 9), (6, 8), (6, 9), (7, 7), (7, 8), (7, 9), (8, 6), (8, 7), (8, 8), (8, 9), (9, 5), (9, 6), (9, 7), (9, 8), (9, 9)]

Use Python to create 2D coordinate

I am truly a novice in Python. Now, I am doing a project which involves creating a list of 2D coordinates. The coordinates should be uniformly placed, using a square grid (10*10), like(0,0)(0,1)(0,2)(0,3)...(0,10)(1,0)(1,2)(1,3)...(2,0)(2,1)(2,2)...(10,10).
Here is my code:
coordinate = []
x = 0
y = 0
while y < 10:
while x < 10:
coordinate.append((x,y))
x += 1
coordinate.append((x,y))
y += 1
print(coordinate)
But I can only get: [(0, 0), (1, 0), (2, 0), (3, 0), (4, 0), (5, 0), (6, 0), (7, 0), (8, 0), (9, 0), (10, 0), (10, 1), (10, 2), (10, 3), (10, 4), (10, 5), (10, 6), (10, 7), (10, 8), (10, 9)]
How can I rewrite my code to get all the points?
It's common to use a couple of for-loops to achieve this:
coordinates = []
for x in range(11):
for y in range(11):
coordinates.append((x, y))
It's also common to simplify this by flattening it into a list comprehension:
coordinates = [(x,y) for x in range(11) for y in range(11)]
from itertools import product
x = (0, 1, 2)
test = product(x, x)
Result:
>>> for ele in test:
... print ele
...
(0, 0)
(0, 1)
(0, 2)
(1, 0)
(1, 1)
(1, 2)
(2, 0)
(2, 1)
(2, 2)
Note that test is a generator, so you probably would want to use list(test).
To actually answer your question, you forgot to reset x back to zero after the first run through x=0..9:
coordinate = []
y = 0
while y < 10:
x = 0
while x < 10:
coordinate.append((x,y))
x += 1
coordinate.append((x,y))
y += 1
print(coordinate)
Feel free to use all other variants, of course.
Use itertools.product:
>>> from itertools import product
>>> list(product(range(11), repeat=2))
[(0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (0, 6), (0, 7), (0, 8), (0, 9), (0, 10), (1, 0), (1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7), (1, 8), (1, 9), (1, 10), (2, 0), (2, 1), (2, 2), (2, 3), (2, 4), (2, 5), (2, 6), (2, 7), (2, 8), (2, 9), (2, 10), (3, 0), (3, 1), (3, 2), (3, 3), (3, 4), (3, 5), (3, 6), (3, 7), (3, 8), (3, 9), (3, 10), (4, 0), (4, 1), (4, 2), (4, 3), (4, 4), (4, 5), (4, 6), (4, 7), (4, 8), (4, 9), (4, 10), (5, 0), (5, 1), (5, 2), (5, 3), (5, 4), (5, 5), (5, 6), (5, 7), (5, 8), (5, 9), (5, 10), (6, 0), (6, 1), (6, 2), (6, 3), (6, 4), (6, 5), (6, 6), (6, 7), (6, 8), (6, 9), (6, 10), (7, 0), (7, 1), (7, 2), (7, 3), (7, 4), (7, 5), (7, 6), (7, 7), (7, 8), (7, 9), (7, 10), (8, 0), (8, 1), (8, 2), (8, 3), (8, 4), (8, 5), (8, 6), (8, 7), (8, 8), (8, 9), (8, 10), (9, 0), (9, 1), (9, 2), (9, 3), (9, 4), (9, 5), (9, 6), (9, 7), (9, 8), (9, 9), (9, 10), (10, 0), (10, 1), (10, 2), (10, 3), (10, 4), (10, 5), (10, 6), (10, 7), (10, 8), (10, 9), (10, 10)]
The above code is equivalent to this nested list comprehension:
>>> [(x, y) for x in range(11) for y in range(11)]
[(0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (0, 6), (0, 7), (0, 8), (0, 9), (0, 10), (1, 0), (1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7), (1, 8), (1, 9), (1, 10), (2, 0), (2, 1), (2, 2), (2, 3), (2, 4), (2, 5), (2, 6), (2, 7), (2, 8), (2, 9), (2, 10), (3, 0), (3, 1), (3, 2), (3, 3), (3, 4), (3, 5), (3, 6), (3, 7), (3, 8), (3, 9), (3, 10), (4, 0), (4, 1), (4, 2), (4, 3), (4, 4), (4, 5), (4, 6), (4, 7), (4, 8), (4, 9), (4, 10), (5, 0), (5, 1), (5, 2), (5, 3), (5, 4), (5, 5), (5, 6), (5, 7), (5, 8), (5, 9), (5, 10), (6, 0), (6, 1), (6, 2), (6, 3), (6, 4), (6, 5), (6, 6), (6, 7), (6, 8), (6, 9), (6, 10), (7, 0), (7, 1), (7, 2), (7, 3), (7, 4), (7, 5), (7, 6), (7, 7), (7, 8), (7, 9), (7, 10), (8, 0), (8, 1), (8, 2), (8, 3), (8, 4), (8, 5), (8, 6), (8, 7), (8, 8), (8, 9), (8, 10), (9, 0), (9, 1), (9, 2), (9, 3), (9, 4), (9, 5), (9, 6), (9, 7), (9, 8), (9, 9), (9, 10), (10, 0), (10, 1), (10, 2), (10, 3), (10, 4), (10, 5), (10, 6), (10, 7), (10, 8), (10, 9), (10, 10)]
Use a for loop. It lets you iterate over things called "iterators". range is a built-in function which returns an iterator from its starting argument (first argument) inclusive. up to its ending argument (second argument) non-inclusive. Thus range(0,11) will return 0,1,2,...,10.
coordinate = []
for y in range(0, 11):
for x in range(0, 11):
coordinate.append((x,y))
print(coordinate)
For more information on for loops in Python, check out the official wiki page.

Categories

Resources