[英]How to "extend" the array in Python?
I would like to extend the 2d array in python in some way.我想以某种方式扩展 python 中的二维数组。
No loops没有循环
Fe if it is: Fe 如果是:
[[255, 255, 255],
[255, 255, 255],
[255, 255, 255]]
I would say I want to extend it by the factor of 2 and get like this:我会说我想将它扩展 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]]
and etc, if by 4 factor.等等,如果是 4 因子。
Is there any function?有没有function?
You can do this without numpy just with list comprehension but its complicated:您可以在没有 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)
the output: 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]]
Here is a solution using numpy.这是使用 numpy 的解决方案。 You didn't provide example for N=4, so I guessed:
您没有提供 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]]
You can create new bigger array with zeros你可以用零创建新的更大的数组
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)
and then you can use for
-loops with zip()
and range(0, new_h, factor)
to get value and its new position然后您可以使用带有
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
gives给
[[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]]
If you use different value instead of 0
in range
then you can get offset
如果您在
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
gives:给出:
[[ 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]]
Working code工作代码
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)
BTW: You could even use factor_x
, factor_y
with different values.顺便说一句:您甚至可以使用具有不同值的
factor_x
、 factor_y
。
for example例如
factor_x = 4
factor_y = 2
in code在代码中
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)
gives给
[[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]]
Extending is simple with slicing:使用切片进行扩展很简单:
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.