多变量Python列表理解

2024-04-23 07:41:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从另一个列表的索引中获取一个具有特定输出的列表, 例如:

L = [(0, 1, 2, 3, 4, 5), (6, 7, 8, 9, 10,...etc), (...etc)]
multiple_index = [entry[0, 3, 4] for entry in L] 
#----> I know this specific code is wrong

如果上面的代码可以输出:

^{pr2}$

如果可能的话,我希望主列表中每个索引中的各个子索引按所示分组,我想知道我可以使用什么代码来正确地实现这一点,谢谢。在

编辑: 另外,我如何将其格式化为行显示,我使用.writelines和一个单独的输出行将它们输出到一个文本文件,再次感谢!在


Tags: 代码in列表forindexisetccode
3条回答

使用^{}

from operator import itemgetter

multiple_index = map(itemgetter(0, 3, 4), L)

或者在列表理解中:

^{pr2}$

你对这个感兴趣吗?在

L = [(0, 1, 2, 3, 4, 5), (6, 7, 8, 9, 10,...etc), (...etc)]
multiple_index = [(entry[0], entry[3], entry[4]) for entry in L] 
#----> I know this specific code is wrong

这里有一个选择:

L = [(0, 1, 2, 3, 4, 5), (6, 7, 8, 9, 10, 11), (11, 12, 13, 14, 15, 16)]
multiple_index = [(entry[0], entry[3], entry[4]) for entry in L] 

或使用^{}

^{pr2}$

相关问题 更多 >