簡體   English   中英

如何在 python 中創建一個二維數組,形成一個菱形?

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

如何在不使用任何庫(沒有 numpy 或數據幀)的情況下在 python 中創建二維數組? 也就是說,僅使用香草 python。

它必須產生一個由 n 個完整的菱形,其中偶數形狀的菱形是兩個散列寬,奇數大小的菱形在它們的點上是一個 hash 寬,例如:

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

到目前為止,我能想到的最好的方法是:

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)
    ]

但它還不能很好地工作。

我也看到了這個答案: 2d array diamond shape of 1's of size x 但它確實使用了 numpy。

你可以這樣做:

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(6)

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

試試這個,你首先添加中間的整行,每次你下一行直到到達底部(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)

在意識到鑽石只是一個球之后。 當距中心的距離是數組大小的一半時,您可以使用 L1 度量,即 |x-center_x| + |y-中心_y| < 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)]

接着

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