将numpy mgrid与可变数量的索引一起使用 [英] Using numpy mgrid with a variable number of indices

查看:173
本文介绍了将numpy mgrid与可变数量的索引一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将numpy.mgrid与可变数量的索引一起使用?我在github上找不到任何示例,除了硬编码值以外,其他任何人都使用此代码.

How do you use numpy.mgrid with a variable number of indices? I can't find any examples on github of anyone using this with anything but hardcoded values.

import numpy as np
np.mgrid[1:10, 1:10] # this works fine

x = (1, 10)
np.mgrid[x[0]:x[1], x[0]:x[1]] # hardcoded

xs = [(1,10)] * 10
np.mgrid[*xs????] # I can't get anything to work here

推荐答案

这似乎可行:

np.mgrid[[slice(i,j) for i,j in [(1,10)]*10]]

尽管*10太大了

它是基于这个事实

np.mgrid[slice(1,10),slice(1,10)]  # same as
np.mgrid[1:10,1:10]

这篇关于将numpy mgrid与可变数量的索引一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆