old = [('ver','1121'),('sign','89'),('address','A45'),('type','00')]
new = [('ver','1121'),('sign','89'),('type','01')]
I need to compare the new
list against old
one based on first element of the tuples, and show the difference between whatever elements new
list has, so that the output should look like:
Match : ver = 1121
Match : sign = 89
Mismatch : type = 01 (old : 00)
I could get all the matching tuples with below list comprehension but could not think beyond it.
my_list = [(a,b) for (a,b) in new for (c,d) in old if ((a==c) and (b==d))]
print( my_list)
Please suggest me a way to do it.
EDIT
I am sorry for not being clear on my question , I did not mention one more thing, the keys in the list can be repetitive, meaning the list can be like:
old = [('ver','1121'),('sign','89'),('address','A45'),('type','00'),('ver','sorry')]
new = [('ver','1121'),('sign','89'),('type','01'),('ver','sorry)]
UPDATE
Thanks to @holdenweb, I've made some changes to his code and this seems to be providing the expected output, please suggest if there are any flaws.
old = [('ver','1121'),('sign','89'),('address','A45'),('type','00'),('ver','works?')]
new = [('ver','1121'),('sign','89'),('type','01'),('ver','This')]
formatter = "{:12}: {:8} = {}".format
newfmter = "{} (old : {})".format
kv_old = []
for i,(kn, vn) in enumerate(new):
vo = [(j,(ko,vo)) for j,(ko, vo) in enumerate(old) if (ko==kn) ]
for idx,(key,val) in vo:
if idx >=i:
kv_old = [key,val]
break;
if kv_old[1]==vn:
print(formatter("Match", kv_old[0], kv_old[1]))
else:
print(formatter("Mismatch", kn, newfmter(vn, kv_old[1])))
Sometimes a list comprehension isn't the answer. This may be one of those times. Also, you don't handle the case where a key is present in
old
but not innew
- I include that case here, though you can chop out that code if it isn't relevant. You could similarly handle the case of keys missing fromnew
, but I didn't go that far.you can do something like :
that will give you :
But for sure there is a more "pythonic" way....
What about this:
Output:
If you need the order, try to use
OrderedDict
:Here's a one-liner to get a list of all comparisons. Depending on how big your old and new lists are, a set comprehension would work a bit more quickly, but that speed would be nullified by my
sorted
+itertools.groupby
approach below (assorted
returns alist
):comps
is now:Print it out:
Edit: The following isn't exactly what OP wants, but I'll leave it anyway for those interested in seeing what stayed the same first, and what changed second (actually, you could define a custom grammar to sort by if you wanted to see the elements that changed first, but that's up to you).
Prepare for using
itertools.groupby
, and taking into account OP's request for the printing order shown above, we can use acollections.OrderedDict
object to create effectively an "ordered set" of "keys":Now sort
comps
based on the custom grammar inproper_order
:Use
itertools.groupby
to print:It just so happens that the order of this output is the same as what OP requested above, but for larger cases, the difference in order between the two approaches will become evident.
You can use a
set
: