帮助解决我的python LIST问题?

kn3*_*n3l 0 python

author_A = [['book_x',1,10],['book_y',2,20],['book_z',3,30]]
author_B = [['book_s',5,10],['book_t',2,20],['book_z',3,30]]

author_A AND author_B = ['book_z',3,30]
             author_A = [['book_x',1,10],['book_y',2,20]]
             author_B = [['book_s',5,10],['book_t',2,20]]
             ---------------------------------------------
Run Code Online (Sandbox Code Playgroud)

我想要这样的现有数据

     author     quantity   Amount($)
      A&B        3            30
      A          3            30
      B          7            30
      total      13           90
Run Code Online (Sandbox Code Playgroud)

我不想要这样的现有数据!在这种情况下,它是ADDED重复['book_z',3,30]

         author   quantity   Amount($)
          A         6            60
          B         10           60
          total     16           120
Run Code Online (Sandbox Code Playgroud)

这是我的问题,任何人请帮助我解决这个问题.谢谢大家

Ale*_*lli 6

author_A = [['book_x',1,10],['book_y',2,20],['book_z',3,30]]
author_B = [['book_s',5,10],['book_t',2,20],['book_z',3,30]]

def present(A, B):
  Aset = set(tuple(x) for x in A)
  Bset = set(tuple(x) for x in B)
  both = Aset & Bset
  justA = Aset - both
  justB = Bset - both
  totals = [0, 0]
  print "%-12s %-12s %12s" % ('author', 'quantity', 'Amount($)')
  for subset, name in zip((both, justA, justB), ('A*B', 'A', 'B')):
    tq = sum(x[1] for x in subset)
    ta = sum(x[2] for x in subset)
    totals[0] += tq
    totals[1] += ta
    print ' %-11s  %-11d    %-11d' % (name, tq, ta)
  print ' %-11s  %-11d    %-11d' % ('total', totals[0], totals[1])

present(author_A, author_B)
Run Code Online (Sandbox Code Playgroud)

我试图用一些数字左对齐和完全时髦的间距重现你想要的怪异格式,但我确定你需要在各种print语句中调整格式以获得精确(并且完全奇怪)的格式化效果你的例子.但是,除了输出的间距和左右对齐之外,这应该是您要求的.