Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #90595

Re: Looking for direction

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!newsreader4.netcologne.de!news.netcologne.de!bcyclone04.am1.xlned.com!bcyclone04.am1.xlned.com!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python@mrabarnett.plus.com>
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; 'tutorial': 0.03; 'else:': 0.03; 'beginner': 0.05; 'duplicate': 0.07; 'problem:': 0.07; 'data:': 0.09; 'item,': 0.09; 'rows': 0.09; 'rows,': 0.09; 'way:': 0.09; 'def': 0.12; '72k': 0.16; 'before.': 0.16; 'columns': 0.16; 'csv': 0.16; 'dictionary,': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'matters,': 0.16; 'message- id:@mrabarnett.plus.com': 0.16; 'mylist': 0.16; 'received:192.168.1.4': 0.16; 'wrote:': 0.18; 'written': 0.21; 'help.': 0.21; 'header:User-Agent:1': 0.23; "i've": 0.25; 'compare': 0.26; 'header:In-Reply-To:1': 0.27; 'point': 0.28; 'external': 0.29; 'said,': 0.30; "i'm": 0.30; 'easier': 0.31; 'end,': 0.31; 'index,': 0.31; 'file': 0.32; 'there.': 0.32; 'this.': 0.32; 'skip:m 30': 0.32; 'run': 0.32; 'worked': 0.33; 'lab': 0.33; 'skip:d 20': 0.34; 'could': 0.34; 'hundreds': 0.35; 'add': 0.35; 'really': 0.36; 'in.': 0.36; 'should': 0.36; 'so,': 0.37; 'starting': 0.37; 'being': 0.38; 'skip:o 20': 0.38; 'to:addr :python-list': 0.38; 'issue': 0.38; 'pm,': 0.38; 'that,': 0.38; 'short': 0.38; 'sure': 0.39; 'to:addr:python.org': 0.39; 'enough': 0.39; 'how': 0.40; 'even': 0.60; 'remove': 0.60; 'staff': 0.61; 'simple': 0.61; 'you.': 0.62; 'accounts': 0.64; 'great': 0.65; 'life': 0.66; 'here': 0.66; 'dont': 0.67; 'combining': 0.68; 'ethan': 0.84; 'furman': 0.84; 'needed:': 0.84; 'noise': 0.84; 'staff,': 0.84; 'hundred': 0.95
X-CM-Score 0.00
X-CNFS-Analysis v=2.1 cv=DZWZq5dW c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=SY9aFQKHIdAA:10 a=IkcTkHD0fZMA:10 a=EBOSESyhAAAA:8 a=6iNIqeUFLsXJTCdefRQA:9 a=oNodqMe0TwPof9wY:21 a=GDv-IhClZb49F3dX:21 a=QEXdDO2ut3YA:10
X-AUTH mrabarnett@:2500
Date Thu, 14 May 2015 01:41:23 +0100
From MRAB <python@mrabarnett.plus.com>
User-Agent Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.6.0
MIME-Version 1.0
To python-list@python.org
Subject Re: Looking for direction
References <5553DD2E.2080600@pacbell.net> <5553E71F.8090402@stoneleaf.us>
In-Reply-To <5553E71F.8090402@stoneleaf.us>
Content-Type text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding 7bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.471.1431564092.12865.python-list@python.org> (permalink)
Lines 69
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1431564092 news.xs4all.nl 2826 [2001:888:2000:d::a6]:46501
X-Complaints-To abuse@xs4all.nl
X-Received-Bytes 5864
X-Received-Body-CRC 3166775598
Xref csiph.com comp.lang.python:90595

Show key headers only | View raw


On 2015-05-14 01:06, Ethan Furman wrote:
> On 05/13/2015 04:24 PM, 20/20 Lab wrote:
>> I'm a beginner to python.  Reading here and there.  Written a couple of
>> short and simple programs to make life easier around the office.
>>
>> That being said, I'm not even sure what I need to ask for. I've never
>> worked with external data before.
>>
>> I have a LARGE csv file that I need to process.  110+ columns, 72k
>> rows.  I managed to write enough to reduce it to a few hundred rows, and
>> the five columns I'm interested in.
>>
>> 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.
>
> You could try using a dictionary, combining when needed:
>
> # untested
> data = {}
> for row in all_rows:
>     key = row[0], row[1]
>     if key in data:
>       item, qty, noise = data[key]
>       qty += row[3]
>     else:
>       item, qty, noise = row[2:]
>     data[key] = item, qty, noise
>
> for (account, staff), (item, qty, noise) in data.items():
>     do_stuff_with(account, staff, item, qty, noise)
>
> At the end, data should have what you want.  It won't, however, be in
> the same order, so hopefully that's not an issue for you.
>
Starting from that, if the order matters, you can do it this way:

data = {}
order = {}
for index, row in enumerate(all_rows):
     key = row[0], row[1]
     if key in data:
         item, qty, noise = data[key]
         qty += row[3]
     else:
         item, qty, noise = row[2:]
     data[key] = item, qty, noise
     order.setdefault(key, index)

merged_rows = [(account, staff, item, qty, noise) for (account, staff), 
(item, qty, noise) in data.items()]

def original_order(row):
     key = row[0], row[1]
     return order[key]

merged_rows.sort(key=original_order)

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Looking for direction MRAB <python@mrabarnett.plus.com> - 2015-05-14 01:41 +0100

csiph-web