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


Groups > comp.lang.python > #43973

Re: itertools.groupby

Path csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
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; 'else:': 0.03; 'output': 0.05; 'append': 0.09; 'logic': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'def': 0.12; 'wrote': 0.14; "'b',": 0.16; "['a',": 0.16; 'counters,': 0.16; 'itertools': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'sequence.': 0.16; 'sequential': 0.16; 'sat,': 0.16; 'basically': 0.19; 'input': 0.22; 'import': 0.22; 'skip:l 30': 0.24; 'url:home': 0.24; 'equivalent': 0.26; 'this:': 0.26; 'header:X -Complaints-To:1': 0.27; 'function': 0.29; 'wondering': 0.29; 'skip:g 30': 0.30; "i'm": 0.30; 'group:': 0.31; 'overhead': 0.31; 'subject:skip:i 10': 0.31; 'probably': 0.32; 'skip:- 30': 0.32; 'skip:# 10': 0.33; "i'd": 0.34; 'skip:u 20': 0.35; 'but': 0.35; 'there': 0.35; 'data,': 0.36; 'charset:us-ascii': 0.36; 'list': 0.37; 'list.': 0.37; 'received:76': 0.38; 'jason': 0.38; 'to:addr :python-list': 0.38; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'received:org': 0.40; 'new': 0.61; "'2',": 0.84; "'3',": 0.84; 'lists:': 0.91; 'wanting': 0.93; '2013': 0.98
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Dennis Lee Bieber <wlfraed@ix.netcom.com>
Subject Re: itertools.groupby
Date Sat, 20 Apr 2013 19:39:58 -0400
Organization > Bestiaria Support Staff <
References <CANy1k1jVD3xDQGVdGRuCycg4yXojtqKfAzZFM=C88SsHnqrLQQ@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=us-ascii
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host adsl-76-249-17-230.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 <python-list.python.org>
List-Unsubscribe <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.865.1366501207.3114.python-list@python.org> (permalink)
Lines 64
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1366501207 news.xs4all.nl 2240 [2001:888:2000:d::a6]:42915
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:43973

Show key headers only | View raw


On Sat, 20 Apr 2013 11:09:42 -0600, Jason Friedman <jsf80238@gmail.com>
declaimed the following in gmane.comp.python.general:


> I am wanting a list of lists:
> ['a', 'b', 'c']
> ['1', '2', '3', '4']
> ['X', 'Y', 'Z']
> []
> 
> I wrote this:
> ------------------------------------
> #!/usr/bin/python3
> from itertools import groupby
> 
> def get_lines_from_file(file_name):
>     with open(file_name) as reader:
>         for line in reader.readlines():
>             yield(line.strip())
> 
> counter = 0
> def key_func(x):
>     if x.startswith("Starting a new group"):
>         global counter
>         counter += 1
>     return counter
> 
> for key, group in groupby(get_lines_from_file("my_data"), key_func):
>     print(list(group)[1:])

	Given that the input is already grouped, in sequential terms...

	I'd probably avoid the whole groupby overhead since the processing
is basically equivalent to a "report break".

	Untested/pseudo-code (I suspect this logic won't give you the empty
last group...)

result = []
group = None
for line in fin:
		if line.startswith("Starting..."):
			if group:
				result.append(group)
			group = []
		else:
			group.append(line)

	Hmmm... last group handling?

if result and group == []:
	result.append(group)
	# if result we had data, and if group == [] the last input was
"start a new group", so append the empty list.

> I get the output I desire, but I'm wondering if there is a solution without
> the global counter.

	No counters, no esoteric function calls... Just a straight through
read/collect sequence.
-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
        wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

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


Thread

Re: itertools.groupby Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-04-20 19:39 -0400

csiph-web