Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在使用如下列表:
L = [['a', 4], ['b', 2], ['c', 13]]
我想为每组列表中第二个位置的所有数字生成一个新列表:
L = [4, 2, 13]
python中是否有任何快捷方式可以获取上述列表?
使用列表推导:
>>> L = [['a', 4], ['b', 2], ['c', 13]] >>> print [i[1] for i in L] [4, 2, 13]
这将访问每个列表中的第二个项目(请记住索引从零开始,因此1获取第二个索引)
1