Quantcast
Channel: User martineau - Stack Overflow
Viewing all articles
Browse latest Browse all 44

Answer by martineau for How to sort the output of difference between two sets?

$
0
0

You can sort the output by supplying a function via a keyword argument named key= to extract a comparison key from elements when calling the built-in sorted() function. In thus case all the callback function does is split the string on the first comma they all contain and returns the substring following it.

a = {'Jane Lee|248 Another St.|Boston|US|02130|4535353535.35353535353','Jim Smth|123 Any Street|Boston|US|02134','Name|Address|City|Country|Pinode'}b = {'Jane Lee|248 Another St.|Boston|US|02130|4535353535.3535355353','Jim Smith|123 Any Stret|Boston|US|02134','Name|Address|Cty|Country|Pincode'}diff = [('ua.csv,'+ i) if i in a else ('pr.csv,'+ i) if i in b else ''         for i in list(a ^ b)]if diff == []:    print('Great!!! There are no differences')else:    diff = sorted(diff, key=lambda v: v.split(',', maxsplit=1)[1])    print('\n'.join(diff))

Output:

ua.csv,Jane Lee|248 Another St.|Boston|US|02130|4535353535.35353535353pr.csv,Jane Lee|248 Another St.|Boston|US|02130|4535353535.3535355353pr.csv,Jim Smith|123 Any Stret|Boston|US|02134ua.csv,Jim Smth|123 Any Street|Boston|US|02134ua.csv,Name|Address|City|Country|Pinodepr.csv,Name|Address|Cty|Country|Pincode

Viewing all articles
Browse latest Browse all 44

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>