Python 使用列表中的值筛选元组

Python 使用列表中的值筛选元组,python,Python,我有以下数据集: test_set = ("The sun in the sky", "The sun in the light", "Do not blame it on moonlight", "Do not blame it on sunshine") 如果根据列表筛选此测试,我现在想做什么。很像 y = [0,1] test_set_2 = test_set[y] 但是,这给了我以下错误: Traceback (most recent call last): File "/Users

我有以下数据集:

test_set = ("The sun in the sky", "The sun in the light", "Do not blame it on moonlight", "Do not blame it on sunshine")
如果根据列表筛选此测试,我现在想做什么。很像

y = [0,1]
test_set_2 = test_set[y]
但是,这给了我以下错误:

Traceback (most recent call last):
File "/Users/marcvanderpeet/PycharmProjects/untitled/test3.py", line 5, in <module>
test_set_2 = test_set[y]
TypeError: tuple indices must be integers, not list

您可以运行索引列表并从大列表中提取相关项

test_set = ("The sun in the sky", "The sun in the light", "Do not blame it on moonlight", "Do not blame it on sunshine")

y = [0,1]
test_set_2 =  tuple([test_set[index] for index in y])

print test_set_2

您可以运行索引列表并从大列表中提取相关项

test_set = ("The sun in the sky", "The sun in the light", "Do not blame it on moonlight", "Do not blame it on sunshine")

y = [0,1]
test_set_2 =  tuple([test_set[index] for index in y])

print test_set_2