gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 20:47:44 26 4
gpt4 key购买 nike

我有一个看起来像的列表

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com