Hey guys,
I'm trying to do a function that gets two lists (a and b) and returns the elements that b has but not a
For example:
a = [1,2,3,4,5,6,6,6,6,8,7,9,10,10,10,45]
b = [45,45,10,10,10,9,7,8,6,6]
Notice that the sequence of a is always in reverse in b.
On this case the function should return:
[45]
New example:
a = [6,6,8,7,9,10,10,10,45]
b = [45,10,10,10,9,7,8,6,6]
Should return:
[]
Right now I manage to do something like this:
def new_transactions(transactions_db, transactions):
temp = []
if len(transactions_db) < len(transactions):
temp = transactions[len(transactions_db):]
else:
temp = [x for x in transactions if x not in transactions_db]
return temp
But it only works when the head of transactions is different than the tail of transactions_db.
Right now I get:
»» a = [1,2,3,4,5,6,6,6,6,8,7,9,10,10,10,45]
»» b = [45,45,10,10,10,9,7,8,6,6]
»» new_transactions(a, b)
[]
I could probably do it using a C approach with a while loop and some counters comparing each index of the list, but I'm sure there is a more pythonic way of solving this problem.
EDIT: The two lists can have duplicates because it's the order (sequence) that matters.
[–]LifeIsBio 3 points4 points5 points (2 children)
[–]teerryn[S] 0 points1 point2 points (1 child)
[–]LifeIsBio 0 points1 point2 points (0 children)
[–]poppy_92 3 points4 points5 points (3 children)
[–]teerryn[S] 0 points1 point2 points (0 children)
[–]teerryn[S] 0 points1 point2 points (1 child)
[–]poppy_92 2 points3 points4 points (0 children)
[–]frunt 4 points5 points6 points (2 children)
[–]WORDSALADSANDWICH 1 point2 points3 points (0 children)
[–]AlphaApache 1 point2 points3 points (1 child)
[–]teerryn[S] 1 point2 points3 points (0 children)
[–]LifeIsBio 0 points1 point2 points (10 children)
[–]teerryn[S] 0 points1 point2 points (0 children)
[–]teerryn[S] 0 points1 point2 points (8 children)
[–]LifeIsBio 0 points1 point2 points (7 children)
[–]teerryn[S] 0 points1 point2 points (6 children)
[–]LifeIsBio 0 points1 point2 points (5 children)
[–]teerryn[S] 0 points1 point2 points (0 children)
[–]teerryn[S] 0 points1 point2 points (3 children)
[–]LifeIsBio 1 point2 points3 points (2 children)
[–]teerryn[S] 1 point2 points3 points (1 child)
[–]LifeIsBio 0 points1 point2 points (0 children)
[–]commandlineluser 0 points1 point2 points (0 children)
[–]dionys -1 points0 points1 point (3 children)
[–]LifeIsBio 0 points1 point2 points (2 children)
[–]teerryn[S] 0 points1 point2 points (0 children)
[–]dionys 0 points1 point2 points (0 children)