gpt4 book ai didi

python - 根据值将数组拆分为多个新数组

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

我需要将绘制蓝色图形的数组拆分为多个新数组。每当数组中的数字值从负数变为正数或反之时,它应该将该数字保存在一个新数组中。所以结果是多个数组,要么只有正数,要么只有负数。该代码并不意味着仅将正数和负数组合在 2 个不同的数组中。重要的是(对于这个例子)我最终得到了 3 个不同的数组(第一个:只有负数,第二个:只有正数,第三个:再次只有负数)。

我很高兴能得到任何帮助。谢谢!

蓝线图的图像

1

在这种情况下:

Difference (blue line) =  [-20.2 -19.7 -19.2 -18.9 -18.8 -18.8 -18.9 -18.9 -18.9 -18.9 -18.9 -18.9 -19.  -19.1 -19.1 -18.9 -18.5 -18.3 -18.9 -20.8 -24.1 -27.2 -28.1 -24.6  19.1  63.4 104.2 143.8 140.9 120.3  91.7  64.5  46.4  38.2  39.   47.8  63.3  82.9 103.5 122.1 136.4 147.1 155.3 162.5 169.7 177.  184.4 191.8 199.2 207.5 217.7 230.7 246.3 260.2 266.7 260.3 237.5 203.1 164.1 127.3  98.   75.3  56.7  39.9  22.7   5.5 -11.2 -26.5 -40.4 -54.9 -72.7 -96.5 -79.3 -67.2 -59.  -53.  -48.1 -44.  -40.7 -37.9 -35.6 -33.6 -31.9 -30.3 -28.7 -27.1 -25.7 -24.6 -23.8 -23.4 -23.  -22.7 -22.3 -21.9 -21.4 -20.8]

此示例的 3 个结果数组应该是:

Array1 = [-20.2 -19.7 -19.2 -18.9 -18.8 -18.8 -18.9 -18.9 -18.9 -18.9 -18.9 -18.9 -19.  -19.1 -19.1 -18.9 -18.5 -18.3 -18.9 -20.8 -24.1 -27.2 -28.1 -24.6]

Array2 = [19.1 63.4 104.2 143.8 140.9 120.3 91.7 64.5 46.4 38.2 39. 47.8 63.3 82.9 103.5 122.1 136.4 147.1 155.3 162.5 169.7 177. 184.4 191.8 199.2 207.5 217.7 230.7 246.3 260.2 266.7 260.3 237.5 203.1 164.1 127.3 98. 75.3 56.7 39.9 22.7 5.5]

Array3 = [-11.2 -26.5 -40.4 -54.9 -72.7 -96.5 -79.3 -67.2 -59. -53. -48.1 -44. -40.7 -37.9 -35.6 -33.6 -31.9 -30.3 -28.7 -27.1 -25.7 -24.6 -23.8 -23.4 -23. -22.7 -22.3 -21.9 -21.4 -20.8]

最佳答案

如果我没理解错的话,您想将数组分组为正/负:

from itertools import groupby


l = [-20.2,-19.7,-19.2,-18.9,-18.8,-18.8,-18.9,-18.9,-18.9,-18.9,-18.9,-18.9,-19.,-19.1,-19.1,-18.9,-18.5,-18.3,-18.9,-20.8,-24.1,-27.2,-28.1,-24.6,19.1,63.4,104.2,143.8,140.9,120.3,91.7,64.5,46.4,38.2,39.,47.8,63.3,82.9,103.5,122.1,136.4,147.1,155.3,162.5,169.7,177.,184.4,191.8,199.2,207.5,217.7,230.7,246.3,260.2,266.7,260.3,237.5,203.1,164.1,127.3,98.,75.3,56.7,39.9,22.7,5.5,-11.2,-26.5,-40.4,-54.9,-72.7,-96.5,-79.3,-67.2,-59.,-53.,-48.1,-44.,-40.7,-37.9,-35.6,-33.6,-31.9,-30.3,-28.7,-27.1,-25.7,-24.6,-23.8,-23.4,-23.,-22.7,-22.3,-21.9,-21.4,-20.8]

out = []
for _, g in groupby(l, lambda x: x<0):
out.append(list(g))

# print the lists:
for subl in out:
print(subl)

打印:

[-20.2, -19.7, -19.2, -18.9, -18.8, -18.8, -18.9, -18.9, -18.9, -18.9, -18.9, -18.9, -19.0, -19.1, -19.1, -18.9, -18.5, -18.3, -18.9, -20.8, -24.1, -27.2, -28.1, -24.6]
[19.1, 63.4, 104.2, 143.8, 140.9, 120.3, 91.7, 64.5, 46.4, 38.2, 39.0, 47.8, 63.3, 82.9, 103.5, 122.1, 136.4, 147.1, 155.3, 162.5, 169.7, 177.0, 184.4, 191.8, 199.2, 207.5, 217.7, 230.7, 246.3, 260.2, 266.7, 260.3, 237.5, 203.1, 164.1, 127.3, 98.0, 75.3, 56.7, 39.9, 22.7, 5.5]
[-11.2, -26.5, -40.4, -54.9, -72.7, -96.5, -79.3, -67.2, -59.0, -53.0, -48.1, -44.0, -40.7, -37.9, -35.6, -33.6, -31.9, -30.3, -28.7, -27.1, -25.7, -24.6, -23.8, -23.4, -23.0, -22.7, -22.3, -21.9, -21.4, -20.8]

关于python - 根据值将数组拆分为多个新数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64331578/

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