📌  相关文章
📜  python从列表中返回特定元素-Python代码示例

📅  最后修改于: 2022-03-11 14:45:57.828000             🧑  作者: Mango

代码示例1
# Basic syntax:
# Using list comprehension:
selected_elements = [your_list[index] for index in indices]

# Example usage:
# Say you want to return the elements at indices 1, 4, 6
your_list = [5, 7, 23, 42, 11, 17, 27]
indices = [1, 4, 6]

# Running:
[your_list[index] for index in indices] # Would return:
--> [7, 11, 27]