gpt4 book ai didi

python - 根据python中的索引列表获取列表中的元素

转载 作者:行者123 更新时间:2023-11-28 20:10:32 25 4
gpt4 key购买 nike

我有一个索引列表,类似于:

b=[0,2]  

和一个元素列表:

a = ['elem0','elem1','elem2'] 

我需要一个列表,它由 a 中的元素和 b
中的索引组成(在这个例子中:['elem0','elem2'])

最佳答案

使用 list comprehension :

[a[i] for i in b]

关于python - 根据python中的索引列表获取列表中的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7011636/

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