更改列表python中多个项目的值

我有一个嵌套列表:


Table=[['','','','',''],

       ['','','','',''],

       ['','','','',''],

       ['','','','',''],

       ['','','','',''],

       ['','','','','']]

我随机放置了一些值Table,现在我想在这些值的 2D 邻居中放置其他东西。例如:


Table=[['','','','',''],

       ['','','','',''],

       ['','','','',''],

       ['','','value','',''],

       ['','','','',''],

       ['','','','','']]

然后我想补充:


Table=[['','','','',''],

       ['','','','',''],

       ['','','1','',''],

       ['','1','value','1',''],

       ['','','1','',''],

       ['','','','','']]

下面是我所有的代码,我不知道为什么,但它会以任何其他格式接受它,抱歉:/

def add_nukes():

    pos=j.index('nuke')

    if "nuke" not in j[0]:j[pos+1]='1'

        if "nuke" not in j[-1]: 

            j[pos-1] = "1"

            board[pos][i-1]="1"

            board[i+1][pos]="1"


import random


size=150


if size%2==1:

    size+=1


board = [[" "]*size for i in range(size)] 

bombs = 25


all_cells = ["nuke"] * bombs + [" "] * (size - bombs) 


random.shuffle(all_cells)


board = [all_cells[i:i+10] for i in range(0, size, 10)]


count=0


for j in board:

    for i in range(len(j)):

        count+=1

        if "nuke" in j[i]:

            add_nukes()

        elif "nuke" in j[i]:

            add_nukes()


for item in board:

    print item 


慕标5832272
浏览 226回答 2
2回答

墨色风雨

中的任何值Table都由其x和y坐标唯一标识,即第 2 列(x == 1因为 0 索引)和第 3 行 ( y == 2) 中的元素是Table[y][x] == Table[2][1]。任何单元格的四个直接邻居A是x一个远离AORy一个远离的单元格A。如果A是Table[y][x],则邻居是[Table[y - 1][x], Table[y + 1][x], Table[y, x - 1], Table[y, x + 1]]。

慕尼黑的夜晚无繁华

就像@Aurel Bílý 提到的那样,您需要为特定情况添加四个相邻坐标:[Table[y - 1][x], Table[y + 1][x], Table[y, x - 1], Table[y, x + 1]].为此,您必须首先确保这些坐标有效并且不会引发IndexError异常。确保此坐标有效后,您可以安全地将它们添加到表中。下面的代码演示了这一点:Table=[['','','','',''],&nbsp; &nbsp; &nbsp; &nbsp;['','','','',''],&nbsp; &nbsp; &nbsp; &nbsp;['','','','',''],&nbsp; &nbsp; &nbsp; &nbsp;['','','value','',''],&nbsp; &nbsp; &nbsp; &nbsp;['','','','',''],&nbsp; &nbsp; &nbsp; &nbsp;['','','','','']]def isInBounds(Table,x,y):&nbsp; &nbsp; return 0 <= x < len(Table) and 0 <= y < len(Table[0])def addValue(Table,x,y,value):&nbsp; &nbsp; if isInBounds(Table,x,y):&nbsp; &nbsp; &nbsp; &nbsp; Table[x][y] = valuedef addValuesAround(Table,x,y,value):&nbsp; &nbsp; addValue(Table,x-1,y,value)&nbsp; &nbsp; addValue(Table,x,y-1,value)&nbsp; &nbsp; addValue(Table,x+1,y,value)&nbsp; &nbsp; addValue(Table,x,y+1,value)addValuesAround(Table,3,2,1)for elem in Table:&nbsp; &nbsp; print(elem)这将返回:['', '', '', '', '']['', '', '', '', '']['', '', 1, '', '']['', 1, 'value', 1, '']['', '', 1, '', '']['', '', '', '', '']编辑:我想我明白了,使用我们的两个代码。请务必更改print函数的语法,因为您使用的是 Python 2.7 而我使用的是 Python 3.6:import randomdef isInBounds(Table,x,y):&nbsp; &nbsp; return 0 <= x < len(Table) and 0 <= y < len(Table[0])def addValue(Table,x,y,value):&nbsp; &nbsp; if isInBounds(Table,x,y):&nbsp; &nbsp; &nbsp; &nbsp; Table[x][y] = valuedef addValuesAround(Table,x,y,value):&nbsp; &nbsp; addValue(Table,x-1,y,value)&nbsp; &nbsp; addValue(Table,x,y-1,value)&nbsp; &nbsp; addValue(Table,x+1,y,value)&nbsp; &nbsp; addValue(Table,x,y+1,value)size=150if size%2==1:&nbsp; &nbsp; size+=1board = [[" " for i in range(size)] for i in range(size)]&nbsp;bombs = 25all_cells = ["nuke"] * bombs + [" "] * (size - bombs)&nbsp;random.shuffle(all_cells)board = [all_cells[i:i+10] for i in range(0, size, 10)]count=0for i in range(len(board)):&nbsp; &nbsp; for j in range(len(board[i])):&nbsp; &nbsp; &nbsp; &nbsp;if board[i][j] == 'nuke':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;addValuesAround(board,i,j,"1")for item in board:&nbsp; &nbsp; print(item)这将给出一个像这样的板实例:[' ', ' ', ' ', ' ', '1', ' ', '1', ' ', '1', ' '][' ', ' ', ' ', '1', 'nuke', '1', 'nuke', '1', 'nuke', '1']['1', ' ', ' ', ' ', '1', ' ', '1', ' ', '1', '1']['nuke', '1', '1', '1', 'nuke', '1', ' ', ' ', '1', 'nuke']['1', '1', 'nuke', '1', '1', ' ', '1', ' ', ' ', '1'][' ', ' ', '1', ' ', ' ', '1', 'nuke', '1', ' ', ' '][' ', ' ', '1', ' ', ' ', '1', '1', ' ', ' ', ' '][' ', '1', 'nuke', '1', '1', 'nuke', '1', ' ', ' ', ' ']['1', 'nuke', '1', ' ', '1', '1', '1', ' ', '1', ' '][' ', '1', 'nuke', '1', 'nuke', '1', 'nuke', '1', 'nuke', '1']['1', 'nuke', '1', ' ', '1', ' ', '1', ' ', '1', ' '][' ', '1', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '][' ', ' ', '1', ' ', ' ', ' ', ' ', ' ', ' ', ' '][' ', '1', 'nuke', '1', ' ', '1', ' ', '1', ' ', ' '][' ', ' ', '1', ' ', '1', 'nuke', '1', 'nuke', '1', ' ']
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python