在Python 2.x中访问数组列表中的元素 [英] Accessing an elements within a array of list of arrays in Python 2.x

查看:109
本文介绍了在Python 2.x中访问数组列表中的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑使用numpy.genfromtxt()提取的以下数据集:

Considering the following dataset extracted using numpy.genfromtxt():

    data[0:3]
array([('T', 2,  8, 3, 5, 1,  8, 13, 0, 6,  6, 10, 8, 0, 8, 0,  8),
       ('I', 5, 12, 3, 7, 2, 10,  5, 5, 4, 13,  3, 9, 2, 8, 4, 10),
       ('D', 4, 11, 6, 8, 6, 10,  6, 2, 6, 10,  3, 7, 3, 7, 3,  9)], 
      dtype=[('f0', 'S1'), ('f1', '<i8'), ('f2', '<i8'), ('f3', '<i8'), ('f4', '<i8'), ('f5', '<i8'), ('f6', '<i8'), ('f7', '<i8'), ('f8', '<i8'), ('f9', '<i8'), ('f10', '<i8'), ('f11', '<i8'), ('f12', '<i8'), ('f13', '<i8'), ('f14', '<i8'), ('f15', '<i8'), ('f16', '<i8')])

我正在尝试使用以下代码从前两个数组中检索字母,但它给出的是第一个完整的数组,而不是每个数组中的第一个元素.

I am trying to retrieve the letters from the first 2 arrays using the following code but it's giving the first complete array instead of the first element from each array.

data[:2][0]

推荐答案

问题是数组的内容是元组.因此,当您要求data[:2][0]时,它将返回元组列表中的第0个项目,而不是每个元组的第0个项目.使用以下代码段:

The issue is that the contents of the array are tuples. So, when you ask for data[:2][0] it returns the 0'th item in a list of tuples, and not the 0th item of each tuple. Use the following snippet:

output = []
for i in range(2):
    output += data[:2][i][0]

希望我能帮上忙.

这篇关于在Python 2.x中访问数组列表中的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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