简体   繁体   English

从元组列表中的项目构建2D numpy数组

[英]Build 2D numpy array from items in list of tuples

Given a python list of tuples such as: 给定一个元组的python列表,例如:

test = [(1, 'string1', 47.9, -112.8, 6400.0),
        (2, 'string2', 29.7, -90.8, 11.0),
        (3, 'string3', 30.8, -99.1, 1644.0),
        (4, 'string4', 45.8, -110.9, 7500.0),
        (5, 'string5', 43.9, -69.8, 25.0)]

What is the most efficient way to build a 2D numpy array using the 3rd and 4th items from each tuple? 使用每个元组的第3和第4个项构建2D numpy数组的最有效方法是什么?

Desired output is: 所需的输出是:

array([[47.9, 29.7, 30.8, 45.8, 43.9],
       [-112.8, -90.8, -99.1, -110.9, -69.8]]) 

You can prepare the data outside numpy using a list comprehension which selects the 3rd and 4th item. 您可以使用列表理解来选择numpy之外的数据,该列表选择第三和第四项。 Then you only need to transpose the resulting array: 然后,您只需要转置结果数组:

np.array([x[2:4] for x in test]).T

zip the list, slice it using itertools.islice : zip列表,使用itertools.isliceslice

from itertools import islice

np.array(list(islice(zip(*test), 2, 4)))
# array([[  47.9,   29.7,   30.8,   45.8,   43.9],
#        [-112.8,  -90.8,  -99.1, -110.9,  -69.8]])

You could transform the list of tuples directly into an array then use slicing and transposing to get the desired output: 您可以将元组列表直接转换为数组,然后使用切片和转置来获得所需的输出:

import numpy as np

test = [(1, 'string1', 47.9, -112.8, 6400.0),
        (2, 'string2', 29.7, -90.8, 11.0),
        (3, 'string3', 30.8, -99.1, 1644.0),
        (4, 'string4', 45.8, -110.9, 7500.0),
        (5, 'string5', 43.9, -69.8, 25.0)]

arr = np.array(test, dtype=object)
result = arr[:, 2:4].T.astype(np.float32)
print(result)

Output 输出量

[[  47.9   29.7   30.8   45.8   43.9]
 [-112.8  -90.8  -99.1 -110.9  -69.8]]

Note that after doing arr = np.array(test) everything is done at numpy level. 请注意,在执行arr = np.array(test)所有操作都在numpy级别完成。

the first list is: 第一个列表是:

the_first = [item[2] for item in test]

and second is: 第二个是:

 second = [item[3] for item in test]

and the result is: 结果是:

 result = np.array([the_first, second])

You can try this: 您可以尝试以下方法:

import numpy as np

test = [(1, 'string1', 47.9, -112.8, 6400.0), (2, 'string2', 29.7, -90.8, 11.0), (3, 'string3', 30.8, -99.1, 1644.0), (4, 'string4', 45.8, -110.9, 7500.0), (5, 'string5', 43.9, -69.8, 25.0)]

result = np.array([(item[3], item[4]) for item in test]).T
print(result)

# array([[-112.8,  -90.8,  -99.1, -110.9,  -69.8],
#       [6400. ,   11. , 1644. , 7500. ,   25. ]])

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

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