简体   繁体   中英

Quick Question About Intersection of Lists in python

I am very new to python and I have a quick question about comparing lists.The code I have is very simple.I created an array using and wanted the intersection of array's 2nd indiced element and a certain list I have. This list changes constantly on the tags that are used but it consists of numbers. Anyway,from this I get the intersection I want homever, I want to do another thing to this code: After I use intersect function, I want to get the 0th and 1st elements of the row. İ.e print(intersection(D,liste[-1]))comes out to be [2,3] then I want to get [40,75] and [75,25] This might be a very simple case but as I said I am a beginner and looking forward to learn more. Thanks in advance

A=np.array([[25,25,1],[40,75,2],[75,25,3],[25,50,10]])
C=[1,2,6]
B=[1,2,19]
D=[]
D.extend(A[:,2])
def intersection(D,B): 
    lst3 = [value for value in D if value in B] 
    return(lst3)
 print(intersection(D,liste[-1])) 

Not sure if I understand your problem, but you can turn your lists into sets to get intersection between them like this:

first_collection = set([1, 3, 4])
second_collection = set([1, 5, 7])

intersection = first_collection.insersection(second_collection)

Hope it helps somehow.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM