从数组列表中的每个数组中获取一个元素?

时间:2013-10-26 04:56:21

标签: python

我有一个数组列表,如下所示:

[array([ 3.04347826,  3.04347826,  3.04347826,  3.04347826,  3.04347826,
    3.04347826,  3.04347826,  3.04347826,  3.04347826,  3.04347826,
    3.04347826,  3.04347826,  3.04347826,  3.04347826,  3.04347826,
    3.04347826,  3.04347826,  3.04347826,  3.04347826,  3.04347826,
    3.04347826,  3.04347826,  3.04347826]), array([ 3.5,  3.5,  3.5,  3.5,  3.5,  3.5,  3.5,  3.5,  3.5,  3.5,  3.5,
    3.5,  3.5,  3.5,  3.5,  3.5,  3.5,  3.5,  3.5,  3.5,  3.5,  3.5,
    3.5,  3.5,  3.5,  3.5,  3.5,  3.5,  3.5,  3.5,  3.5,  3.5,  3.5,
    3.5,  3.5,  3.5,  3.5,  3.5]), array([ 3.08823529,  3.08823529,  3.08823529,  3.08823529,  3.08823529,
    3.08823529,  3.08823529,  3.08823529,  3.08823529,  3.08823529,
    3.08823529,  3.08823529,  3.08823529,  3.08823529,  3.08823529,
    3.08823529,  3.08823529,  3.08823529,  3.08823529,  3.08823529,
    3.08823529,  3.08823529,  3.08823529,  3.08823529,  3.08823529,
    3.08823529,  3.08823529,  3.08823529,  3.08823529,  3.08823529,
    3.08823529,  3.08823529,  3.08823529,  3.08823529]), array([ 3.84848485,  3.84848485,  3.84848485,  3.84848485,  3.84848485,
    3.84848485,  3.84848485,  3.84848485,  3.84848485,  3.84848485,
    3.84848485,  3.84848485,  3.84848485,  3.84848485,  3.84848485,
    3.84848485,  3.84848485,  3.84848485,  3.84848485,  3.84848485,
    3.84848485,  3.84848485,  3.84848485,  3.8484848....

如何只获取每个数组的第一个元素?

1 个答案:

答案 0 :(得分:4)

这可能有效,假设数组是numpy.array

first_elements = [e[0] for e in list1]

其中:

list1是您的数组列表

first_elements是一个列表,其中包含list1

中每个数组的第一个元素

以更清晰的方式:

first_elements = []
for arr in list1:
    first_elements.append(arr[0])