gpt4 book ai didi

python - 在Python中从字母数字数组中提取一行

转载 作者:行者123 更新时间:2023-12-01 05:08:37 25 4
gpt4 key购买 nike

我在 python 中有这个数组,我想将以 p 结尾的行提取到一个新数组中。

array([[ 0,   6,   p],
[ 1, 7, m],
[ 2, 10, m],
[ 3, 4, p],
[ 4, 1, m],
[ 5, 10, p],
[ 6, 6, m],
[ 7, 4, m],
[ 8, 6, p],
[ 9, 7, p]])

最佳答案

简单的方法是使用 numpy bool 索引,如下所示

a = numpy.array(a) # ensure its a numpy array
print a[a[:,-1]=="p"]

分解它

mask = a[:,-1]=="p" #gives an array of True/False that is the same as the array, based on if last char is "p"
a[mask] #indexes into the original array returning only the rows that end with "p"

关于python - 在Python中从字母数字数组中提取一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24618980/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com