python - 是否可以在 Python 的一条语句中连接列表索引?

标签 python list list-comprehension

我有一个看起来像的列表

alist = [a, b, c, d, e]

我想把它传递给一个看起来像这样的函数

def callme(a, b, e):
    pass

所以,我想做类似的事情

callme(*alist[0,1,4])

有没有一种衬垫可以实现这一目标?

编辑

我想我也可以这样做(编辑不要这样做,drewk 用更好的枚举方法回答了。)

callme(*[a for a in alist if alist.index(a) in [0,1,4]])

最佳答案

使用operator.itemgetter:

from operator import itemgetter
callme(*itemgetter(0, 1, 4)(alist))

关于python - 是否可以在 Python 的一条语句中连接列表索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18166360/

相关文章:

r - 使用 rep() 创建向量列表

python - 如何获得两个双元素子列表列表之间的对称差异?

python - 从循环中创建值列表

python - 为什么 Python 在使用 from-import 时对循环导入更加严格?

python - 线程不同时运行。 (Python)

javascript - 多阶段范围验证

c# - 在列表中跟踪分数以及姓名

python - Python 中的循环编程(corecursion)——可能吗?

python - 列出目录并获取目录的名称

python - 根据特定顺序重新排序列表