Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何获得字典形式的对角线列表,其中键表示第一项的索引?_Python - Fatal编程技术网

Python 如何获得字典形式的对角线列表,其中键表示第一项的索引?

Python 如何获得字典形式的对角线列表,其中键表示第一项的索引?,python,Python,我正在制作游戏《奥赛罗》,棋盘如下: L= [['.','.','.','.'], ['.','B','W','.'], ['.','W','B','.'], ['.','.','.','.']] {(1,1):["B"],(1,2):['W','W'],(2,2):['B']} 我的逻辑的一部分是围绕一个函数展开的,该函数返回一个字典,其中的值是对角线列表,一个元组的键包含两个数字;列表中第一项的索引。例如,下图的左对角线如下所示: {(0, 1): ['.', '.'

我正在制作游戏《奥赛罗》,棋盘如下:

L= [['.','.','.','.'],
   ['.','B','W','.'],
   ['.','W','B','.'],
   ['.','.','.','.']]
 {(1,1):["B"],(1,2):['W','W'],(2,2):['B']}
我的逻辑的一部分是围绕一个函数展开的,该函数返回一个字典,其中的值是对角线列表,一个元组的键包含两个数字;列表中第一项的索引。例如,下图的左对角线如下所示:

{(0, 1): ['.', '.'], (1, 3): ['.', 'B', '.'], (2, 3): ['.', '.'], (3, 3): ['.'], (0, 3): ['.', 'W', 'W', '.'], (0, 2): ['.', 'B', '.'], (0, 0): ['.']}
我使用下面的代码实现了这一点

def get_Rightdiagonally_adjacent(Li,ir,ic):
     L = []
     for i in range(max(len(Li),len(Li[0]))):
     try:
         L.append(Li[ir+i][ic+i])
    except:
        return L
    return L

def get_Leftdiagonally_adjacent(Li,ir,ic):
    L = []
    for i in range(max(len(Li),len(Li[0]))):
        if ic - i < 0:
            return L
        try:
            L.append(Li[ir+i][ic-i])
        except:
            return L
    return L


def get_Leftdiagonals(M: "2D list") -> list:
    Ld = {}
    for i in range(1,len(M)):
        Ld[(i,len(M[0])-1)] = get_Leftdiagonally_adjacent(M,i,len(M[0])-1)
    for i in range(len(M[0])):
        Ld[0,i] = get_Leftdiagonally_adjacent(M,0,i)

    return Ld

def get_Rightdiagonals(M: "2D list") -> list:
    Ld = {}
    for i in range(len(M[0])):
        Ld[(0,i)] = get_Rightdiagonally_adjacent(M,0,i)
    for i in range(1,len(M)):
        Ld[(i,0)] = get_Rightdiagonally_adjacent(M,i,0)
    return Ld

get_Leftdiagonals(L)

请注意元组是如何表示列表中第一项相对于原始板的索引的。在尝试了几天之后,我在这方面失败得很惨,如果有任何帮助,我将不胜感激

您说如果不弄乱索引,就无法删除空字符串。您可以采取的一种方法是将偏移量与每个值一起存储,以便保留索引。例如:

def get_Rightdiagonally_discs(Li,ir,ic):
    # Get the unstripped diagonal
    L = get_Rightdiagonally_adjacent(Li, ir, ic)
    # Return pairs of the offset from ir/ic and value for locations with discs only
    return [(i, disc) for i, disc in enumerate(L) if disc != '.']

因此,对于原始代码,其右对角线开始于
(3,3)
,值
[','W','W','.]
与偏移配对,然后剥离
s,返回
[(1,'W'),(2,'W')]
它将您限制在带有圆盘的元素上,并识别它们的偏移量,以便您可以获得它们沿对角线下落的完整信息。这样行吗?

你的字典是什么样子的,你希望它们是什么样子的。另外,从第二行到最后一行的表达式
Ld[(1,0)]
正确吗?还有,对于这样的电路板,[',',',','][','B','W','B','B',您所说的“我不能删除空格并更改字典的键以使其工作”是什么意思字典看起来像这样{(0,1):[],(1,3):['B',B'],(2,3):[],(3,3):[],(0,3):['W',W'],(0,2):['B'],(0,0):[]但是,我需要它们看起来像这样{(1,3):['B','B'],(1,2):['W','W'],(1,1):['B']}我不能从列表中删除“.”并为磁盘维护正确的索引。如果我键入了错误,它应该是Ld[(I,0)]