2 回答
TA贡献1828条经验 获得超13个赞
您可以使用列表理解:
arr=[[[f'{i}{j}{k}' for k in item]for j in y]for i in x]
输出:
arr
[[['00a', '00b'], ['01a', '01b'], ['02a', '02b']],
[['10a', '10b'], ['11a', '11b'], ['12a', '12b']]]
itertools使用and的另一种选择numpy:
import itertools
import numpy as np
prod=itertools.product(x,y,item)
prod=list(map(lambda x: f'{x[0]}{x[1]}{x[2]}',prod))
np.array(prod).reshape(len(x),len(y),len(item))
输出:
array([[['00a', '00b'],
['01a', '01b'],
['02a', '02b']],
[['10a', '10b'],
['11a', '11b'],
['12a', '12b']]], dtype='<U3')
TA贡献1906条经验 获得超10个赞
这是另一个没有使用 numpy 循环的解决方案:
import numpy as np
x=np.array([0,1]).astype(str)
y=np.array([0,1,2]).astype(str)
items=np.array(['a','b'])
temp= np.core.defchararray.add(y[:,np.newaxis], items)
result = np.core.defchararray.add(x[:,np.newaxis,np.newaxis], temp)
print(result)
输出:
[[['00a' '00b']
['01a' '01b']
['02a' '02b']]
[['10a' '10b']
['11a' '11b']
['12a' '12b']]]
添加回答
举报
