How can i check if item in b is in a and the found match item in a should not be use in the next matching?
Currently this code will match both 2 in b.
a = [3,2,5,4]
b = [2,4,2]
for i in b:
if i in a:
print "%d is in a" % i
This is the required output:
2 => 2 is in a
4 => 4 is in a
2 =>
EDIT: Example 2:
a = [3,2,2,4]
b = [2,4,2]
output should be
2 => 2 is in a
4 => 4 is in a
2 => 2 is in a
(long post but read it entirely, solution is at the end).
Remove the found value or register it in another dict.
Better though is to count the number of apparitions inside each array and test how many are common.
For the second case, you’d have
for
a:3 appears 1 times
2 appears 1 times
5 appears 1 times
4 appears 1 times
for
b:2 appears 2 times
4 appears 1 times
Keep these values in dictionaries:
And now, it is simple:
The
b_appdictionary would be used in other case.Here is a test script I wrote (testing all testcases issued here):
And here is the output:
Everything is perfect and no order is lost 🙂
Edit: Updated with @Avaris’s suggestions, this script looks like: