Help to solve my problems with python LIST?

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]] ---------------------------------------------

I Want present data like this

author quantity Amount($) A&B 3 30 A 3 30 B 7 30 total 13 90

I DO NOT Want present data like this !!! in this case it's ADDED duplicate ['book_z',3,30]

author quantity Amount($) A 6 60 B 10 60 total 16 120

that is my problems ,Anybody Please help me to sove this problems. Thanks everybody

-------------Problems Reply------------

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)

I've tried to reproduce your desired weird format with some numbers left-aligned and totally funky spacing, but I'm sure you'll need to tweak the formatting in the various print statements to get the exact (and totally weird) formatting effect of your examples. However, apart from the spacing and left- vs right- alignment of the output, this should otherwise be exactly what you request.

You can find the intersections and exclusive ones like this...

A_and_B = [a for a in author_A if a in author_B]
only_A = [a for a in author_A if a not in author_B]
only_B = [b for b in author_B if b not in author_A]

then it is only a matter of printing them...

print '%s %d %d' % tuple(A_and_B)
print '%s %d %d' % tuple(only_A)
print '%s %d %d' % tuple(only_B)

Hope that helps

books = {
'A':[['book_x',1,10],['book_y',2,20]],
'B':[['book_s',5,10],['book_t',2,20]],
'A & B':[['book_z',3,30]],
}
for key in books:
quantity = []
amount = []
for item in books[key]:
quantity.append(item[1])
amount.append(item[2])
print ("%s\t%s\t%s") % (key,sum(quantity),sum(amount))

Category:python Views:0 Time:2009-10-07
Tags: python

Related post

  • How to solve an LCP (linear complementarity problem) in python? 2010-01-31

    Is there a good library to numericly solve an LCP in python ? Edit: I need a working python code example because most libraries seem to only solve quadratic problems and i have problems converting an LCP into a QP. --------------Solutions------------

  • Obtaining all possible states of an object for a NP-Complete(?) problem in Python 2009-02-12

    Not sure that the example (nor the actual usecase) qualifies as NP-Complete, but I'm wondering about the most Pythonic way to do the below assuming that this was the algorithm available. Say you have : class Person: def __init__(self): self.status='u

  • Problem using Python comtypes library to add a querytable to Excel 2009-03-26

    I'm trying to create a QueryTable in an excel spreadsheet using the Python comtypes library, but getting a rather uninformative error... In vba (in a module within the workbook), the following code works fine: Sub CreateQuery() Dim con As ADODB.Conne

  • Simplest way to solve mathematical equations in Python 2009-10-29

    I want to solve a set of equations, linear, or sometimes quadratic. I don't have a specific problem, but often, I have been in this situation often. It is simple to use wolframalpha.com, the web equivalent of Mathematica, to solve them. But that does

  • Solving statistics question using Python 2010-11-08

    I'm really lost. How can I approach this problem? Beginning with $1 of capital, you choose a fixed proportion p of your capital to bet on a fair coin tossed repeatedly for 1000 times. Your returns is doubled if the toss lands head and you lose your i

  • N queen problem in python 2011-01-27

    I have just solved the nqueen problem in python. The solution outputs the total number of solutions for placing n queens on an nXn chessboard but trying it with n=15 takes more than an hour to get an answer. Can anyone take a look at the code and giv

  • eight queens problem in Python 2011-02-27

    8-queens problem in Python. Hi! I only start teaching Python, so could someone explain the code written below (found in the Internet)? Some pieces of the code are complicated for me. Please, explain them. Thank you. Questions are near the code. BOARD

  • Optimization problem in Python 2011-03-03

    I need to solve a problem. I have 5 devices. They all have 4 kind of I/O types. And there is a target input/output combination. At first step, I want to find all combinations among the devices so that the total I/O number of selected devices are all

  • Unbound local variable problem in Python 2011-04-29

    I've got a following code snippet: def isolation_level(level): def decorator(fn): def recur(level, *args, **kwargs): if connection.inside_block: if connection.isolation_level < level: raise IsolationLevelError(connection) else: fn(*args, **kwargs)

  • Problems installing Python Fabric on Windows 7 2012-02-03

    I'm trying to install Python Fabric on Windows 7 using the guide from Getting Python and Fabric Installed on Windows. What i did so far: Installed Python 2.7 to C:\Python27 Added the Python-Directory and Python/Scripts to the Windows System Paths Ins

  • Solved: Chrome problem 2013-12-27

    Hi everyone. I'm current have a very annoying problem with Chrome. Every time I open Chrome for the first time after turning on my laptop, it shows a strange screen with multiple orange tabs (like in the picture I attached). No website would open unl

  • Have you used a traveling salesman algorithm to solve a problem? 2008-11-05

    I studied TSP in college in the context of NP Completeness. I have never actually had a situation where it would apply to a practical problem. A little bit of research shows that it has been used to pick the cheapest path to move a drill around, that

  • Level of Indirection solves every Problem 2008-11-13

    What does the quote "Level of Indirection solves every Problem" mean in Computer Science? --------------Solutions------------- Generally it means that by increasing the level of abstraction one can make the problem easier to understand/resolve. Be ca

  • Is there really a "Best" way of solving a problem? 2009-02-24

    I have been looking at a lot of these questions asking for the "best" way to do this or the "best" way to do that. Does one person's best way make it every person's best way? Can there really be a "best" way to do a particular algorithm or to solve a

  • What are some situations in which the Decorator Pattern solves the problem really well? 2009-06-10

    I think a classic example is that the Window class can have a Border decorator and a ScrollBar decorator in the GoF book. What are some situations you think, know of, in which the Decorator Pattern solves the problem really well? --------------Soluti

  • Upgrading SQL Server 2000 to MySQL 5.1 using DTS - How solve DateTime problem? 2009-06-24

    I'm trying to migrate from sql server 2000 to mysql by using DTS. That's the best tool I know yet. But one a sql server tables has a column with datetime type, the DTS suggest the following sql code: `StartDate` long varbinary NULL, `EndDate` long va

  • How to solve these problems with Asynchronous Callback? 2009-12-03

    I need to run 5 algorithms parallely each takes an image as input and gives image as output. After each of these is done, I need to display the 5 output images. I'm using Asynchronous Callback using delegates for this task. So, I created 5 delegates

  • Has using an acknowledged anti-pattern ever been proven to actually solve a problem, or be beneficial in any other way? 2009-12-04

    Has using an acknowledged anti-pattern ever been proven to actually work in a certain specific case? Did you ever solve a problem or gain any kind of benefit in one of your projects by using an anti-pattern? --------------Solutions------------- Anti-

  • How to solve a problem in using RAII code and non-RAII code together in C++? 2009-12-30

    We have 3 different libraries, each developed by a different developer, and each was (presumably) well designed. But since some of the libraries are using RAII and some don't, and some of the libraries are loaded dynamically, and the others aren't -

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.217 (s). 11 q(s)