简体   繁体   English

如何删除NumPy数组中的数字?

[英]How to remove numbers in a NumPy array?

I want to know how I can remove the numbers resulted by the code from the original list, based on How to find larger numbers of any selected number in a series in an ascending order in NumPy? 我想知道如何删除原始列表中的代码所产生的数字,基于如何在NumPy中按升序查找系列中任何选定数字的更大数字?

EXAMPLE: Series of random numbers: 4, 8, 5, 9, 3, 11, 17, 19, 9, 15, 16 示例:随机数系列:4,8,5,9,3,11,17,19,9,15,16

X=4, Then: X = 4,然后:

4, 8, 9, 11, 17, 19 (We call this ROUND 1) 4,8,9,11,17,19(我们称之为第1轮)

The numbers resulted in ROUND 1 are removed and the operation goes for the new list as follows: 将删除导致ROUND 1的数字,操作将按以下方式进入新列表:

5, 3, 9, 15, 16 5,3,9,15,16

So if we apply the code to the new list, it will generate new results: 因此,如果我们将代码应用于新列表,它将生成新结果:

X=5, Then: 5, 9, 15, 16 X = 5,然后:5,9,15,16

Edit: 编辑:

a = np.array([4, 8, 5, 9, 3, 11, 17, 19, 9, 15, 16])
X = 4
withreps = np.maximum.accumulate(a[np.argmax(a==X):])
result = withreps[np.where(np.diff(withreps, prepend=withreps[0]-1))]
result
# array([ 4,  8,  9, 11, 17, 19])

Check out the following code 请查看以下代码

import numpy as np

rand_nums = np.array([4, 8, 5, 9, 3, 11, 17, 19, 9, 15, 16])
round_one = np.array([4, 8, 9, 11, 17, 19])
res = np.setdiff1d(rand_nums, round_one)
print(res)

# output 
>>> array([ 3,  5, 15, 16])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM