I am EXTREMELY new to coding and have no idea how to efficiently compare the elements in two lists/arrays in jython. I have one list of length 5 and another of length 0 to infinity. Elements are not in any kind of order. I want to find the difference between each element of the two lists. So if list1 = [30, 20, 50, 66, 2] and list2 = [6, 50, 90], I should have 15 outputs (len(list1)*len(list2)). I know how to go through and compare each element one by one, but I need an efficient way to compare all elements at once. I think I need some kind of iterator function, but have no idea how to implement it.
4 Answers
0
votes
This tutorial on control flows might be helpful.
list1 = [30, 20, 50, 66, 2]
list2 = [6, 50, 90]
result = []
for item1 in list1:
for item2 in list2:
result.append( item1 - item2 )
print result
#[24, -20, -60, 14, -30, -70, 44, 0, -40, 60, 16, -24, -4, -48, -88]
0
votes
0
votes
Another option here with list comprehensions:
compare one by one if they are equal
print [x==y for x in list2 for y in list1 ]
output:
[False, False, False, False, False, False, False, True, False, False, False, False, False, False, False]
and print matching:
print [ x for x in list2 for y in list1 if x==y ]
output:
[50]