python - 如何访问列表列表中每个列表的第一个元素

标签 python

例子:

numbers = ['1','2','3']
letters = ['a','b','c']

我想得到 [1,a] 作为结果。是的,我可以循环遍历它,但我想知道是否有一种快速的单行方法可以做到这一点。

编辑编辑!!!!

我在描述问题时犯了一个可怕的错误。

我有权访问组合列表(问题列表的列表):

list_of_lists = [ numbers, letters]

等于:

[ ['1','2','3'],['a','b','c']]

抱歉造成混淆。最终结果仍然是相同的,这将是 ['1','a']。

最佳答案

试试list comprehension :

# (numbers, letters) can be replaced with `list_of_lists` 
>>> [ x[0] for x in (numbers, letters) ] 
['1', 'a']

关于python - 如何访问列表列表中每个列表的第一个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4821363/

相关文章:

python - 如何在单击 n 次后禁用所有单选按钮?

python - 如何触发模型更新它链接到的列表

python - 来自 Firefox bookmark.html 源代码的 YouTube video_id [几乎存在]

python - 如何将 CSS 存储在 Django 类中?

python - 嵌套列表迭代

python - 如何在 Python 中访问父类(super class)的类属性?

python - urllib2 通过代理发送 Post 数据

python - 如何通过 render_template 将渲染图传递给 html 文件?

python - 给定一个字符串,如何返回 json 路径?

c++ - C/C++ Python 解释器