二维填充二维阵列

时间:2017-07-21 08:24:29

标签: python

我试图用这样的方式填充一个具有唯一整数的数组(除了0):

# range(0, 37)
[[ 0,  0,  0, 36, 19, 20, 21],
 [ 0,  0, 35, 18,  7,  8, 22],
 [ 0, 34, 17,  6,  1,  9, 23],
 [33, 16,  5,  0,  2, 10, 24],
 [32, 15,  4,  3, 11, 25,  0],
 [31, 14, 13, 12, 26,  0,  0],
 [30, 29, 28, 27,  0,  0,  0]]

(这表示十六进制网格为二维数组)

如何在填充前确定2D数组(宽度和高度)的大小? 如何确定下一个整数的坐标?

感谢@Błotosmętek我现在能够弄清楚数组的大小:

def array_size(n):
    k = 0
    while n > 0:
        k += 1
        n -= 6 * k
    return 2 * k + 1

3 个答案:

答案 0 :(得分:0)

您可以使用np.put将值放在目标索引处。这是我的解决方案,可以根据需要进行更改

SELECT categories.*
FROM categories
WHERE categories.parent = '1'
AND categories.visible = 'yes'
AND (
        (SELECT count(*) FROM products WHERE products.categorie  = categories.id AND visible = 'yes') > 0
        OR
        (SELECT count(*) FROM categories as sub WHERE sub.parent = categories.id) > 0
    )
ORDER BY `position` DESC, `name` ASC;

你给了这个数组

import numpy as np

a = np.empty(49)
b = np.arange(0, 48, 1)
ind = np.arange(len(a))
np.put(a, ind, b)
a1 = np.reshape(a, (7, 7), order='F')
print(a1)

答案 1 :(得分:0)

计算尺寸相对容易:注意你最小的"六角形"是一个元素,然后在它周围添加6个元素,然后是12,然后是18 ...围绕中心的每个循环添加6 * k个数字,其中k = 1,2,...并且k个循环的维度是2 * k + 1 by 2 * K + 1。所以,你的功能应该是这样的:

def array_size(n):
    k = 0
    while n > 0:
        k += 1
        n -= 6 * k
    return 2 * k + 1

所以例如array_size(36) == 7

现在,正确放置你的号码是另一回事......

N=36
n=array_size(N)
t = [ ['.'] * n for _ in range(n) ]

x = y = (n-1)//2
d = ((0, 1), (-1, 1), (-1, 0), (0, -1), (1, -1), (1, 0))
k = 0
t[y][x] = 0
y -= 1
x += 1
t[y][x] = 1
i = 1
while i<N:
    nx = x + d[k][0]
    ny = y + d[k][1]
    px = x + d[(k+1) % 6][0]
    py = y + d[(k+1) % 6][1]
    if t[py][px] != '.':
        x, y = nx, ny
        i += 1
        t[y][x] = i
    else:
        k = (k+1) % 6

for row in t:
    print("".join(("{:>3} ".format(p if p != '.' else 0) for p in row)))
print()

答案 2 :(得分:0)

回答我自己的问题:

为了弄清楚如何以给定方式放置值,我们需要找出每个项目的坐标delta。

为了便于解释,我将使用n = 60元素。

要做到这一点,我们需要知道我们将得到的数组的大小,这可以通过@Błotosmętek建议实现:

def array_size(n):
k = 0
while n > 0:
    k += 1
    n -= 6 * k
return 2 * k + 1

array_size = array_size(n)

(顺便说一下,非常感谢单挑局。) 现在,我们需要数组中心的坐标,这很简单:

# This is (4, 4), the exact coordinates we need for said amount of items (60)
array_center = (array_size // 2, array_size // 2)

接下来,我们获得增量。我不确定这是最好的方法,但无论如何:

def get_coordinates_delta(n):
    x = [0]
    x_ones = 1
    x_zeroes = 1
    while len(x) <= n:
        x += ([1] * x_ones + [0] * x_zeroes + [-1] * (x_ones + 1) + [0] * x_zeroes)
        x_ones += 2
        x_zeroes += 1

    y = [0, -1]
    y_ones = 2
    y_zeroes = 1
    while len(y) <= n:
        y += ([1] * y_ones + [0] * y_zeroes + [-1] * (y_ones + 1) + [0] * y_zeroes)
        y_ones += 2
        y_zeroes += 1
    return [x for x in zip(x[:n], y[:n])]

coordinates_delta = get_coordinates_delta(n)

现在是有趣的部分,我们将所有内容都放在网格上:

def fill_matrix():
    matrix = [[0] * array_size for _ in range(array_size)]
    x, y = array_center
    for item, (dx, dy) in zip(range(n), coordinates_delta):
        x += dx
        y += dy
        matrix[y][x] = item
    return matrix

matrix = fill_matrix()    

瞧,

for line in matrix:
    print(line)

告诉我们:

[ 0,  0,  0,  0,  0, 37, 38, 39, 40]
[ 0,  0,  0, 59, 36, 19, 20, 21, 41]
[ 0,  0, 58, 35, 18,  7,  8, 22, 42]
[ 0, 57, 34, 17,  6,  1,  9, 23, 43]
[56, 33, 16,  5,  0,  2, 10, 24, 44]
[55, 32, 15,  4,  3, 11, 25, 45,  0]
[54, 31, 14, 13, 12, 26, 46,  0,  0]
[53, 30, 29, 28, 27, 47,  0,  0,  0]
[52, 51, 50, 49, 48,  0,  0,  0,  0]