如何从二维字符数组中提取对角线作为字符串? [英] How do I extract the diagonals as strings from a two-dimensional array of characters?

查看:66
本文介绍了如何从二维字符数组中提取对角线作为字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试以编程方式解决下面的单词难题,作为自己的练习:

I have been trying to programmatically solve the word puzzle below as an exercise for myself:

puzzle = [
    ['t', 'r', 'o', 'l', 'l'],
    ['u', 'u', 'a', 'a', 'y'],
    ['n', 'r', 'r', 'e', 'q'],
    ['a', 'o', 'h', 'd', 'q']]
words = ["troll", "tuna", "nut", "oral", "turd", "deal", "rut", "hey", "ear"]

但我需要一点帮助来完成练习.这是我到目前为止所做的:

But I need a little help with completing the exercise. Here's what I've done so far:

我将行和列转换为单个字符串并将它们放入列表中:

I convert the rows and columns into single strings and put them into a list:

a = []

#Rows
for x in puzzle:
    a.append(''.join(x))
    a.append(''.join(x)[::-1])  #Append reversed string.

#Columns
for i in range(len(puzzle[0])):
    s = ""
    for j in range(len(puzzle)):
        s += puzzle[j][i]
    a.append(s)
    a.append(''.join(s)[::-1])  #Append reversed string.

所以现在我可以找到水平或垂直拼写的任何单词.正如您可能已经猜到的那样,下一步是遍历对角线并将它们放入列表中,但就是无法弄清楚这样做的逻辑.有人能帮我解决这个问题吗?

So now I can find any word spelt horizontally or vertically. As you might have guessed the next step is to go through the diagonals and put them into the list, but just can't figure out the logic to do it. Can anyone help me with this?

推荐答案

首先查看嵌套列表中所有元素的索引:

First, look at the indices of all the elements in the nested lists:

(0,0) (0,1) (0,2) (0,3) (0,4)
(1,0) (1,1) (1,2) (1,3) (1,4)
(2,0) (2,1) (2,2) (2,3) (2,4)
(3,0) (3,1) (3,2) (3,3) (3,4)
(4,0) (4,1) (4,2) (4,3) (4,4)

从这里您可以写下对角线上半部分的索引以了解模式:

From this you can write down the indices for the top half of the diagonals to get a sense of the pattern:

(0,4)
(0,3), (1,4)
(0,2), (1,3), (2,4)
(0,1), (1,2), (2,3), (3,4)
(0,0), (1,1), (2,2), (3,3), (4,4)

我们如何生成这些对列表?首先,让我们看一下外部循环.外部循环将遍历第一个子列表的顶部,即 (0,j) 对,其中 j 从最后一个索引开始倒计时:

How do we generate these lists of pairs? First, let's look at the outside loop. The outside loop will go across the top of the first sublist, i.e. the pairs (0,j) where j counts down from the last index:

for j in range(len(puzzle), -1, -1):
    print((0, j))

#output:
(0, 4)
(0, 3)
(0, 2)
(0, 1)
(0, 0)

然后我们需要让我们的内部循环在每一步都向下和向右移动,并且永远不要越过列表的末尾:

Then we need to have our inner loop go down and to the right on each step, and to never go past the end of the list:

for j in range(len(puzzle), -1, -1):
    for i in range(len(puzzle)):
        j += 1
        if j > len(puzzle):
             break
        print((i,j))
    print("")

输出:

(0, 4)

(0, 3)
(1, 4)

(0, 2)
(1, 3)
(2, 4)

(0, 1)
(1, 2)
(2, 3)
(3, 4)

如果您使外循环从 N..-1 开始,那么您还可以获得通过中心的对角线.之后,用类似的逻辑得到对角线的下半部分.

If you make the outer loop go from N..-1 then you can also get the diagonal that passes through the center. After that, use similar logic to get the bottom half of the diagonals.

要使对角线朝另一个方向移动,您可以再次使用相同类型的逻辑(通过查看您想要的索引,编写外循环,然后编写内循环),或者您可以反转每个子列表(把它想象成看镜像,或者在垂直线上反射一个二维数组!当你这样做时,所有的对角线都将穿过相反"方向的元素).

To get the diagonals going in the other direction you can again use the same kind of logic (by looking at what indices you want, writing the outer loop, then writing the inner loop), or you can just reverse each of the sublists (think of it as looking at the mirror image, or reflecting a 2d array over a vertical line! when you do this, all the diagonals will be going through the elements in the "opposite" direction).

这篇关于如何从二维字符数组中提取对角线作为字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆