Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!newsreader4.netcologne.de!news.netcologne.de!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'algorithm': 0.04; 'essentially': 0.04; 'subject:two': 0.07; 'kumar': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:fields': 0.09; 'changes': 0.15; '"control"': 0.16; '203': 0.16; '509': 0.16; 'comparison.': 0.16; 'csv': 0.16; 'finds': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'subject:compare': 0.16; 'subject:python': 0.16; 'python?': 0.22; 'example.': 0.24; 'url:home': 0.24; '(or': 0.24; 'question': 0.24; 'compare': 0.26; 'header:X-Complaints-To:1': 0.27; 'record': 0.27; 'url:wiki': 0.31; 'file:': 0.31; 'group:': 0.31; 'url:wikipedia': 0.31; 'file': 0.32; 'basic': 0.35; 'done': 0.36; 'next': 0.36; 'charset:us-ascii': 0.36; 'thanks': 0.36; 'url:org': 0.36; 'should': 0.36; 'received:76': 0.38; 'to:addr:python-list': 0.38; 'track': 0.38; 'previous': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'break': 0.61; 'new': 0.61; 'numbers': 0.61; 'first': 0.61; 'field': 0.63; 'different': 0.65; 'finally': 0.65; 'here': 0.66; 'close': 0.67; 'action.': 0.84; 'same,': 0.91; '2013': 0.98 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Dennis Lee Bieber Subject: Re: how to compare two fields in python Date: Tue, 30 Apr 2013 19:54:17 -0400 Organization: > Bestiaria Support Staff < References: <59afdc2b-ab76-40b6-8f63-4a562e288029@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: adsl-76-253-104-134.dsl.klmzmi.sbcglobal.net X-Newsreader: Forte Agent 3.3/32.846 X-No-Archive: YES X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 43 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1367366071 news.xs4all.nl 15961 [2001:888:2000:d::a6]:40177 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:44576 On Tue, 30 Apr 2013 10:41:56 -0700 (PDT), upendra kumar Devisetty declaimed the following in gmane.comp.python.general: > I have a very basic question in python. I want to go through each line of the a csv file and compare to see if the first field of line 1 is same as first field of next line and so on. If it finds a match then i would like to put that field in an object1 else put that field in a different object2. Finally i would like to count how many of the fields in object1 vs object2. Can this be done in python? Here is a small example. > You are essentially describing a "control-break" (or "report break") http://en.wikipedia.org/wiki/Control_break The basic algorithm requires one to keep track of "previous" record and do a comparison. While the "control" field is the same, you do one action. When the control changes you close out the previous group and start a new one. > BRM_1 679 1929 > BRM_1 203 567 > BRM_2 367 1308 > BRM_3 435 509 > As you can see field1 of line1 is same as field2 of line2 and so that field BRM_1 should be place in object1 and BRM_2 and BRM_3 should be placed in object2. So the final numbers of object1 is 1 and object2 is 2. > Pseudo-code: control = None group = [] for record in file: if control is not None and record[0] != control: output(group) #close out previous group data group = [] #initialize new group data control = record[0] #reset control break data group.append(record) #add current record to group if group: output(group) #handle non-empty last group > Thanks in advance.. > > Upendra -- Wulfraed Dennis Lee Bieber AF6VN wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/