Python 基于特定值访问矩阵中的元素

Python 基于特定值访问矩阵中的元素,python,matrix,element,Python,Matrix,Element,我有下面的代码,需要一些关于如何根据骰子掷出的数字访问矩阵中特定元素的建议。例如,在游戏开始时,如果滚动数为8,矩阵位置将为8。我不确定语法,尤其是我使用了稍微不同的方法来创建矩阵: 矩阵输出如下 [43, 44, 45, 46, 47, 48, 49] [42, 41, 40, 39, 38, 37, 36] [29, 30, 31, 32, 33, 34, 35] [28, 27, 26, 25, 24, 23, 22] [15, 16, 17, 18, 19, 20, 21] [14, 1

我有下面的代码,需要一些关于如何根据骰子掷出的数字访问矩阵中特定元素的建议。例如,在游戏开始时,如果滚动数为8,矩阵位置将为8。我不确定语法,尤其是我使用了稍微不同的方法来创建矩阵:

矩阵输出如下

[43, 44, 45, 46, 47, 48, 49]
[42, 41, 40, 39, 38, 37, 36]
[29, 30, 31, 32, 33, 34, 35]
[28, 27, 26, 25, 24, 23, 22]
[15, 16, 17, 18, 19, 20, 21]
[14, 13, 12, 11, 10, 9, 8]
[1, 2, 3, 4, 5, 6, 7]
注意:如果用户滚动7,它将评估第一个子列表中的第6个元素,但是如果用户滚动8,由于布局的原因,它将是下一个子列表中的最后一个元素(与第一个相反)

到目前为止,我掌握的代码如下。我唯一需要一些建议的是如何根据掷骰子的数量访问和计算矩阵中的元素:

    def RollTwoDiceP1(player1,player2):
    turn=input("Player 1, it's your turn to roll the dice: Press r to roll:>>>")
    #create two variables here and assign them random numbers
    die1=random.randint(1,6)
    die2=random.randint(1,6)
    #add the two die numbers together
    roll=die1+die2    
    #when you are printing an integer, you need to cast it into a string before you printit
    print("Player1: You rolled a:", die1, "and a", die2, "which gives you a:", roll)
    playing = False
    for i in matrix(7):
        #list=[i[0]]#this gives me the first column, or the first index in each sub list
        moves=roll
        print("You have moved", roll, "spaces to position:.......")#here I want to access the element in the list that corresponds to the number rolled. e.g. if 8 rolled, it would be on position, 8

    playerturns(player1,player2,playing)
创建网格/矩阵并显示它的代码是:

    def matrix(n): 
    grid = [[1 + i + n * j for i in range(n)] for j in range(n)] 
    for row in grid[1::2]:
        row.reverse()    
    return grid[::-1][:]

def callmatrix(player1,player2, n):
    print("*************LOADING GAME******************")
    print("Welcome:", player1,"and", player2)
    for i in matrix(n):
            print(i)
    playing = True
    playerturns(player1,player2,playing)

注意:我不想使用pandas、numpy或任何类似的东西,所以我不希望任何建议的解决方案使用非常基本的python工具/对已经使用的东西的修复来澄清一些东西,您所指的第一个子列表实际上是最后一个子列表。将所有这些列表按当前的顺序放入一个列表
l
,然后执行
l[-1]
,你就会明白我的意思。是的,谢谢-但没有回答问题或提出任何建议?好吧,列表的顺序对于如何访问其中的元素非常重要。因为每个列表都有7个项目,所以您可以使用除法查找商(这是您的行)和余数(这是您的行中的元素)。例如:如果您想要第八项:8/7=1,剩余部分为1,这意味着您想要第二个列表中的第一个元素,这有意义吗?