Getting the first elements per row in an array in Python?

前端 未结 3 1871
遇见更好的自我
遇见更好的自我 2020-12-16 17:19

Let\'s say i have an array of Tuples, s, in the form of:

s = ((1, 23, 34),(2, 34, 44), (3, 444, 234))

and i want to return another Tuple,

3条回答
  •  被撕碎了的回忆
    2020-12-16 17:37

    The list comprehension method given by Ignacio is the cleanest.

    Just for kicks, you could also do:

    zip(*s)[0]
    

    *s expands s into a list of arguments. So it is equivalent to

    zip( (1, 23, 34),(2, 34, 44), (3, 444, 234))
    

    And zip returns n tuples where each tuple contains the nth item from each list.

提交回复
热议问题