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']]]
添加回答
舉報