[英]How to "extend" the array in Python?
我想以某种方式扩展 python 中的二维数组。
没有循环
Fe 如果是:
[[255, 255, 255],
[255, 255, 255],
[255, 255, 255]]
我会说我想将它扩展 2 倍并得到这样的结果:
[[255, 0, 255, 0, 255, 0],
[0, 0, 0, 0, 0, 0],
[255, 0, 255, 0, 255, 0],
[0, 0, 0, 0, 0, 0],
[255, 0, 255, 0, 255, 0],
[0, 0, 0, 0, 0, 0]]
等等,如果是 4 因子。
有没有function?
您可以在没有 numpy 的情况下执行此操作,只需使用列表理解,但它很复杂:
lst = [[255, 255, 255],
[255, 255, 255],
[255, 255, 255]]
extend_list = [ [lst[j // 2][i // 2] if j % 2 == 0 and i % 2 == 0 else 0 for i in range( 2 * (len(lst[j // 2])) )] if j != len(2 * lst) else [0 for _ in range( (2 * len(lst)) -1)] for j in range(2 * (len(lst)) )]
print(extend_list)
output:
[[255, 0, 255, 0, 255, 0], [0, 0, 0, 0, 0, 0], [255, 0, 255, 0, 255, 0], [0, 0, 0], [255, 0, 255, 0, 255, 0], [0, 0, 0, 0, 0, 0]]
这是使用 numpy 的解决方案。 您没有提供 N=4 的示例,所以我猜想:
import numpy as np
arr = np.array([[255, 255, 255], [255, 255, 255], [255, 255, 255]])
factor = 4
print(arr)
nx, ny = arr.shape
if nx != ny:
raise Exception("Array is not square")
step = 2 + factor//2 - 1
stop = nx * step
print('stop:', stop)
print('step:', step)
for x in range(1,stop,step):
print()
nx, ny = arr.shape
print('x:', x)
value = [[0]*nx]*(factor//2)
print('Inserting columns:', value)
arr = np.insert(arr, x, value, axis=1)
nx, ny = arr.shape
print(arr)
value = [[0]*ny]*(factor//2)
print('Inserting rows:', value)
arr = np.insert(arr, x, value, axis=0)
print(arr)
[[255 255 255]
[255 255 255]
[255 255 255]]
stop: 9
step: 3
x: 1
Inserting columns: [[0, 0, 0], [0, 0, 0]]
[[255 0 0 255 255]
[255 0 0 255 255]
[255 0 0 255 255]]
Inserting rows: [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
[[255 0 0 255 255]
[ 0 0 0 0 0]
[ 0 0 0 0 0]
[255 0 0 255 255]
[255 0 0 255 255]]
x: 4
Inserting columns: [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
[[255 0 0 255 0 0 255]
[ 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0]
[255 0 0 255 0 0 255]
[255 0 0 255 0 0 255]]
Inserting rows: [[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]]
[[255 0 0 255 0 0 255]
[ 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0]
[255 0 0 255 0 0 255]
[ 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0]
[255 0 0 255 0 0 255]]
x: 7
Inserting columns: [[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]]
[[255 0 0 255 0 0 255 0 0]
[ 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0]
[255 0 0 255 0 0 255 0 0]
[ 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0]
[255 0 0 255 0 0 255 0 0]]
Inserting rows: [[0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0]]
[[255 0 0 255 0 0 255 0 0]
[ 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0]
[255 0 0 255 0 0 255 0 0]
[ 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0]
[255 0 0 255 0 0 255 0 0]
[ 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0]]
你可以用零创建新的更大的数组
factor = 2
h, w = arr.shape[:2]
new_w = h*factor
new_h = h*factor
new_arr = np.zeros((new_h, new_w), np.uint8)
然后您可以使用带有zip()
和range(0, new_h, factor)
的for
-loops 来获取价值及其新的 position
for row, y in zip(arr, range(0, new_h, factor)):
for value, x in zip(row, range(0, new_w, factor)):
new_arr[y,x] = value
给
[[255 0 255 0 255 0]
[ 0 0 0 0 0 0]
[255 0 255 0 255 0]
[ 0 0 0 0 0 0]
[255 0 255 0 255 0]
[ 0 0 0 0 0 0]]
如果您在range
使用不同的值而不是0
,那么您可以获得offset
offset_y = 1
offset_x = 1
for row, y in zip(arr, range(offset_y, new_h, factor)):
for value, x in zip(row, range(offset_x, new_w, factor)):
new_arr[y,x] = value
给出:
[[ 0 0 0 0 0 0]
[ 0 255 0 255 0 255]
[ 0 0 0 0 0 0]
[ 0 255 0 255 0 255]
[ 0 0 0 0 0 0]
[ 0 255 0 255 0 255]]
工作代码
import numpy as np
arr = np.array([[255, 255, 255],
[255, 255, 255],
[255, 255, 255]]
)
factor = 2
h, w = arr.shape[:2]
new_w = h*factor
new_h = h*factor
new_arr = np.zeros((new_h, new_w), np.uint8)
offset_x = 0
offset_y = 0
for row, y in zip(arr, range(offset_y, new_h, factor)):
#print(row, y)
for value, x in zip(row, range(offset_x, new_w, factor)):
#print(y, x, value)
new_arr[y,x] = value
print(new_arr)
顺便说一句:您甚至可以使用具有不同值的factor_x
、 factor_y
。
例如
factor_x = 4
factor_y = 2
在代码中
import numpy as np
arr = np.array([[255, 255, 255],
[255, 255, 255],
[255, 255, 255]]
)
factor_x = 4
factor_y = 2
h, w = arr.shape[:2]
new_w = h*factor_x
new_h = h*factor_y
new_arr = np.zeros((new_h, new_w), np.uint8)
offset_x = 0
offset_y = 0
for row, y in zip(arr, range(offset_y, new_h, factor_y)):
#print(row, y)
for value, x in zip(row, range(offset_x, new_w, factor_x)):
#print(y, x, value)
new_arr[y,x] = value
print(new_arr)
给
[[255 0 0 0 255 0 0 0 255 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0]
[255 0 0 0 255 0 0 0 255 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0]
[255 0 0 0 255 0 0 0 255 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0]]
使用切片进行扩展很简单:
array = np.array([[255, 255, 255], [255, 255, 255], [255, 255, 255]])
factor = 2
extended_array = np.zeros((array.shape[0]*factor, array.shape[1]*factor))
extended_array[::factor,::factor] = array
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.