Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!eternal-september.org!feeder.eternal-september.org!border1.nntp.ams1.giganews.com!nntp.giganews.com!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.024 X-Spam-Evidence: '*H*': 0.95; '*S*': 0.00; 'tutorial': 0.03; 'algorithm': 0.04; 'output': 0.05; 'duplicate': 0.07; 'initialize': 0.07; 'problem:': 0.07; 'cest': 0.09; 'closest': 0.09; 'exit': 0.09; 'rows': 0.09; '>on': 0.16; '>the': 0.16; 'be:': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'thu,': 0.19; 'help.': 0.21; 'appears': 0.22; 'select': 0.22; 'reset': 0.22; 'sort': 0.25; 'compare': 0.26; 'record': 0.27; 'to:2**1': 0.27; 'point': 0.28; 'to:no real name:2**1': 0.29; 'url:wiki': 0.31; '-0700,': 0.31; 'loading': 0.31; 'url:wikipedia': 0.31; 'this.': 0.32; 'run': 0.32; 'lab': 0.33; 'table': 0.34; 'could': 0.34; 'hundreds': 0.35; 'add': 0.35; 'there': 0.35; 'really': 0.36; '14,': 0.36; 'object,': 0.36; 'next': 0.36; 'charset:us-ascii': 0.36; 'url:org': 0.36; 'so,': 0.37; 'skip:- 20': 0.37; 'to:addr :python-list': 0.38; 'short': 0.38; 'to:addr:python.org': 0.39; 'how': 0.40; 'even': 0.60; 'remove': 0.60; 'read': 0.60; 'received:98.137': 0.60; 'break': 0.61; 'new': 0.61; 'matter': 0.61; 'first': 0.61; 'field': 0.63; 'sum': 0.64; 'accounts': 0.64; 'great': 0.65; 'account': 0.65; 'granted': 0.65; 'url:0': 0.67; 'capable': 0.67; 'dont': 0.67; 'received:gq1.yahoo.com': 0.68; 'received:mail.gq1.yahoo.com': 0.68; 'received:98.136': 0.69; '2015': 0.84; 'received:bullet.mail.gq1.yahoo.com': 0.84 X-Yahoo-Newman-Property: ymail-3 X-Yahoo-Newman-Id: 970049.63267.bm@omp1019.mail.gq1.yahoo.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s1024; t=1431623301; bh=g/wqtjerb5I0Tft1cVz/7ExYntl2Wql5y0aAVIqgkgM=; h=Message-ID:Date:From:Subject:To:MIME-Version:Content-Type; b=Z+WGxyxVhQtQF+VejAXQ3bcNnV+bJZ9QmzWoiBC+zkw2HUsaoSu/JZSbHh1gCVRzm/jId4t5L+07EaX75QN4OYiHSgV2plZg9jD08tyrd4rQn8vNSxSsB47V8d1ucbWj0OovGwZpyAhzq0GW522E3C93lCN7ijqo2dvII/DVYXw= X-YMail-OSG: LsECcbEVM1mQNp3KuuYcTD4hU2ljvBz7Xv0UTn4o2e1kS7V ekFSpuNQGvWz12vNL3vz0TIanbsFOGfzfhuGS4M9qe3YVbNTwQp2P2JReDbF dgsk7DkaxPmIQRufahsst5k03VtNEsHNCVaqr6utDm43I_TVUGnto.QKCo9o eFZTznibi3vtxZO5ily5SzovMo_F2RPMHW1TJrGdrBENOsXBvlQP2y30iTKh T6ocIEoNmZIhY6_1QQOkxzyeGaPrwb7QYoIXIMUVR6lJF3u_tlBJToOth6BO vIPEZsnKU8BO0WAtNB2yDW84Gd7Q.7RD8LCsrQeG33UVjiFemr7bEZjAtYJJ CrkdAgId92QHoWFjl7HlJcZ36ylJNBohjZjFsqw9Pj7ohbzcHRwTBWLSy1q4 10Z0WeOAVA0hjPayHGEOGiB8hWy0_K1He5vwP9fjH5.j1DvaZelxAIdCNzXH 0_9jCbYci4Xa3067AsqpXBsieNkcYJfOa68_TI3C5lmCt5BNXRdyuWO1vMSL yj2scUIZ6PK_QNC.IjvBKno828KLrW.JpxGbvXlri0wJ1UUeRp_w0k.5ZpcP KHtXB7fFY0iwlgA-- X-Rocket-MIMEInfo: 002.001, Ci0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tDQpPbiBUaHUsIE1heSAxNCwgMjAxNSAzOjM1IFBNIENFU1QgRGVubmlzIExlZSBCaWViZXIgd3JvdGU6DQoNCj5PbiBXZWQsIDEzIE1heSAyMDE1IDE2OjI0OjMwIC0wNzAwLCAyMC8yMCBMYWIgPGxhYkBwYWNiZWxsLm5ldD4gZGVjbGFpbWVkDQo.dGhlIGZvbGxvd2luZzoNCj4NCj4.Tm93IGlzIHdlcmUgSSBoYXZlIG15IHByb2JsZW06DQo.Pg0KPj5teUxpc3QgPSBbIFsxMjMsICJYWFgiLCAiSXRlbSIsICJRdHkiLCAiTm9pc2UiXSwNCj4.ICAgICAgICABMAEBAQE- X-Mailer: YahooMailWebService/0.8.203.740 Date: Thu, 14 May 2015 10:08:21 -0700 From: Albert-Jan Roskam Subject: Re: Looking for direction To: wlfraed@ix.netcom.com, python-list@python.org MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 54 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1431623438 news.xs4all.nl 2859 [2001:888:2000:d::a6]:50024 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:90615 ----------------------------- On Thu, May 14, 2015 3:35 PM CEST Dennis Lee Bieber wrote: >On Wed, 13 May 2015 16:24:30 -0700, 20/20 Lab declaimed >the following: > >>Now is were I have my problem: >> >>myList = [ [123, "XXX", "Item", "Qty", "Noise"], >> [72976, "YYY", "Item", "Qty", "Noise"], >> [123, "XXX" "ItemTypo", "Qty", "Noise"] ] >> >>Basically, I need to check for rows with duplicate accounts row[0] and >>staff (row[1]), and if so, remove that row, and add it's Qty to the >>original row. I really dont have a clue how to go about this. The >>number of rows change based on which run it is, so I couldnt even get >>away with using hundreds of compare loops. >> >>If someone could point me to some documentation on the functions I would >>need, or a tutorial it would be a great help. >> > > This appears to be a matter of algorithm development -- there won't be >an pre-made "function" for it. The closest would be the summing functions >(control break http://en.wikipedia.org/wiki/Control_break ) of a report >writer application. > > The short gist would be: > > SORT the data by the account field > Initialize sum using first record > loop > read next record > if end of data > output sum record > exit > if record is same account as sum > add quantity to sum > else > output sum record > reset sum to the new record > > Granted -- loading the data into an SQL capable database would make >this simple... > > select account, sum(quantity) from table > order by account You could also use pandas. Read the data in a DataFrame, create a groupby object, use the sum() and the first() methods. http://pandas.pydata.org/pandas-docs/version/0.15.2/groupby.html