Generating 2D grid indices using list comprehension - python

I would like to store the row and column indices of a 3 x 3 list in list. It should look like the following:
rc = [(0,0),(0,1),(0,2),(1,0),(1,1),(1,2),(2,0),(2,1),(2,2)]
How can I get this list using a list comprehension in Python?

Some consider multiple for loops inside a list comprehension to be poor style. Use itertools.product() instead:
from itertools import product
list(product(range(3), repeat=2))
This outputs:
[(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]

How about:
[(x, y) for x in range(3) for y in range(3)]

If I understand correctly, and the input is A, the B can be output as follows:
A = [[1,2,3],[4,5,6],[7,8,9]]
B =[(A[i], A[j]) for i in range(3) for j in range(3)]

Related

Given a List get all the combinations of tuples without duplicated results

I have a list=[1,2,3,4]
And I only want to receive tuple results for like all the positions in a matrix, so it would be
(1,1),(1,2),(1,3),(1,4),(2,1),(2,2),(2,3),(2,4),(3,1),(3,2),(3,3),(3,4),(4,1),(4,2),(4,3),(4,4)
I've seen several codes that return all the combinations but i don't know how to restrict it only to the tuples or how to add the (1,1),(2,2),(3,3),(4,4)
Thank you in advance.
You just need a double loop. A generator makes it easy to use
lst = [1,2,3,4]
def matrix(lst):
for i in range(len(lst)):
for j in range(len(lst)):
yield lst[i], lst[j]
output = [t for t in matrix(lst)]
print(output)
Output:
[(1, 1), (1, 2), (1, 3), (1, 4), (2, 1), (2, 2), (2, 3), (2, 4), (3, 1), (3, 2), (3, 3), (3, 4), (4, 1), (4, 2), (4, 3), (4, 4)]
If you just want to do this for making pairs of all symbols in the list
tuple_pairs = [(r,c) for r in lst for c in lst]
If you have instead some maximum row/colum numbers max_row and max_col you could avoid making the lst=[1,2,3,4] and instead;
tuple_pairs = [(r,c) for r in range(1,max_row+1) for c in range(1,max_col+1)]
But that's assuming that the lst's goal was to be = range(1, some_num).
Use itertools.product to get all possible combinations of an iterable object. product is roughly equivalent to nested for-loops with depth specified by the keyword parameter repeat. It returns an iterator.
from itertools import product
lst = [1, 2, 3, 4]
combos = product(lst, repeat=2)
combos = list(combos) # cast to list
print(*combos, sep=' ')
Diagonal terms can be found in a single loop (without any extra imports)
repeat = 2
diagonal = [(i,)*repeat for i in lst]
print(*diagonal sep=' ')
You can do that using list comprehension.
lst=[1,2,3,4]
out=[(i,i) for i in lst]
print(out)
Output:
[(1, 1), (2, 2), (3, 3), (4, 4)]

Zip two int lists with all possible sign combinations

Say I have two lists of ints:
a = [1,2]
b = [3,4]
And I want to end up with a list of tuples like this:
[(1,3),(2,4),(-1,-3),(-2,-4),(1,-3),(2,-4),(-1,3),(-2,4)]
That is, I'm zipping the nth element of a with the nth element of b, but using all possible positive/negative sign combinations. What's the most elegant way to do this?
A not elegant way would be to create two further lists like this
a_neg = [-n for n in a]
b_neg = [-n for n in b]
then zip a with b, a_neg with b_neg, a with b_neg, and a_neg with b, and combine the results. This works fine but is unsatisfying. Is there a simpler way to do this?
I wouldn't try to be fancy with list comprehensions and just use a for loop
for idx, i in enumerate(a):
c.extend([(i, b[idx]), (-i, b[idx]), (i, -b[idx]), (-i, -b[idx])])
>>> c
[(1, 3), (-1, 3), (1, -3), (-1, -3), (2, 4), (-2, 4), (2, -4), (-2, -4)]
Or using a zip
for x,y in zip(a,b):
c.extend([(x,y), (-x, y), (x,-y), (-x, -y)])
Use this answer and just apply it across the zipped sequence, flattening the result into a single list.
from itertools import product
def sign_combinations(nums):
return product(*((x, -x) for x in nums))
a = [1,2]
b = [3,4]
print([c for t in zip(a, b) for c in sign_combinations(t)])

Remove bracket in a list of tuples in tuples

I would like to remove the bracket of a tuples who belong to tuples within a list:
Here's an example:
List_1 = [(0, (1, 1)),
(0, (1, 2)),
(0, (1, 3))]
And the expected output should look like this:
list_2 = [(0, 1, 1),
(0, 1, 2),
(0, 1, 3)]
I tried this:
for element in List_n_d1d2:
newlist = (element[0], element[1])
But ended up with the same output... Could you please help me, thank you !
Use the * spread operator to expand the tuple into separate elements.
list_2 = [(a, *b) for a, b in list_1]
A simple comprehension would be:
[(a,) + b for a,b in List_1]

List to list of tuples conversion

I am brand new to Python. I can change a list to a tuple, for example
li = [1]
tuple(li)
I am trying to create a tuple that gives the item and its position, so a tuple that would come out (1, 0). I have no idea how to get started.
If you want a tuple of tuples, you can use a generator expression like this;
li = [4, 5, 6]
tuple((li[i], i) for i in range(len(li)))
Or with enumerate;
tuple((v, k) for k,v in enumerate(li))
Both will return;
((4, 0), (5, 1), (6, 2))
If you want a list of tuples, you can use a list comprehension expression like this;
[(li[i], i) for i in range(len(li))]
or with enumerate;
[(v,k) for k, v in enumerate(li)]
Both will return;
[(4, 0), (5, 1), (6, 2)]
Use enumerate which does exactly what you need, just with elements and indexes flipped:
> li = [2, 4, 6]
> [(x, i) for i, x in enumerate(li)]
[(2, 0), (4, 1), (6, 2)]
If you want a just one tuple you could do this:
li = (list[1], 1)
The brackets here are the literal syntax for tuples.
If you wanted to do it for all the elements of the list, you could use a list comprehension:
lis = [(list[i], i) for i in range(len(list))]
Which would create a list of tuples were each tuple has the element and its index.
One possible way is to use enumerate,
li = [10, 20, 30]
list(enumerate(li))
prints
[(0, 10), (1, 20), (2, 30)]
If you want the output to be in (item, position) order, you can use,
[(v, k) for k,v in enumerate(li)]
zip with range is another option:
In [4]: li = [2,4,6]
In [5]: zip(li, range(len(li)))
Out[5]: [(2, 0), (4, 1), (6, 2)]

For loop to compare one element with all the remaining elements of an array

I have an array in Python [0,1,2,3,4] with 5 elements. I want to compare elements in following fashion.
(0,1),(0,2),(0,3),(0,4),(1,2),(1,3),(1,4),(2,3),(2,4),(3,4),(4,4)
What I am doing is as follows.
for i in range(len(array)):
for j in range(i+1,len(array)):
But this is comparing in following fashion.
(0,1),(1,2),(2,3),(3,4)...
Where I am doing it wrong?
This code produces the desired result:
array = [0,1,2,3,4]
for i in range(len(array)):
for j in range(i+1,len(array)):
print(array[i], array[j])
print(array[-1], array[-1])
This code is one way that you may have gotten the erroneous result:
for i in range(len(array)):
for j in range(i+1,len(array)):
print(array[i], array[j])
break
Using itertools is another option:
>>> [x for x in itertools.combinations(xrange(5), 2)]
[(0, 1), (0, 2), (0, 3), (0, 4), (1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)]

Categories

Resources