Please tell me how to check the location of intersection in python.

Asked 2 years ago, Updated 2 years ago, 41 views

#Condition—There is no duplication of data.
x = {"a", "b", "c"}
y = {"c", "b", "e"}
z = {"f", "b", "c"}
result=x.intersection(y,z)
print(result)
# {'b', 'c'}

Sample Output) Please let me know if there is a recommended format.
[['b', 1, 1, 1], ['c', 2, 0, 2]}

Would you like to search for 'b', 'c'?It would be helpful if you could write in one line.
Thank you for your cooperation.

python algorithm

2022-09-30 10:19

1 Answers

I don't know python, but I tried writing it in ruby.
Please translate it to python.

 x = ["a", "b", "c" ]
y = ["c", "b", "e" ]
z = ["f", "b", "c" ]

def intersection (x, *a)
    x.find_all {|i|[x,*a].all?{|j|j.include?(i)}}.collect {|i|[i]+[x,*a].collect {|j|j.index(i)}}}
end

print(intersection(x,y,z))


2022-09-30 10:19

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.