I searched the forums and tried a couple of the answers but it does not seem to work for me.
So my problem is I have a list of lists which has 0 and 'M' in at different places every time you run the program.The position of the 'M' is randomized.
So I wanna get the indexes of all the positions of where the 'M' is in the list.
I have tried this:
hiddenfield = [[0, 0, 0, 0, 0, 'M', 0, 0, 0, 'M'],
[0, 0, 0, 0, 0, 'M', 0, 'M', 'M', 'M'],
[0, 0, 'M', 0, 'M', 0, 0, 'M', 0, 'M'],
[0, 0, 0, 0, 'M', 0, 0, 0, 0, 0],
[0, 0, 'M', 0, 'M', 0, 0, 0, 0, 0],
[0, 0, 0, 'M', 0, 'M', 'M', 0, 0, 0],
[0, 0, 0, 'M', 0, 0, 0, 'M', 0, 0],
[0, 0, 0, 'M', 0, 0, 0, 0, 0, 0],
[0, 'M', 'M', 0, 'M', 0, 0, 0, 0, 0],
['M', 0, 0, 'M', 0, 0, 0, 0, 'M', 0]]
position_of_m = []
for i in hiddenfield:
position_of_m.append(i.index('M'))
print(position_of_m)
and all i get is the first index of all of the lists like this:
[5, 5, 2, 4, 2, 3, 3, 3, 1, 0]
how do i solve this?
EDIT:
Ok guys so i got some help from you earlier. But right now i am banging my head against the table.
The code works 'kinda'. But i don't want the last numbers of the list to add to the first number of another list. Hard for me to explain so i´ll show you further down!
heres the code:
hiddenfield = [[0, 0, 0, 0, 0, 'M', 0, 0, 0, 'M'],
[0, 0, 0, 'M', 0, 0, 0, 'M', 'M', 'M'],
[0, 0, 'M', 0, 'M', 0, 0, 'M', 0, 'M'],
[0, 0, 0, 0, 'M', 0, 0, 0, 0, 0],
[0, 0, 'M', 0, 'M', 0, 0, 0, 0, 0],
[0, 0, 0, 'M', 0, 'M', 'M', 0, 0, 0],
['M', 0, 0, 'M', 0, 0, 0, 'M', 0, 0],
[0, 0, 0, 'M', 0, 0, 0, 0, 0, 0],
[0, 'M', 'M', 0, 'M', 0, 0, 0, 0, 'M'],
['M', 0, 0, 'M', 0, 0, 0, 0, 'M', 0]]
def update_numbers(x, y, hiddenfield):
try:
if hiddenfield[x][y] != 'M':
hiddenfield[x][y] += 1
except IndexError:
pass
def numbersinhiddefield(indexes):
indexes = [[i,j] for i,row in enumerate(indexes) for j,elem in enumerate(row) if elem == 'M']
loop = 0
while loop < len(indexes):
update_numbers(indexes[loop][0]+1,indexes[loop][1], hiddenfield)
update_numbers(indexes[loop][0]-1,indexes[loop][1], hiddenfield)
update_numbers(indexes[loop][0],indexes[loop][1]+1, hiddenfield)
update_numbers(indexes[loop][0],indexes[loop][1]-1, hiddenfield)
update_numbers(indexes[loop][0]+1,indexes[loop][1]+1, hiddenfield)
update_numbers(indexes[loop][0]-1,indexes[loop][1]-1, hiddenfield)
update_numbers(indexes[loop][0]+1,indexes[loop][1]-1, hiddenfield)
update_numbers(indexes[loop][0]-1,indexes[loop][1]+1, hiddenfield)
loop += 1
def showMineFieldHidden(hiddenfield):
border = list(range(0,len(hiddenfield)))
row = [' ']+border
i = 0
for rows in [border]+hiddenfield:
print(row[i], end=' ')
i += 1
for lines in rows:
print(lines, end=' ')
print()
numbersinhiddefield(hiddenfield)
showMineFieldHidden(hiddenfield)
But the thing i get out isn't just right:
0 1 2 3 4 5 6 7 8 9
0 0 0 1 1 2 M 2 2 4 M
1 0 1 2 M 3 2 3 M M M
2 0 1 M 4 M 2 2 M 5 M
3 0 2 2 5 M 3 1 1 2 1
4 0 1 M 4 M 4 2 1 0 0
5 1 2 3 M 4 M M 2 1 1
6 M 1 3 M 4 2 3 M 1 1
7 2 3 4 M 3 1 1 1 2 2
8 2 M M 4 M 1 0 1 2 M
9 M 3 3 M 3 2 1 1 M 4 <-- this is supposed to be a 2.
^
this is supposed to be a 1 and the one next to it a zero
I think it is adding from the borders to the lower list?
Would appreciate som help!