我有一个叫GTY的字典,其中有100个键,都是
的组合range(10,101,10)
(即:(10,10),(10,20),...(90,100),(100,100))。 我想对该字典进行“解压”,并得到一个10x10矩阵,其列数为0到100(带有步骤10),行的列数为0到100(带有步骤10)。
我希望通过运行一种简单的拆包技术来做到这一点
np.array([GTY[i] for i in itertools.combinations(range(10,101,10), 2)])
,但它在单列而不是10x10矩阵中产生数据。是否有一种有效的简单方法来获取矩阵,其中col为键元组nr1,行为键元组nr2?
非常感谢!
答案 0 :(得分:0)
您可以使用numpy生成11x11的数组,并使用product生成所有组合:
import numpy as np
z = itertools.product(range(0,101,10),repeat= 2)
z1 = np.array([i for i in z]).reshape(11,11,2)
for i in range(11):
print(f'z[{i}] = {z1[i]}')
输出:
z [0] = [[0 0] [0 10] [0 20] [0 30] [0 40] [0 50] [0 60] [0 70] [0 80] [0 90] [0 100]]
z [1] = [[10 0] [10 10] [10 20] [10 30] [10 40] [10 50] [10 60] [10 70] [10 80] [10 90] [10 100]]
z [2] = [[20 0] [20 10] [20 20] [20 30] [20 40] [20 50] [20 60] [20 70] [20 80] [20 90] [20 100]]
z [3] = [[30 0] [30 10] [30 20] [30 30] [30 40] [30 50] [30 60] [30 70] [30 80] [30 90] [30 100]]
z [4] = [[40 0] [40 10] [40 20] [40 30] [40 40] [40 50] [40 60] [40 70] [40 80] [40 90] [40 100]]
z [5] = [[50 0] [50 10] [50 20] [50 30] [50 40] [50 50] [50 60] [50 70] [50 80] [50 90] [50 100]]
z [6] = [[60 0] [60 10] [60 20] [60 30] [60 40] [60 50] [60 60] [60 70] [60 80] [60 90] [60 100]]
z [7] = [[70 0] [70 10] [70 20] [70 30] [70 40] [70 50] [70 60] [70 70] [70 80] [70 90] [70 100]]
z [8] = [[80 0] [80 10] [80 20] [80 30] [80 40] [80 50] [80 60] [80 70] [80 80] [80 90] [80 100]]
z [9] = [[90 0] [90 10] [90 20] [90 30] [90 40] [90 50] [90 60] [90 70] [90 80] [90 90] [90 100]]
z [10] = [[100 0] [100 10] [100 20] [100 30] [100 40] [100 50] [100 60] [100 70] [100 80] [100 90] [100 100]]