简体   繁体   English

根据其中包含的任意值拆分 numpy 数组

[英]Split numpy array based on arbitrary values contained in it

I'd like to split this (numpy) array:我想拆分这个(numpy)数组:

[ 4 0 1 3 2 3 8 10 11 4 12 13 15 14 4 16 18 19 17 4 20 21 23 22]

into something like this:变成这样的东西:

[[0 1 3 2] [8 10 11] [12 13 15 14] [16 18 19 17] [20 21 23 22]]

The first number 4 , indicates how many subsequent numbers I need to group [0, 1, 3, 2] , the next one would be 3 , so the next values to group would be [8, 10, 11] , and so on.第一个数字4表示我需要将多少个后续数字分组[0, 1, 3, 2] ,下一个数字是3 ,因此要分组的下一个值是[8, 10, 11] ,依此类推.

Is there an efficient way to do this?有没有一种有效的方法来做到这一点? I need to handle arrays that will have thousands or hundreds of thousands of values.我需要处理具有数千或数十万个值的 arrays。

Thanks!谢谢!

I don't know if numpy has something for your problem.我不知道 numpy 是否能解决您的问题。

This is simple solution but probably for big list it may not be so efficient这是一个简单的解决方案,但可能对于大列表它可能不是那么有效

data = [4, 0, 1, 3, 2, 3, 8, 10, 11, 4, 12, 13, 15, 14, 4, 16, 18, 19, 17, 4, 20, 21, 23, 22]

results = []

while data:
    number = data.pop(0)
    results.append(data[:number])
    data = data[number:]

print(results)

Result:结果:

[[0, 1, 3, 2], [8, 10, 11], [12, 13, 15, 14], [16, 18, 19, 17], [20, 21, 23, 22]]

BTW: maybe using indexes (with less slicing data) can be faster.顺便说一句:也许使用索引(切片数据较少)可以更快。

data = [4, 0, 1, 3, 2, 3, 8, 10, 11, 4, 12, 13, 15, 14, 4, 16, 18, 19, 17, 4, 20, 21, 23, 22]

results = []
index = 0
length = len(data)

while index < length:
    number = data[index]
    index += 1
    results.append(data[index:index+number])
    index += number

print(results)

EDIT: second version works also with numpy array:编辑:第二个版本也适用于 numpy 阵列:

import numpy as np

data = np.asarray([4, 0, 1, 3, 2, 3, 8, 10, 11, 4, 12, 13, 15, 14, 4, 16, 18, 19, 17, 4, 20, 21, 23, 22])

results = []
index = 0
length = len(data)

while index < length:
    number = data[index]
    index += 1
    results.append(data[index:index+number])
    index += number

results = np.asarray(results)
print(results)

Result:结果:

[array([0, 1, 3, 2]) array([ 8, 10, 11]) array([12, 13, 15, 14]) array([16, 18, 19, 17]) array([20, 21, 23, 22])]

You could do something like this using list comprehension你可以使用列表理解来做这样的事情

lst = [4, 0, 1, 3, 3, 8, 10, 11, 4, 12, 13, 15, 14, 4, 16, 18, 19, 17, 4, 20, 21, 23, 22]

marker = lst[0]

split_array = [[lst[x] for x in range(c, c+marker) if x < len(lst)] for c in range(1,len(lst),marker)]

>>> split_array
[[0, 1, 3, 3], [8, 10, 11, 4], [12, 13, 15, 14], [4, 16, 18, 19], [17, 4, 20, 21], [23, 22]]

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

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