'How to loop vertically through a list in Python?
This is my code atm:
puzzle = [
['1','1','1','1','1','1','1','1','1','1'],
['1','0','0','0','0','1','1','1','1','1'],
['1','1','0','1','0','0','0','0','0','0'],
['0','1','0','0','0','0','0','0','0','0'],
['0','0','1','0','1','0','1','0','0','0'],
['0','0','0','0','1','0','0','0','0','0'],
['1','0','0','0','1','0','1','0','1','0'],
['0','0','0','0','1','0','0','0','0','1'],
]
#30 1 s
counter1 = 0
counter2 = 0
for a in puzzle:
b = ''.join(a)
for search in b:
if search == '1':
counter1 = counter1 + 1
if counter1 > counter2:
counter2 = counter1
#output should be 3
print(counter1, counter2)
Now I am trying to loop vertically through my list. So I can find the highest amount of 1's after each other without a 0 interfering. Starting with counting from the top left. Only I haven't the slightest idea on how to do that. #output should be 3 Could someone help me out?
Solution 1:[1]
This is how I would go about it
puzzle_new = list(map(list, zip(*puzzle)))
puzzle_new will now look like the following:
[['1', '1', '1', '0', '0', '0', '1', '0'],
['1', '0', '1', '1', '0', '0', '0', '0'],
['1', '0', '0', '0', '1', '0', '0', '0'],
['1', '0', '1', '0', '0', '0', '0', '0'],
['1', '0', '0', '0', '1', '1', '1', '1'],
['1', '1', '0', '0', '0', '0', '0', '0'],
['1', '1', '0', '0', '1', '0', '1', '0'],
['1', '1', '0', '0', '0', '0', '0', '0'],
['1', '1', '0', '0', '0', '0', '1', '0'],
['1', '1', '0', '0', '0', '0', '0', '1']]
This will transpose the puzzle list. You can use the same code that you have for the rest of the logic on puzzle_new
Solution 2:[2]
Vectorized NumPy solution (if I understood your question correctly):
import numpy as np
puzzle = p = [
['1','1','1','1','1','1','1','1','1','1'],
['1','0','0','0','0','1','1','1','1','1'],
['1','1','0','1','0','0','0','0','0','0'],
['0','1','0','0','0','0','0','0','0','0'],
['0','0','1','0','1','0','1','0','0','0'],
['0','0','0','0','1','0','0','0','0','0'],
['1','0','0','0','1','0','1','0','1','0'],
['0','0','0','0','1','0','0','0','0','1'],
]
p = np.array(puzzle).astype(int)
np.max(np.cumsum(np.diff(np.cumsum(p, axis=0), axis=0), axis=0), axis=0)
which gives the maximum number of consecutive 1
s in each of the columns:
array([3, 2, 1, 1, 4, 1, 3, 1, 2, 2])
Edit - solution for the vertically flattened array:
import numpy as np
puzzle = p = [
['1','1','1','1','1','1','1','1','1','1'],
['1','0','0','0','0','1','1','1','1','1'],
['1','1','0','1','0','0','0','0','0','0'],
['0','1','0','0','0','0','0','0','0','0'],
['0','0','1','0','1','0','1','0','0','0'],
['0','0','0','0','1','0','0','0','0','0'],
['1','0','0','0','1','0','1','0','1','0'],
['0','0','0','0','1','0','0','0','0','1'],
]
p = np.array(puzzle).astype(int).flatten(order='F')
[sum(i) for i in np.split(p, np.where(np.diff(p) == 1)[0]+1)]
which gives:
[3, 1, 1, 2, 1, 1, 1, 1, 1, 6, 2, 1, 1, 2, 2, 1, 2, 1]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Dharman |
Solution 2 |