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']]
Related
titleValues = {'Movie 1 (1998)': ['0', '1', '0', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'], 'Movie 2 (1994)': ['0', '1', '0', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '1', '1', '0', '0']}
categories={'unknown': '0', 'Action': '1', 'Adventure': '2', 'Animation': '3', "Children's": '4', 'Comedy': '5', 'Crime': '6', 'Documentary': '7', 'Drama': '8', 'Fantasy': '9', 'Film-Noir': '10', 'Horror': '11', 'Musical': '12', 'Mystery': '13', 'Romance': '14', 'Sci-Fi': '15', 'Thriller': '16', 'War': '17', 'Western': '18'}
selectedCol = 1
titles=[]
for key, value in titleValues.items():
for num in value:
if num == '1':
valIdx = value.index(num)
if valIdx == selectedCol:
titles.append(key)
else:
continue
print(titles)
output:
['Movie 1 (1998)', 'Movie 1 (1998)', 'Movie 2 (1994)', 'Movie 2 (1994)', 'Movie 2 (1994)', 'Movie 2 (1994)']
I think it appears 6 times because of the six '1' occurrences. However, how can I only obtain two names as for both lists '1' appears at index 1.
['Movie 1 (1998)', 'Movie 2 (1994)']
only when titleValues contains a one, put key once in a list:
titles = [k for k,v in zip(titleValues.keys(),titleValues.values()) if '1' in v]
result will be
print(titles)
# ['Movie 1 (1998)', 'Movie 2 (1994)']
Explanation
Create two iterables (keys, values)
print(titleValues.keys())
#dict_keys(['Movie 1 (1998)', 'Movie 2 (1994)'])
print(titleValues.values())
#dict_values([['0', '1', '0', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'], ['0', '1', '0', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '1', '1', '0', '0']])
zip, is a function to iterate element-wise and simultanouesly over two iterables
print(list(zip(titleValues.keys(),titleValues.values())))
# [('Movie 1 (1998)', ['0', '1', '0', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0']), ('Movie 2 (1994)', ['0', '1', '0', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '1', '1', '0', '0'])]
in a for-comprehension you can access elements of both iterables (specified variable name: k for element in first iterable and v for element in second)
This question already has answers here:
How to to read a matrix from a given file?
(8 answers)
Closed 3 years ago.
I'm trying to read a matrix from a given text file, put it in a dictionary and manipulate it later, however when I try to access the elements, "," shows as an element and it's really messing up my indexing
I've tried to use the split function and it seems to be reading it nicely, but ',' still appears as an element
def loadboard():
f = open("game.txt", "r")
A=f.readline()
B=f.readline()
C=f.readline()
board=[]
board = [[int(num) for num in line.split(',')] for line in f]
print(board)
game = {
"player1" : A,
"player2" :B,
"who" : C,
"board" : board
}
f.close()
return(game)
this is my text file
A
B
C
0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0
0,0,1,2,1,0,0,0
0,0,1,2,2,2,0,0
0,0,1,2,1,0,0,0
0,0,0,2,1,0,0,0
0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0
this is what I see when i print the board
Out[242]:
['0,0,0,0,0,0,0,0\n',
'0,0,0,0,0,0,0,0 \n',
'0,0,1,2,1,0,0,0 \n',
'0,0,1,2,2,2,0,0\n',
'0,0,1,2,1,0,0,0 \n',
'0,0,0,2,1,0,0,0\n',
'0,0,0,0,0,0,0,0\n',
'0,0,0,0,0,0,0,0']
when i try to access the second element which should be a zero again, i get ','
board[0][1]
Out[243]: ','
This work well for me:
board = []
with open('toto.txt', 'r') as f:
for row in f.read().strip().split("\n")[3:]:
board.append(row.split(","))
for line in board:
print (line)
print "board[0] : " + str(board[0])
print "board[0][1] : " + str(board[0][1])
Output:
(venv) C:\Users\hlupo\Documents\SoTest>python test.py
['0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '1', '2', '1', '0', '0', '0']
['0', '0', '1', '2', '2', '2', '0', '0']
['0', '0', '1', '2', '1', '0', '0', '0']
['0', '0', '0', '2', '1', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0']
board[0] : ['0', '0', '0', '0', '0', '0', '0', '0']
board[0][1] : 0
Try this:
with open('game.txt', 'r') as f:
l = [[int(num) for num in line.split(',')] for line in f]
print(l)
This will do:
matrix = []
with open('game.txt','r') as f:
for row in f.read().strip().split("\n")[3:]:
matrix.append(row.split(","))
print(matrix)
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