![](/img/trans.png)
[英]How do I individually print items of a list that are in another list in python
[英]how do I determine if one list has items contained in another list, then print their index in Python
我想確定一個列表中是否包含另一個列表中的項目。 如果是這樣,我想打印這些項目的索引:
List1 = [10, 20, 30]
List2 = [10, 50, 80, 90, 20, 30, 40, 50]
例如,我想在List2
存在的List1
中找到10
,並打印索引0
。 有什么解決方案或直接功能嗎?
#Finding intersection between two lists
inter=set(List1)&set(List2)
for val in inter:
#prints index of value which contains on both lists
print List2.index(val)
使用 for 循環和index
方法
演示:
List1= [10,20,30]
List2=[10,50,80,90,20,30,40,50]
for i in List1:
if i in List2:
print(List2.index(i))
嘗試這個 ;)
List1,List2=[10,20,30],[10,50,80,90,20,30,40,50] #input
i = List1[0] #number you're looking for
for index in range(0,len(List2)):
if List2[index] == i:
print (index) # if number is present
嘗試這個:-
ls = [i for i,x in enumerate(List2) if x in List1]
print(ls)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.