Prev: Looking for the mysterious zlib.py* files in Python 2.6.4(32-bit) for Windows
Next: [ANN] pysqlite 2.6.0 released
From: jimgardener on 5 Mar 2010 03:05 hi I have two lists of names.I need to find the difference between these two lists.I tried to do it using sets.But I am wondering if there is a better way to do it.Please tell me if there is a more elegant way. thanks, jim my code snippet follows.. oldlst=['jon','arya','ned','bran'] newlst=['jaime','jon','cersei'] newlyadded=set(newlst)-set(oldlst) removed=set(oldlst)-set(newlst) unchanged=set(oldlst)& set(newlst) print '%d were newly added= %s'%(len(newlyadded),list(newlyadded)) print '%d were removed=%s'%(len(removed),list(removed)) print '%d were unchanged=%s'%(len(unchanged),list(unchanged)) this produces the output -------------- 2 were newly added= ['jaime', 'cersei'] 3 were removed=['ned', 'arya', 'bran'] 1 were unchanged=['jon']
From: Vlastimil Brom on 5 Mar 2010 03:50 2010/3/5 jimgardener <jimgardener(a)gmail.com>: > hi > I have two lists of names.I need to find the difference between these > two lists.I tried to do it using sets.But I am wondering if there is a > better way to do it.Please tell me if there is a more elegant way. > thanks, > jim > > my code snippet follows.. > > oldlst=['jon','arya','ned','bran'] > newlst=['jaime','jon','cersei'] > > newlyadded=set(newlst)-set(oldlst) > removed=set(oldlst)-set(newlst) > unchanged=set(oldlst)& set(newlst) > print '%d were newly added= %s'%(len(newlyadded),list(newlyadded)) > print '%d were removed=%s'%(len(removed),list(removed)) > print '%d were unchanged=%s'%(len(unchanged),list(unchanged)) > > this produces the output > -------------- > 2 were newly added= ['jaime', 'cersei'] > 3 were removed=['ned', 'arya', 'bran'] > 1 were unchanged=['jon'] > -- > http://mail.python.org/mailman/listinfo/python-list > Hi, I guess for lists with unique items and order insignificant this is just fine; you may also check difflib, if it suits your needs; there are multiple comparing and displaying possibilities, e.g.: >>> import difflib >>> print "\n".join(difflib.unified_diff(['jon','arya','ned','bran'], ['jaime','jon','cersei'])) --- +++ @@ -1,4 +1,3 @@ +jaime jon -arya -ned -bran +cersei >>> hth, vbr
From: Terry Reedy on 5 Mar 2010 12:17
On 3/5/2010 3:05 AM, jimgardener wrote: > hi > I have two lists of names.I need to find the difference between these > two lists.I tried to do it using sets.But I am wondering if there is a > better way to do it.Please tell me if there is a more elegant way. > thanks, > jim > > my code snippet follows.. > > oldlst=['jon','arya','ned','bran'] > newlst=['jaime','jon','cersei'] > > newlyadded=set(newlst)-set(oldlst) > removed=set(oldlst)-set(newlst) > unchanged=set(oldlst)& set(newlst) Except for the duplicate calls to set, this is fine. If order and duplication are irrelevant (and hashability is guaranteed), use sets to start with. > print '%d were newly added= %s'%(len(newlyadded),list(newlyadded)) > print '%d were removed=%s'%(len(removed),list(removed)) > print '%d were unchanged=%s'%(len(unchanged),list(unchanged)) > > this produces the output > -------------- > 2 were newly added= ['jaime', 'cersei'] > 3 were removed=['ned', 'arya', 'bran'] > 1 were unchanged=['jon'] |