Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从与第三个列表中包含的另一个列表的项具有相同索引的列表中提取值_Python_Python 3.x_List_For Loop_Set - Fatal编程技术网

Python 从与第三个列表中包含的另一个列表的项具有相同索引的列表中提取值

Python 从与第三个列表中包含的另一个列表的项具有相同索引的列表中提取值,python,python-3.x,list,for-loop,set,Python,Python 3.x,List,For Loop,Set,我想知道我是否可以在2个列表中识别类似值的位置,并使用第3个列表中识别值的位置 v= [100,200,300,400,500,600,700,800,900,1000,1100] x= [67,56,89,21,90,54,38,93,46,17,75] j= [200,500,600] 我希望代码能够识别j的值可以在v[1]、v[4]和v[5]中找到,并使用v[1]、v[4]和v[5]的位置来获取/返回x[1]、x[4]和x[5]的值。到目前为止,我已经尝试: h = set(v)&

我想知道我是否可以在2个列表中识别类似值的位置,并使用第3个列表中识别值的位置

v= [100,200,300,400,500,600,700,800,900,1000,1100]
x= [67,56,89,21,90,54,38,93,46,17,75]
j= [200,500,600]
我希望代码能够识别j的值可以在v[1]、v[4]和v[5]中找到,并使用v[1]、v[4]和v[5]的位置来获取/返回x[1]、x[4]和x[5]的值。到目前为止,我已经尝试:

h = set(v)&set(j) 
print(h)

您可以使用列表理解:

>>> v= [100,200,300,400,500,600,700,800,900,1000,1100]
>>> x= [67,56,89,21,90,54,38,93,46,17,75]
>>> j= [200,500,600]
>>> [x[i] for i, v_ele in enumerate(v) if v_ele in j]
[56, 90, 54]
或者使用部分解决方案:

>>> [x[v.index(i)] for i in set(v)&set(j)]
[56, 54, 90]
请注意,这并不能维持顺序,因为集合本身就是无序的。要解决此问题,我们可以使用键排序为
x.index

>>> sorted([56, 54, 90], key=x.index)
[56, 90, 54]