用短数字分割列表 [英] Splitting lists by short numbers

查看:58
本文介绍了用短数字分割列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用NumPy在图形上查找交点,但是isClose每个交点返回多个值

I'm using NumPy to find intersections on a graph, but isClose returns multiple values per intersection

因此,我将尝试查找其平均值.但是首先,我想隔离相似的值.我觉得这也是一项有用的技能.

So, I'm going to try to find their averages. But first, I want to isolate the similar values. This is also a useful skill I feel.

我有一个名为idx的交集的x值列表,看起来像这样

I have a list of the x values for the intersection called idx that looks like this

[-8.67735471 -8.63727455 -8.59719439 -5.5511022  -5.51102204 -5.47094188
 -5.43086172 -2.4248497  -2.38476954 -2.34468938 -2.30460922  0.74148297
  0.78156313  0.82164329  3.86773547  3.90781563  3.94789579  3.98797595
  7.03406814  7.0741483   7.11422846]

并且我想将其分成每个由相似数字组成的列表.

and I want to separate it out into lists each comprised of the similar numbers.

这是我到目前为止所拥有的:

this is what I have so far:

n = 0
for i in range(len(idx)):
    try:
        if (idx[n]-idx[n-1])<0.5:
            sdx.append(idx[n-1])
        else:
            print(sdx)
            sdx = []
    except:
        sdx.append(idx[n-1])
    n = n+1

它在大多数情况下都起作用,但它忘记了一些数字:

It works for the most part but it forgets some numbers:

[-8.6773547094188377, -8.6372745490981959]
[-5.5511022044088181, -5.5110220440881763, -5.4709418837675354]
[-2.4248496993987976, -2.3847695390781567, -2.3446893787575149]
[0.7414829659318638, 0.78156312625250379]
[3.8677354709418825, 3.9078156312625243, 3.9478957915831661]

也许有一种更有效的方法来做到这一点,有人知道吗?

Theres probably a more efficient way to do this, does anyone know of one?

推荐答案

考虑到您有一个numpy数组,可以使用

Considering you have a numpy array, you can use np.split, splitting where the difference is > .5:

import numpy as np
x = np.array([-8.67735471, -8.63727455, -8.59719439, -5.5511022, -5.51102204, -5.47094188,
     -5.43086172, -2.4248497, -2.38476954, -2.34468938, -2.30460922, 0.74148297,
     0.78156313, 0.82164329, 3.86773547, 3.90781563, 3.94789579, 3.98797595,
     7.03406814, 7.0741483])


print np.split(x, np.where(np.diff(x) > .5)[0] + 1)

[array([-8.67735471, -8.63727455, -8.59719439]), array([-5.5511022 , -5.51102204, -5.47094188, -5.43086172]), array([-2.4248497 , -2.38476954, -2.34468938, -2.30460922]), array([ 0.74148297,  0.78156313,  0.82164329]), array([ 3.86773547,  3.90781563,  3.94789579,  3.98797595]), array([ 7.03406814,  7.0741483 ])]

np.where(np.diff(x) > .5)[0]返回以下元素不符合np.diff(x) > .5)条件的索引:

np.where(np.diff(x) > .5)[0] returns the index where the following element does not meet the np.diff(x) > .5) condition:

In [6]: np.where(np.diff(x) > .5)[0]
Out[6]: array([ 2,  6, 10, 13, 17])

+ 1向每个索引加1:

In [12]: np.where(np.diff(x) > .5)[0] + 1
Out[12]: array([ 3,  7, 11, 14, 18])

然后将[ 3, 7, 11, 14, 18]传递给np.split将元素拆分为子数组x[:3], x[3:7],x[7:11] ...

Then passing [ 3, 7, 11, 14, 18] to np.split splits the elements into subarrays, x[:3], x[3:7],x[7:11] ...

这篇关于用短数字分割列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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