Python数组的快速子集 [英] Quick Subsetting of Python Array
本文介绍了Python数组的快速子集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
从R到Python,我明白了为什么这么多人喜欢Python来进行数据科学.R的一个有用功能是快速子集.例如:
Coming from R to Python and I see why so many people love Python for data science. One useful feature of R is the quick subsetting. For example:
my_data = c(11,34,67,134,45,8,99,3543,1)
my_subset = c(3,8,1,6,4)
print(my_data[my_subset])
[1] 67 3543 11 8 134
一个人可以以编程方式生成满足各种条件的子集,并使用一条指令将数据过滤到该子集.如何在python中做到这一点?
One can programmatically generate subsets that satisfy various conditions and filter the data to that subset with a single instruction. How does one accomplish this in python?
推荐答案
我将继续指出明显的一线:
I'll go ahead and point out the obvious one-liner:
data = (11,34,67,134,45,8,99,3543,1)
indices = (3,8,1,6,4)
subset = [data[i] for i in indices]
print(subset)
输出:
[134, 1, 34, 99, 45]
这篇关于Python数组的快速子集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文