在 Python 中获取数组中每行的第一个元素? [英] Getting the first elements per row in an array in Python?

查看:25
本文介绍了在 Python 中获取数组中每行的第一个元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个元组数组,s,形式如下:

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

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

并且我想返回另一个元组 t,它由每行的第一个元素组成:

and i want to return another Tuple, t, consisting of the first element per row:

t = (1, 2, 3)

哪种方法最有效?我当然可以遍历 s,但有没有更灵活的方法呢?

Which would be the most efficient method to do this? I could of course just iterate through s, but is there any slicker way of doing it?

推荐答案

没有

t = tuple(x[0] for x in s)

这篇关于在 Python 中获取数组中每行的第一个元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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