Andy
0
Q:

python list INTERSECTION

def intersection(lst1, lst2): 
    lst3 = [value for value in lst1 if value in lst2] 
    return lst3 
  
# Driver Code 
lst1 = [4, 9, 1, 17, 11, 26, 28, 54, 69] 
lst2 = [9, 9, 74, 21, 45, 11, 63, 28, 26] 
print(intersection(lst1, lst2)) 
1
>>> a = [1,2,3,4,5]
>>> b = [1,3,5,6]
>>> list(set(a) & set(b))
[1, 3, 5]
1
import numpy as np
recent_coding_books =  np.intersect1d(recent_books,coding_books)
1
x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}

z = x.intersection(y)
    
print(z)

# Output: {"google"}
1

    x = 
    {"apple", "banana", "cherry"}
y = {"google", 
    "microsoft", "apple"}

z = x.intersection(y)
    

print(z) 
1
#get name1 and name2 as a list input
name1 =list("".join(str(x)for x in input("Enter name1").replace(" ","")))
name2 =list("".join(str(x)for x in input("Enter name2").replace(" ","")))
#check using list comprehension if x in name1 is in name2
#this will return multiple instances of the same character from name1 that matches with the name2
common = [x for x in name1 if x in name2]
#create a set out of the output so as to have only unique values of the repeated characters
unique = set(common)
#thus the above set will have common unrepeated characters from both names
#create a variable and initialize it to zero
d=0
#run a loop that checks the minimum occurrence of 
#the character from the set in name1 & name2
#Minimum because for ex: a might exist thrice in name1, but only twice in name2
#we will need to take only 2 common occurrences from the name2
#thus finding the minimum occurrence of the character from both names
for x in unique:
    d = d + min(name1.count(x),name2.count(x))
#multiplying by two, because if one character from name 1 matches with one,
#character from name2, then it makes two in total
difference = (len(name1) + len(name2)) - d*2
print(difference)
0
a = ['apple', 'banana', 'pear']
b = ['fridge', 'stove', 'banana']

a & b == ['banana'] #True
1
def line_intersection(line1, line2):
    xdiff = (line1[0][0] - line1[1][0], line2[0][0] - line2[1][0])
    ydiff = (line1[0][1] - line1[1][1], line2[0][1] - line2[1][1])

    def det(a, b):
        return a[0] * b[1] - a[1] * b[0]

    div = det(xdiff, ydiff)
    if div == 0:
       raise Exception('lines do not intersect')

    d = (det(*line1), det(*line2))
    x = det(d, xdiff) / div
    y = det(d, ydiff) / div
    return x, y

print (line_intersection((A, B), (C, D)))

# And FYI, I would use tuples instead of lists for your points. E.g.
# A = (X, Y)
0

New to Communities?

Join the community