小编H. *_*bri的帖子

itertools python字典值的产物

我有一个Python字典,字符串作为键,numpy数组作为值:

dictionary = {'first': np.array([1, 2]), 'second': np.array([3, 4])}
Run Code Online (Sandbox Code Playgroud)

现在我想用它itertools product来创建以下列表:

requested = [(1, 3), (1, 4), (2, 3), (2, 4)]
Run Code Online (Sandbox Code Playgroud)

通常在传递的项目product是numpy数组时完成.

当我执行以下操作时:

list(product(list(dictionary.values())))
Run Code Online (Sandbox Code Playgroud)

我得到以下输出:

[(array([3, 4]),), (array([1, 2]),)] 
Run Code Online (Sandbox Code Playgroud)

python dictionary python-itertools python-3.x

6
推荐指数
1
解决办法
3521
查看次数

标签 统计

dictionary ×1

python ×1

python-3.x ×1

python-itertools ×1