Related
This is a nested list:
matrix = [['0', '0', '0', '0', '0', '1', '1', '0', '0', '0'],
['0', '1', '1', '1', '1', '1', '0', '0', '0', '0'],
['0', '0', '1', '1', '0', '1', '0', '1', '1', '0'],
['0', '1', '0', '0', '0', '0', '1', '0', '0', '1'],
['0', '0', '0', '1', '0', '0', '1', '0', '1', '0'],
['0', '0', '0', '0', '1', '0', '1', '1', '1', '0'],
['0', '1', '1', '1', '1', '0', '1', '1', '1', '1'],
['1', '1', '1', '1', '1', '1', '1', '1', '1', '1']]
#output should be [1,2,2,2,3,1,5,3,4,2]
Height = 8
Width = 10
I want to know how I can get the height of each column. And then each height, I want to put it in a list.
We counting 1's and they only count for the height if they are adjoint 1's.
We start with counting below and then go up.
Output should be [1,2,2,2,3,1,5,3,4,1]
I only want to use build in Python functions.
I tried with a for loop and if, else statements.
For loop iterate through the list.
like if i == '1' add 1 to counter.
if i == '0' reset counter and add the last value from counter to counter1, but only if counter is greater then counter1.
You can use itertools.takewhile on the reversed, zipped matrix:
from itertools import takewhile
out = [len(list(takewhile(lambda x: x=='1', reversed(l)))) for l in zip(*matrix)]
output:
[1, 2, 2, 2, 3, 1, 5, 3, 4, 2]
If you don't want to import takewhile, use the recipe:
def takewhile(predicate, iterable):
# takewhile(lambda x: x<5, [1,4,6,4,1]) --> 1 4
for x in iterable:
if predicate(x):
yield x
else:
break
How it works:
zip rotates the matrix:
>>> list(zip(*matrix))
[('0', '0', '0', '0', '0', '0', '0', '1'),
('0', '1', '0', '1', '0', '0', '1', '1'),
('0', '1', '1', '0', '0', '0', '1', '1'),
('0', '1', '1', '0', '1', '0', '1', '1'),
('0', '1', '0', '0', '0', '1', '1', '1'),
('1', '1', '1', '0', '0', '0', '0', '1'),
('1', '0', '0', '1', '1', '1', '1', '1'),
('0', '0', '1', '0', '0', '1', '1', '1'),
('0', '0', '1', '0', '1', '1', '1', '1'),
('0', '0', '0', '1', '0', '0', '1', '1')]
The list comprehension with reversed rotates the other way around (actually inverses each row):
>>> [list(reversed(l)) for l in zip(*matrix)]
[['1', '0', '0', '0', '0', '0', '0', '0'],
['1', '1', '0', '0', '1', '0', '1', '0'],
['1', '1', '0', '0', '0', '1', '1', '0'],
['1', '1', '0', '1', '0', '1', '1', '0'],
['1', '1', '1', '0', '0', '0', '1', '0'],
['1', '0', '0', '0', '0', '1', '1', '1'],
['1', '1', '1', '1', '1', '0', '0', '1'],
['1', '1', '1', '0', '0', '1', '0', '0'],
['1', '1', '1', '1', '0', '1', '0', '0'],
['1', '1', '0', '0', '1', '0', '0', '0']]
takewhile keepd the elements while the condition is True, here while the items are '1' (lambda x: x=='1'), and len gets the length of the output:
>>> l = ['1', '1', '1', '0', '0', '0', '1', '0']
>>> list(takewhile(lambda x: x=='1', l))
['1', '1', '1']
>>> len(list(takewhile(lambda x: x=='1', l)))
3
NB. functions like zip, reversed, takewhile are generators, they don't produce output unless something consumes it, that's why I used list(generator(…)) in the exammples
solution with classical python loops:
out = []
for l in zip(*matrix):
counter = 0
for elem in reversed(l):
if elem == '1':
counter +=1
else:
break
out.append(counter)
The output of the function is to remove all lines (including list1 after the text) whose first n strings are contained in another line.
The code I tried is terribly written and super slow, but I have no other idea about how to do this. Is there an easier and faster way to do this?
The order of each element in the line is important.
list1 = [["0","0","0","0","0"],
["0","0","0","0","0","0"],
["0","0","0","0","0","275"],
["0","0","0","0","0","275","275"],
["0","0","0","0","275"],
["0","0","0","0","275","275"],
["0","0","0","0","275","990"],
["0","0","0","0","275","990","990"],
["0","0","0","0","275","990","2761"],
["0","0","0","0","275","990","2761","2761"],
["0","0","0","0","688"],
["0","0","0","0","688","688"],
["0","0","0","0","688","1940"],
["0","0","0","0","688","1940","1940"],
["0","0","0","0","688","1940","5041"],
["0","0","0","0","688","1940","5041","5041"],
["0","0","0","165","165","165"],
["0","0","0","165","165","165","165"]]
remove_lines = []
index_to_be_removed = []
for x in range(len(list1)):
for y in range(len(list1)):
if len(list1[x]) > len(list1[y]):
c = 0
for i in range(len(list1[y])):
if list1[x][i] == list1[y][i]:
c = c + 1
if c == len(list1[y]):
if list1[x] not in remove_lines:
remove_lines.append(list1[x])
index_to_be_removed.append(x)
for x in range(len(index_to_be_removed)):
print("lines to be removed:", list1[index_to_be_removed[x]])
Output of the lines which we want to remove:
lines to be removed: ['0', '0', '0', '0', '0', '0']
lines to be removed: ['0', '0', '0', '0', '0', '275']
lines to be removed: ['0', '0', '0', '0', '0', '275', '275']
lines to be removed: ['0', '0', '0', '0', '275', '275']
lines to be removed: ['0', '0', '0', '0', '275', '990']
lines to be removed: ['0', '0', '0', '0', '275', '990', '990']
lines to be removed: ['0', '0', '0', '0', '275', '990', '2761']
lines to be removed: ['0', '0', '0', '0', '275', '990', '2761', '2761']
lines to be removed: ['0', '0', '0', '0', '688', '688']
lines to be removed: ['0', '0', '0', '0', '688', '1940']
lines to be removed: ['0', '0', '0', '0', '688', '1940', '1940']
lines to be removed: ['0', '0', '0', '0', '688', '1940', '5041']
lines to be removed: ['0', '0', '0', '0', '688', '1940', '5041', '5041']
lines to be removed: ['0', '0', '0', '165', '165', '165', '165']
this code is expected to be versatile and Fast.
Lists = [["0","0","0","0","0"], ["0","0","0","0","0","0"], ["0","0","0","0","0","275"], ["0","0","0","0","0","275","275"], ["0","0","0","0","275"], ["0","0","0","0","275","275"], ["0","0","0","0","275","990"], ["0","0","0","0","275","990","990"], ["0","0","0","0","275","990","2761"], ["0","0","0","0","275","990","2761","2761"], ["0","0","0","0","688"], ["0","0","0","0","688","688"], ["0","0","0","0","688","1940"], ["0","0","0","0","688","1940","1940"], ["0","0","0","0","688","1940","5041"], ["0","0","0","0","688","1940","5041","5041"], ["0","0","0","165","165","165"], ["0","0","0","165","165","165","165"]]
def Get_Uniques(Lists):
Selected, Rejected = [], []
for List in Lists :
lenL, selected = len(List), True
for ele in Selected:
lenE = len(ele)
if lenE > lenL: continue
if List[:lenE] == ele:
Rejected.append(List)
selected = False
break
if selected: Selected.append(List)
return Selected, Rejected
Selected, Rejected = Get_Uniques(Lists)
print("Selected Lists : ", *Selected, "\nRejected Lists : ", *Rejected, sep="\n")
OUTPUT
Selected Lists :
['0', '0', '0', '0', '0']
['0', '0', '0', '0', '275']
['0', '0', '0', '0', '688']
['0', '0', '0', '165', '165', '165']
Rejected Lists :
['0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '275']
['0', '0', '0', '0', '0', '275', '275']
['0', '0', '0', '0', '275', '275']
['0', '0', '0', '0', '275', '990']
['0', '0', '0', '0', '275', '990', '990']
['0', '0', '0', '0', '275', '990', '2761']
['0', '0', '0', '0', '275', '990', '2761', '2761']
['0', '0', '0', '0', '688', '688']
['0', '0', '0', '0', '688', '1940']
['0', '0', '0', '0', '688', '1940', '1940']
['0', '0', '0', '0', '688', '1940', '5041']
['0', '0', '0', '0', '688', '1940', '5041', '5041']
['0', '0', '0', '165', '165', '165', '165']
Hope this helps!
I would do this through a pairwise comparison while loop. Basically, I first store all pairs of lists, (n lists becomes 0.5*n*(n-1) pairs of lists). Then, one at a time, I look at each pair, find the smaller list, and check if one is contained in the other using delimited-string comparison. Here's the code:
from itertools import combinations
lists = [["0","0","165","165"],...]
pairs = combinations(lists)
for pair in pairs:
smaller = pair[0] if len(pair[0]) <= len(pair[1]) else pair[1]
bigger = pair[1] if smaller == pair[0] else pair[0]
smaller_str = ",".join(smaller)
bigger_str = ",".join(bigger)
if smaller_str in bigger_str and smaller in lists:
lists.remove(smaller)
print(lists)
This ought to leave only those arrays which are not contained in any other arrays. I don't know if it's necessarily more efficient than your algorithm when you break down its component operations, but it's definitely a bit easier to follow.
You could use a dictionary of tuples for the lists that you keep and use it to check the relevant prefix lengths of subsequent lists. This will give results in O(N) time, or more specifically O(NxD) where N is the number of lists and D is the length difference between the largest and smallest list.
keep = dict() # lists to keep (as tuples)
minLen = min(map(len,list1)) # shortest prefix length
for i,L in enumerate(list1):
prefixes = (tuple(L[:n]) for n in range(minLen,len(L)+1))
if not any(sl in keep for sl in prefixes): # check all prefix lengths
keep[tuple(L)]=i # track lists that remain
else:
print("list to remove",L) # list at index i will be dropped
# clean up
list1 = [*map(list,keep)]
Output:
list to remove ['0', '0', '0', '0', '0', '0']
list to remove ['0', '0', '0', '0', '0', '275']
list to remove ['0', '0', '0', '0', '0', '275', '275']
list to remove ['0', '0', '0', '0', '275', '275']
list to remove ['0', '0', '0', '0', '275', '990']
list to remove ['0', '0', '0', '0', '275', '990', '990']
list to remove ['0', '0', '0', '0', '275', '990', '2761']
list to remove ['0', '0', '0', '0', '275', '990', '2761', '2761']
list to remove ['0', '0', '0', '0', '688', '688']
list to remove ['0', '0', '0', '0', '688', '1940']
list to remove ['0', '0', '0', '0', '688', '1940', '1940']
list to remove ['0', '0', '0', '0', '688', '1940', '5041']
list to remove ['0', '0', '0', '0', '688', '1940', '5041', '5041']
list to remove ['0', '0', '0', '165', '165', '165', '165']
print(list1)
[['0', '0', '0', '0', '0'],
['0', '0', '0', '0', '275'],
['0', '0', '0', '0', '688'],
['0', '0', '0', '165', '165', '165']]
I have this list :
List_of_all = [
['3', '0', '6', '5', '0', '8', '4', '0', '0'],
['5', '2', '0', '0', '0', '0', '0', '0', '0'],
['0', '8', '7', '0', '0', '0', '0', '3', '1'],
['0', '0', '3', '0', '1', '0', '0', '8', '0'],
['9', '0', '0', '8', '6', '3', '0', '0', '5'],
['0', '5', '0', '0', '9', '0', '6', '0', '0'],
['1', '3', '0', '0', '0', '0', '2', '5', '0'],
['0', '0', '0', '0', '0', '0', '0', '7', '4'],
['0', '0', '5', '2', '0', '6', '3', '0', '0']
]
And I need to also 9 vertically list of items
My code :
vertical = []
x = []
n = 0
for List in List_of_all:
for num in List[n]:
x.append(num)
vertical.append(x)
x = []
n += 1
if n == 9:
break
My out put :
[['3'], ['2'], ['7'], ['0'], ['6'], ['0'], ['2'], ['7'], ['0']]
Why this iteration dont't work true ?!
How should I define this to get
Bold assumption: you are looking for the following transposition:
>>> list(map(list, zip(*List_of_all)))
[['3', '5', '0', '0', '9', '0', '1', '0', '0'],
['0', '2', '8', '0', '0', '5', '3', '0', '0'],
['6', '0', '7', '3', '0', '0', '0', '0', '5'],
['5', '0', '0', '0', '8', '0', '0', '0', '2'],
['0', '0', '0', '1', '6', '9', '0', '0', '0'],
['8', '0', '0', '0', '3', '0', '0', '0', '6'],
['4', '0', '0', '0', '0', '6', '2', '0', '3'],
['0', '0', '3', '8', '0', '0', '5', '7', '0'],
['0', '0', '1', '0', '5', '0', '0', '4', '0']]
The zip(*...) idiom is the simplest way to go about that in pure Python. Without the nested list casting, it will be an iterator over tuples.
Basically, you are taking the diagonal array, you can do:
import numpy as np
vertical = np.diag(List_of_all)
print(vertical)
array(['3', '2', '7', '0', '6', '0', '2', '7', '0'], dtype='<U1')
# to put each element in a list do this
vertical = [[x] for x in np.diag(List_of_all)]
print(vertical)
[['3'], ['2'], ['7'], ['0'], ['6'], ['0'], ['2'], ['7'], ['0']]
List_of_all = [
['3', '0', '6', '5', '0', '8', '4', '0', '0'],
['5', '2', '0', '0', '0', '0', '0', '0', '0'],
['0', '8', '7', '0', '0', '0', '0', '3', '1'],
['0', '0', '3', '0', '1', '0', '0', '8', '0'],
['9', '0', '0', '8', '6', '3', '0', '0', '5'],
['0', '5', '0', '0', '9', '0', '6', '0', '0'],
['1', '3', '0', '0', '0', '0', '2', '5', '0'],
['0', '0', '0', '0', '0', '0', '0', '7', '4'],
['0', '0', '5', '2', '0', '6', '3', '0', '0']
]
vertical=[]
for i in range(9):
print([x[i] for x in List_of_all])#for visual appeal only
vertical.append([x[i] for x in List_of_all])
print()
for line_slicer in range(0,9,3):
count=0
for line in vertical:
count+=1
print(line[line_slicer:line_slicer+3])
if count%3==0:
print()
Output:
['3', '5', '0', '0', '9', '0', '1', '0', '0']
['0', '2', '8', '0', '0', '5', '3', '0', '0']
['6', '0', '7', '3', '0', '0', '0', '0', '5']
['5', '0', '0', '0', '8', '0', '0', '0', '2']
['0', '0', '0', '1', '6', '9', '0', '0', '0']
['8', '0', '0', '0', '3', '0', '0', '0', '6']
['4', '0', '0', '0', '0', '6', '2', '0', '3']
['0', '0', '3', '8', '0', '0', '5', '7', '0']
['0', '0', '1', '0', '5', '0', '0', '4', '0']
['3', '5', '0']
['0', '2', '8']
['6', '0', '7']
['5', '0', '0']
['0', '0', '0']
['8', '0', '0']
['4', '0', '0']
['0', '0', '3']
['0', '0', '1']
['0', '9', '0']
['0', '0', '5']
['3', '0', '0']
['0', '8', '0']
['1', '6', '9']
['0', '3', '0']
['0', '0', '6']
['8', '0', '0']
['0', '5', '0']
['1', '0', '0']
['3', '0', '0']
['0', '0', '5']
['0', '0', '2']
['0', '0', '0']
['0', '0', '6']
['2', '0', '3']
['5', '7', '0']
['0', '4', '0']
If you want a list of the first elements in all of your lists:
vertical=[i[0] for i in List_of_all]
this would be the same as
vertical= ['3','5','0','0','9','0','1','0','0']
You can get the columns as folows:
from collections import defaultdict
List_of_all = [
['3', '0', '6', '5', '0', '8', '4', '0', '0'],
['5', '2', '0', '0', '0', '0', '0', '0', '0'],
['0', '8', '7', '0', '0', '0', '0', '3', '1'],
['0', '0', '3', '0', '1', '0', '0', '8', '0'],
['9', '0', '0', '8', '6', '3', '0', '0', '5'],
['0', '5', '0', '0', '9', '0', '6', '0', '0'],
['1', '3', '0', '0', '0', '0', '2', '5', '0'],
['0', '0', '0', '0', '0', '0', '0', '7', '4'],
['0', '0', '5', '2', '0', '6', '3', '0', '0']
]
if __name__ == '__main__':
columns = defaultdict(list)
for row in List_of_all:
for j, item in enumerate(row):
columns[j].append(item)
print(columns[0]) # ['3', '5', '0', '0', '9', '0', '1', '0', '0']
It is unclear what you expect of output. If you want to get all the columns in the 2D list, one option is to use numpy:
>>> import numpy as np
>>> items = np.array([[1,2,3], [4,5,6], [7,8,9]])
>>> items
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
>>> columns = items.T
>>> columns
array([[1, 4, 7],
[2, 5, 8],
[3, 6, 9]])
If you transpose the numpy array, the columns will be rows and vice versa.
Another way to do this is with numpy indexing:
>>> columns = [items[:, i] for i in range(len(items[0]))]
>>> columns
[array([1, 4, 7]), array([2, 5, 8]), array([3, 6, 9])]
>>>
If you simply want to use loops, you should iterate column-wise and not row-wise:
>>> columns = []
>>> for j in range(len(items[0])):
... column = []
... for i in range(len(items)):
... column.append(items[i, j])
... columns.append(column)
...
>>> columns
[[1, 4, 7], [2, 5, 8], [3, 6, 9]]
A more pythonic way for the latter solution would be with list comprehensions:
>>> [[items[i][j] for i in range(len(items))] for j in range(len(items[0]))]
[[1, 4, 7], [2, 5, 8], [3, 6, 9]]
That said, the solution provided by #schwobaseggl is a good way to do it too, very pythonic.
One-liner stolen from GeeksforGeeks:
t = [[m[j][i] for j in range(len(m))] for i in range(len(m[0]))]
Can we perform replace on python list?
I have that list that is imported from csv file:
[['1', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '0', '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', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'], ['2', '0', '0', '0', '0', '0', '0', '0', '1', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0'], ['3', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0']]
fist index(mark as bold) from list above will became costumer id followed by the item that they buy, from list above we can see costumer 1 purchased item in column 12 that marked by italic.
desire out put is :
costumer 1 purchased item 12 and item 22 and so it's for costumer 2 and
3
Note that I have try used panda and not work, and I not sure how to use if statement inside the for loop.
Also, I have used rappid minner and they replaced column by column and they included the [0][0] to be replaced. Is there any other solution beside python?
Here is my code:
import csv
csvfile = open("tran.csv", 'r')
reader = csv.reader(csvfile, delimiter=',')
my_list = list(reader)
a = len(my_list[1])
b = (my_list)
x=0
y=0
for name in my_list:
print ("costummer", my_list[0][0], "buy", my_list[n][g])
update for csv writer:
csvdict = {words[0]:words[1:] for words in csv}
for x in csvdict.keys(): # iterate over the keys '1', '2', ....
products = [index+1 for index,v in enumerate(csvdict[x]) if v == '1' ] # create list of purchases where the '1's are indexed
f = open("trans.csv", 'w')
result = ("costummer", x, "buy", products)
resultstr = (','.join([str(x) for x in hasil]))#I try to convert it to str.
print (resultstr) #to check whether the conversion from list into str is working or not.
f.write(resultstr) #try to write to csv file
Here is a "starting point", there will be an extra index for the customer 1, that is something you can play around with. It might not be important. You can add a if clause to take care of it.
csv = [['1', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '0', '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', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'], ['2', '0', '0', '0', '0', '0', '0', '0', '1', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0'], ['3', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0']]
csvdict = {words[0]:words[1:] for words in csv}
for x in sorted(csvdict.keys()): # iterate over the keys '1', '2', ....
products = [index+1 for index,v in enumerate(csvdict[x]) if v == '1' ] # create list of purchases where the '1's are indexed
print "costummer", x, "purchased", ' '.join(['"item '+str(i)+'"' for i in B])
Okey, I'm currently writing Conway's Game of Life and I have reached a point that I don't know how to solve.
Edit here are the different lists I'm having trouble with:
for repeticiones in range(0,5):
nuevo_mapa = mapax
print(mapax)
if '#' in mapax:
print('Jell')
if mapax is mapa_juego:
print('God')
for y in range(0,10):
for x in range(0,10):
if mapa_juego[y][x] == '#':
viva_muerta = True
elif mapa_juego[y][x] == '0':
viva_muerta = False
for i in range(0,8):
try:
if mapa_juego[y + check_listy[i]][x + check_listx[i]] == '#':
sum += 1
except IndexError:
pass
if viva_muerta == True and sum in [0,1]:
nuevo_mapa[y][x] = '0'
elif viva_muerta == True and sum >= 4:
nuevo_mapa[y][x] = '0'
elif viva_muerta == True and sum in [2,3]:
nuevo_mapa[y][x] = '#'
elif viva_muerta == False and sum == 3:
nuevo_mapa[y][x] = '#'
sum = 0
mapa_juego = nuevo_mapa
print('\n\n')
mapax is a list of lists full of 0.
I want to do this:
nuevo_mapa = mapax
nuevo_mapa is modified
mapa_juego = nuevo_mapa
And start over and over again.
But the problem comes here, after the first iteration mapax is no more a full list of 0.
The output is something like this(I want publish it all because it's so messy and to avoid confusions)
>>> [0,0,0,0,0,0,0] First print of mapax
>>> [0,0,#,0,0,0,0] Second print of mapax and I want to avoid this.
If you see anything in the code let me know, thanks.
Edit: here are the different lists I'm having trouble with:
mapa_juego = [
['0','0','0','0','0','0','0','0','0','0',],
['0','0','0','0','0','0','0','0','0','0',],
['0','0','0','0','0','0','0','0','0','0',],
['0','0','#','0','0','0','0','0','0','0',],
['0','0','0','#','0','0','0','0','0','0',],
['0','#','#','#','0','0','0','0','0','0',],
['0','0','0','0','0','0','0','0','0','0',],
['0','0','0','0','0','0','0','0','0','0',],
['0','0','0','0','0','0','0','0','0','0',],
['0','0','0','0','0','0','0','0','0','0',]
]
mapax = [
['0','0','0','0','0','0','0','0','0','0',],
['0','0','0','0','0','0','0','0','0','0',],
['0','0','0','0','0','0','0','0','0','0',],
['0','0','0','0','0','0','0','0','0','0',],
['0','0','0','0','0','0','0','0','0','0',],
['0','0','0','0','0','0','0','0','0','0',],
['0','0','0','0','0','0','0','0','0','0',],
['0','0','0','0','0','0','0','0','0','0',],
['0','0','0','0','0','0','0','0','0','0',],
['0','0','0','0','0','0','0','0','0','0',]
]
2 Edit: Here is full code:
mapa_juego = [
['0','0','0','0','0','0','0','0','0','0',],
['0','0','0','0','0','0','0','0','0','0',],
['0','0','0','0','0','0','0','0','0','0',],
['0','0','#','0','0','0','0','0','0','0',],
['0','0','0','#','0','0','0','0','0','0',],
['0','#','#','#','0','0','0','0','0','0',],
['0','0','0','0','0','0','0','0','0','0',],
['0','0','0','0','0','0','0','0','0','0',],
['0','0','0','0','0','0','0','0','0','0',],
['0','0','0','0','0','0','0','0','0','0',]
]
mapax = [
['0','0','0','0','0','0','0','0','0','0',],
['0','0','0','0','0','0','0','0','0','0',],
['0','0','0','0','0','0','0','0','0','0',],
['0','0','0','0','0','0','0','0','0','0',],
['0','0','0','0','0','0','0','0','0','0',],
['0','0','0','0','0','0','0','0','0','0',],
['0','0','0','0','0','0','0','0','0','0',],
['0','0','0','0','0','0','0','0','0','0',],
['0','0','0','0','0','0','0','0','0','0',],
['0','0','0','0','0','0','0','0','0','0',]
]
viva_muerta = None
check_listy = [0,0,1,-1,-1,1,-1,1]
check_listx = [1,-1,0,0,1,1,-1,-1]
sum = 0
#Reglas
# Una célula muerta con exactamente 3 células vecinas vivas "nace" (es decir, al turno siguiente estará viva).
# Una célula viva con 2 ó 3 células vecinas vivas sigue viva, en otro caso muere o permanece muerta (por "soledad" o "superpoblación").
# Célula viva = True muerta = False
# for line in mapa_juego:
# print(line)
for repeticiones in range(0,5):
nuevo_mapa = mapax
print(mapax)
if '#' in mapax:
print('Jell')
if mapax is mapa_juego:
print('God')
for y in range(0,10):
for x in range(0,10):
if mapa_juego[y][x] == '#':
viva_muerta = True
elif mapa_juego[y][x] == '0':
viva_muerta = False
for i in range(0,8):
try:
if mapa_juego[y + check_listy[i]][x + check_listx[i]] == '#':
sum += 1
except IndexError:
pass
if viva_muerta == True and sum in [0,1]:
nuevo_mapa[y][x] = '0'
elif viva_muerta == True and sum >= 4:
nuevo_mapa[y][x] = '0'
elif viva_muerta == True and sum in [2,3]:
nuevo_mapa[y][x] = '#'
elif viva_muerta == False and sum == 3:
nuevo_mapa[y][x] = '#'
sum = 0
mapa_juego = nuevo_mapa
print('\n\n')
Edit 3: Real output
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '#', '0', '#', '0', '0', '0', '0', '0', '0']
['0', '0', '#', '#', '0', '0', '0', '0', '0', '0']
['0', '0', '#', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
God
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '#', '#', '0', '0', '0', '0', '0', '0']
['0', '#', '0', '#', '0', '0', '0', '0', '0', '0']
['0', '0', '#', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
God
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '#', '#', '0', '0', '0', '0', '0', '0']
['0', '#', '0', '#', '0', '0', '0', '0', '0', '0']
['0', '0', '#', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
God
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '#', '#', '0', '0', '0', '0', '0', '0']
['0', '#', '0', '#', '0', '0', '0', '0', '0', '0']
['0', '0', '#', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
God