简体   繁体   English

如何在 python 中创建一个二维数组,形成一个菱形?

[英]How to create a 2D array in python forming a diamond?

How can I create a 2d array in python without using any libraries (no numpy or dataframes)?如何在不使用任何库(没有 numpy 或数据帧)的情况下在 python 中创建二维数组? That is, only using vanilla python.也就是说,仅使用香草 python。

It must result in an by n full diamond, where even-shaped diamonds are two hashes wide and odd-sized diamonds are one hash wide at their points, for example:它必须产生一个由 n 个完整的菱形,其中偶数形状的菱形是两个散列宽,奇数大小的菱形在它们的点上是一个 hash 宽,例如:

diamond(5) returns
    [[' ', ' ', '#', ' ', ' '],
     [' ', '#', '#', '#', ' '],
     ['#', '#', '#', '#', '#'],
     [' ', '#', '#', '#', ' '],
     [' ', ' ', '#', ' ', ' ']]
diamond(6) returns
    [[' ', ' ', '#', '#', ' ', ' '],
     [' ', '#', '#', '#', '#', ' '],
     ['#', '#', '#', '#', '#', '#'],
     ['#', '#', '#', '#', '#', '#'],
     [' ', '#', '#', '#', '#', ' '],
     [' ', ' ', '#', '#', ' ', ' ']]

So far, the best I could come up with was:到目前为止,我能想到的最好的方法是:

HASH = "#"
SPACE = " "
def diamond(n):
    if n % 2 == 0:
        return [
            [HASH if (i+1 == n//2) or (i == n//2) or (j == n//2) or (j+1 == n//2)
             or (i > 0 and j>0 and i!=j and (i%j ==0 or j%i ==0))
             else SPACE for i in range(n)
            ] 
            for j in range(n)
        ]
    return [
        [HASH if (i == n//2) or (j == n//2)
         or (i > 0 and j>0 and  (i%j ==0 or j%i ==0))
         else SPACE for i in range(n) 
        ] for j in range(n)
    ]

but it does not work well yet.但它还不能很好地工作。

I also have seen this answer: 2d array diamond shape of 1's of size x .我也看到了这个答案: 2d array diamond shape of 1's of size x But it does use numpy.但它确实使用了 numpy。

You could do it like this:你可以这样做:

def diamond(n):
    if n% 2:
        result = [[' ']*i+['#']*(n-(2*i))+[' ']*i for i in range(n//2, -1,-1)]
        return result + result[:-1][::-1]

    result = [[' ']*i+['#']*(n-(2*i))+[' ']*i for i in range(n//2-1, -1, -1)]
    return result + result[::-1]

diamond(5) : diamond(5)

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

diamond(6) : diamond(6)

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

try this, you start by adding the middle full line and reduce 1 column every time you step down a row until you reach the bottom (n//2), then you flip the list and add it to the original list, creating the full diamond试试这个,你首先添加中间的整行,每次你下一行直到到达底部(n//2)时减少一列,然后你翻转列表并将其添加到原始列表中,创建完整的钻石

def diamond(n):
    # define the full line in the middle
    hash_line = ["#"]*n
    # this will hold the diamond shape
    d_list = []
    # start by adding the hash_line
    d_list.append(hash_line)
    # add one half of the diamond, reducing one column each time you step down
    for i in range(n//2):
        line = hash_line.copy()
        line[:i+1] = [' ']*(i+1)
        line[-1*(i+1):] = [' ']*(i+1)
        d_list.append(line)
    # now you have the bottom half of the diamond
    # if n is even flip d_list and add it in the beginning of the original d_list
    if n%2 == 0:
        final = d_list[::-1]+d_list
        return final[1:-1]
    # if n is odd flip d_list without the hash_line which is held in the first index
    return d_list[1:][::-1]+d_list

d = diamond(5)
print(d)
d = diamond(6)
print(d)

After realising that a diamond simply is a ball wrt.在意识到钻石只是一个球之后。 L1 metric you can put '#' when distance from the centre is half of the size of the array, ie |x-center_x|当距中心的距离是数组大小的一半时,您可以使用 L1 度量,即 |x-center_x| + |y-center_y| + |y-中心_y| < half_screen which gives a one liner solution < half_screen 提供单层解决方案

def diamond(n):
    return [['#' if (abs(i-(n+1)/2) + abs(j- (n+1)/2)) <  (n+1)/2 else ' '
             for i in range(1, n+1)] for j in range(1, n+1)]

and then接着

print('\n'.join(map(str,diamond(5))))


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



print('\n'.join(map(str,diamond(6))))


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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM