简体   繁体   中英

Compare two List of tuples based in python

Trying to compare two list of tuples based on a key present in both the lists. my lists look like below.

sr = [('#1', '004634105000', 'sr1'), ('#2', '01fG91258000', 'sr2'), ('#6','01F991259000', 'sr3'), ('#4', '07W997296000', 'sr4'), ('#5', '07W997296000', 'sr5'), ('#7', '454546465666', 'sr6'), ('#7', '4545TRY5666', 'sr7')]

tr = [ ('#1', '00461U105000', 'tr1'), ('#2', '01F991258000', 'tr2'),
('#3', '01F991259000', 'tr3'), ('#4', '07W997296000', 'tr4'),
('#5', '07W997296000', 'tr5'), ('#6', '454546465666', 'tr6')]

Considering first element as key for comparison, I want output like this

common records:('#1', '004634105000', 'sr1'), ('#1', '00461U105000', 'tr1') ('#2', '01fG91258000', 'sr2'), ('#2', '01F991258000', 'tr2') ('#4', '07W997296000', 'sr4'), ('#4', '07W997296000', 'tr4') ('#5', '07W997296000', 'sr5') , ('#5', '07W997296000', 'tr5')
present only in sr: ('#7', '454546465666', 'sr6'), ('#7', '4545TRY5666', 'sr7')
present only in tr: ('#6', '454546465666', 'tr6')

Here is the code I tried but its not giving required output

source = []
for key in tr:
    source = key[0]
for value in sr:
    if value[0] not in source:
        print (value[0], value[1], value[2])   

target = []

for key in sr:
    target = key[0]
   # print (target)
for value in tr:
    #print (value[0]) 
    if value[0] not in target:
        print (value[0], value[1], value[2])

common = [] 

for key in sr:
    common = key[0]
   # print (target)
for value in tr:
    #print (value[0]) 
    if value[0] in common:
      print (value[0], value[1], value[2]) 

I am missing something in the loop and also not handling duplicates, if someone can help me that would be nice. Thanks

First, group the data by key in the standard fashion:

by_key={}
for xr in (sr,tr):  # cover both lists
  for x in xr:
    by_key.setdefault(x[0], []).append(x)

Then, form sets of the keys:

sk=set(x[0] for x in sr)
tk=set(x[0] for x in tr)

Then address the three categories:

for nm,keys in (("common records",sk&tk),
                ("present only in sr",sk-tk),
                ("present only in tr",tk-sk)):
  print(nm+':', " ".join(str(x) for k in keys for x in by_key[k]))

The solution that is closest to your own code would be:

sr = [('#1', '004634105000', 'sr1'), ('#2', '01fG91258000', 'sr2'),
      ('#6','01F991259000', 'sr3'), ('#4', '07W997296000', 'sr4'), 
      ('#5', '07W997296000', 'sr5'), ('#7', '454546465666', 'sr6'), 
      ('#7', '4545TRY5666', 'sr7')]

tr = [('#1', '00461U105000', 'tr1'), ('#2', '01F991258000', 'tr2'),
      ('#3', '01F991259000', 'tr3'), ('#4', '07W997296000', 'tr4'),
      ('#5', '07W997296000', 'tr5'), ('#6', '454546465666', 'tr6')]

print ("present only in sr")
source = []
for key in tr:
    source.append(key[0])
for value in sr:
    if value[0] not in source:
        print (value[0], value[1], value[2])

print ("present only in tr")
target = []

for key in sr:
    target.append(key[0])
for value in tr:
    if value[0] not in target:
        print (value[0], value[1], value[2])

print ("common entries")
common = []

for key in sr:
    common.append(key[0])
for value in tr:
    if value[0] in common:
      print (value[0], value[1], value[2])

The part you were missing was:

source.append(key[0])

which adds the value of key[0] to your source-list.

You should consider using dictionaries though, as suggested by Davis Herring

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM