使用Python中的itemgetter保留嵌套列表结构

时间:2018-03-04 20:40:30

标签: python list

例如,假设library(gplots) n<-10^6 sigma<- matrix(c(4,2,2,3), ncol=2) x<- rmvnorm(n, mean = c(1,2), sigma = sigma) h2d<- hist2d(x, show = FALSE, same.scale = TRUE, nbins = c(40,40)) persp(h2d$x, h2d$y, h2d$counts/n, ticktype="detailed", theta=30, phi=30, expand=0.5, shade=0.5, col="cyan", ltheta=-30,main="Bivariate Normal Distribution", zlab="f(x1,x2)",xlab="x1",ylab="x2") a = [[1, 2, 3], [2, 3, 4], [1, 2], [3, 5]]是我要提取的条目inds的索引列表。例如,

a

工作正常。但是,如果In [1]: from operator import itemgetter In [2]: itemgetter(*inds)(a) Out[2]: ([1, 2, 3], [2, 3, 4]) 只包含一个元素,那么结果就不再是(子)列表的元组:

inds

有没有办法让In [3]: itemgetter(*[0])(a) Out[3]: [1, 2, 3] 仍然返回相同的格式:itemgetter在这种情况下?我需要输出格式为后面的处理提供相同的深度,无论它包含多少元素。

0 个答案:

没有答案
相关问题