Slicing iterables in sub-generators without loosing elements
The group you are posting to is a
Usenet group . Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
Newsgroups: comp.lang.python
From:
Thomas Bach <thb... @students.uni-mainz.de>
Date: Sat, 29 Sep 2012 18:14:38 +0200
Local: Sat, Sep 29 2012 12:14 pm
Subject: Slicing iterables in sub-generators without loosing elements
Hi,
say we have the following:
>>> data = [('foo', 1), ('foo', 2), ('bar', 3), ('bar', 2)]
is there a way to code a function iter_in_blocks such that
>>> result = [ list(block) for block in iter_in_blocks(data) ]
evaluates to
>>> result = [ [('foo', 1), ('foo', 2)], [('bar', 3), ('bar', 2)] ]
by _only_ _iterating_ over the list (caching all the elements sharing
the same first element doesn't count)?
I came up with the following
def iter_in_blocks(iterable):
my_iter = iter(iterable)
while True:
first = next(my_iter)
pred = lambda entry: entry[0] == first[0]
def block_iter():
yield first
for entry in itertools.takewhile(pred, my_iter):
yield entry
yield block_iter()
which does not work as itertools.takewhile consumes the first entry
not fulfilling the pred.
I currently have the intuition that the problem is not solvable
without using e.g. a global to pass something back to iter_in_blocks
from block_iter. Any other suggestions?
Regards,
Thomas Bach.
You must
Sign in before you can post messages.
You do not have the permission required to post.
Newsgroups: comp.lang.python
From:
Paul Rubin <no.em... @nospam.invalid>
Date: Sat, 29 Sep 2012 09:26:00 -0700
Subject: Re: Slicing iterables in sub-generators without loosing elements
Thomas Bach <thb
... @students.uni-mainz.de> writes:
>>>> result = [ [('foo', 1), ('foo', 2)], [('bar', 3), ('bar', 2)] ]
> by _only_ _iterating_ over the list (caching all the elements sharing
> the same first element doesn't count)?
itertools.groupby(data, lambda (x,y) : x)
is basically what you want.
You must
Sign in before you can post messages.
You do not have the permission required to post.
Newsgroups: comp.lang.python
From:
Ian Kelly <ian.g.ke... @gmail.com>
Date: Sat, 29 Sep 2012 10:36:39 -0600
Local: Sat, Sep 29 2012 12:36 pm
Subject: Re: Slicing iterables in sub-generators without loosing elements
On Sat, Sep 29, 2012 at 10:14 AM, Thomas Bach
<thb
... @students.uni-mainz.de> wrote:
> Hi,
> say we have the following:
>>>> data = [('foo', 1), ('foo', 2), ('bar', 3), ('bar', 2)]
> is there a way to code a function iter_in_blocks such that
>>>> result = [ list(block) for block in iter_in_blocks(data) ]
> evaluates to
>>>> result = [ [('foo', 1), ('foo', 2)], [('bar', 3), ('bar', 2)] ]
> by _only_ _iterating_ over the list (caching all the elements sharing
> the same first element doesn't count)?
Am I correct in understanding that the intent is that the "blocks" are
groups that share the same first item?
Is it guaranteed that the blocks will be contiguous? If so, you could
use itertools.groupby:
from itertools import groupby, imap
from operator import itemgetter
def iter_in_blocks(data):
return imap(itemgetter(1), groupby(data, itemgetter(0)))
If there is no such guarantee, then the list would need to be sorted first.
You must
Sign in before you can post messages.
You do not have the permission required to post.
Newsgroups: comp.lang.python
From:
Thomas Bach <thb... @students.uni-mainz.de>
Date: Sat, 29 Sep 2012 18:44:51 +0200
Local: Sat, Sep 29 2012 12:44 pm
Subject: Re: Slicing iterables in sub-generators without loosing elements
On Sat, Sep 29, 2012 at 09:26:00AM -0700, Paul Rubin wrote:
> Thomas Bach <thb
... @students.uni-mainz.de> writes:
> itertools.groupby(data, lambda (x,y) : x)
> is basically what you want.
True!
Thanks,
Thomas Bach
You must
Sign in before you can post messages.
You do not have the permission required to post.
Newsgroups: comp.lang.python
From:
88888 Dihedral <dihedral88... @googlemail.com>
Date: Sun, 30 Sep 2012 17:58:39 -0700 (PDT)
Local: Sun, Sep 30 2012 8:58 pm
Subject: Re: Slicing iterables in sub-generators without loosing elements
On Sunday, September 30, 2012 12:15:57 AM UTC+8, Thomas Bach wrote:
> Hi,
> say we have the following:
> >>> data = [('foo', 1), ('foo', 2), ('bar', 3), ('bar', 2)]
> is there a way to code a function iter_in_blocks such that
> >>> result = [ list(block) for block in iter_in_blocks(data) ]
> evaluates to
> >>> result = [ [('foo', 1), ('foo', 2)], [('bar', 3), ('bar', 2)] ]
> by _only_ _iterating_ over the list (caching all the elements sharing
> the same first element doesn't count)?
> I came up with the following
> def iter_in_blocks(iterable):
> my_iter = iter(iterable)
> while True:
> first = next(my_iter)
> pred = lambda entry: entry[0] == first[0]
> def block_iter():
> yield first
> for entry in itertools.takewhile(pred, my_iter):
> yield entry
> yield block_iter()
> which does not work as itertools.takewhile consumes the first entry
> not fulfilling the pred.
> I currently have the intuition that the problem is not solvable
> without using e.g. a global to pass something back to iter_in_blocks
> from block_iter. Any other suggestions?
> Regards,
> Thomas Bach.
Your question seems vague to me. If you know you are storing
only immutable tuples in a list, then the way to iterate is simple.
For example:
data = [('foo', 1), ('foo', 2), ('bar', 3), ('bar', 2)] # all tuples
for item in data: x1=item[0] # first entry in each tuple
x2=item[1]
print x1, x2 # or use yield in a function to iterate
You must
Sign in before you can post messages.
You do not have the permission required to post.
Newsgroups: comp.lang.python
From:
88888 Dihedral <dihedral88... @googlemail.com>
Date: Sun, 30 Sep 2012 17:58:39 -0700 (PDT)
Local: Sun, Sep 30 2012 8:58 pm
Subject: Re: Slicing iterables in sub-generators without loosing elements
On Sunday, September 30, 2012 12:15:57 AM UTC+8, Thomas Bach wrote:
> Hi,
> say we have the following:
> >>> data = [('foo', 1), ('foo', 2), ('bar', 3), ('bar', 2)]
> is there a way to code a function iter_in_blocks such that
> >>> result = [ list(block) for block in iter_in_blocks(data) ]
> evaluates to
> >>> result = [ [('foo', 1), ('foo', 2)], [('bar', 3), ('bar', 2)] ]
> by _only_ _iterating_ over the list (caching all the elements sharing
> the same first element doesn't count)?
> I came up with the following
> def iter_in_blocks(iterable):
> my_iter = iter(iterable)
> while True:
> first = next(my_iter)
> pred = lambda entry: entry[0] == first[0]
> def block_iter():
> yield first
> for entry in itertools.takewhile(pred, my_iter):
> yield entry
> yield block_iter()
> which does not work as itertools.takewhile consumes the first entry
> not fulfilling the pred.
> I currently have the intuition that the problem is not solvable
> without using e.g. a global to pass something back to iter_in_blocks
> from block_iter. Any other suggestions?
> Regards,
> Thomas Bach.
Your question seems vague to me. If you know you are storing
only immutable tuples in a list, then the way to iterate is simple.
For example:
data = [('foo', 1), ('foo', 2), ('bar', 3), ('bar', 2)] # all tuples
for item in data: x1=item[0] # first entry in each tuple
x2=item[1]
print x1, x2 # or use yield in a function to iterate
You must
Sign in before you can post messages.
You do not have the permission required to post.
Newsgroups: comp.lang.python
From:
Mark Lawrence <breamore... @yahoo.co.uk>
Date: Mon, 01 Oct 2012 09:19:37 +0100
Local: Mon, Oct 1 2012 4:19 am
Subject: Re: Slicing iterables in sub-generators without loosing elements
On 01/10/2012 01:58, 88888 Dihedral wrote:
> Your question seems vague to me. If you know you are storing
> only immutable tuples in a list, then the way to iterate is simple.
Does Python have a magic method that let's me use mutable tuples? I'd
also like immutable lists. Is it worth raising a feature request on the
bug tracker?
-- Cheers.
Mark Lawrence.
You must
Sign in before you can post messages.
You do not have the permission required to post.
Newsgroups: comp.lang.python
From:
Ramchandra Apte <maniandra... @gmail.com>
Date: Tue, 2 Oct 2012 09:12:04 -0700 (PDT)
Local: Tues, Oct 2 2012 12:12 pm
Subject: Re: Slicing iterables in sub-generators without loosing elements
On Monday, 1 October 2012 13:47:50 UTC+5:30, Mark Lawrence wrote:
> On 01/10/2012 01:58, 88888 Dihedral wrote:
> > Your question seems vague to me. If you know you are storing
> > only immutable tuples in a list, then the way to iterate is simple.
> Does Python have a magic method that let's me use mutable tuples? I'd
> also like immutable lists. Is it worth raising a feature request on the
> bug tracker?
> --
> Cheers.
> Mark Lawrence.
Mark, you are talking to a bot.
You must
Sign in before you can post messages.
You do not have the permission required to post.
Newsgroups: comp.lang.python
From:
Ramchandra Apte <maniandra... @gmail.com>
Date: Tue, 2 Oct 2012 09:12:04 -0700 (PDT)
Local: Tues, Oct 2 2012 12:12 pm
Subject: Re: Slicing iterables in sub-generators without loosing elements
On Monday, 1 October 2012 13:47:50 UTC+5:30, Mark Lawrence wrote:
> On 01/10/2012 01:58, 88888 Dihedral wrote:
> > Your question seems vague to me. If you know you are storing
> > only immutable tuples in a list, then the way to iterate is simple.
> Does Python have a magic method that let's me use mutable tuples? I'd
> also like immutable lists. Is it worth raising a feature request on the
> bug tracker?
> --
> Cheers.
> Mark Lawrence.
Mark, you are talking to a bot.
You must
Sign in before you can post messages.
You do not have the permission required to post.
Newsgroups: comp.lang.python
From:
Mark Lawrence <breamore... @yahoo.co.uk>
Date: Tue, 02 Oct 2012 17:44:45 +0100
Local: Tues, Oct 2 2012 12:44 pm
Subject: Re: Slicing iterables in sub-generators without loosing elements
On 02/10/2012 17:12, Ramchandra Apte wrote:
> On Monday, 1 October 2012 13:47:50 UTC+5:30, Mark Lawrence wrote:
>> On 01/10/2012 01:58, 88888 Dihedral wrote:
>>> Your question seems vague to me. If you know you are storing
>>> only immutable tuples in a list, then the way to iterate is simple.
>> Does Python have a magic method that let's me use mutable tuples? I'd
>> also like immutable lists. Is it worth raising a feature request on the
>> bug tracker?
>> --
>> Cheers.
>> Mark Lawrence.
> Mark, you are talking to a bot.
What happened to freedom of speech? If I want to talk to a bot, I'll
talk to a bot. Besides I'm not convinced it/he/she is a bot. Plus if
you read my post carefully, add in several years experience of Python
the language and Python the comedy, you might come to the conclusion
that a certain amount of urine extraction was going on :)
-- Cheers.
Mark Lawrence.
You must
Sign in before you can post messages.
You do not have the permission required to post.
Newsgroups: comp.lang.python
From:
Chris Angelico <ros... @gmail.com>
Date: Wed, 3 Oct 2012 03:58:02 +1000
Local: Tues, Oct 2 2012 1:58 pm
Subject: Re: Slicing iterables in sub-generators without loosing elements
On Wed, Oct 3, 2012 at 2:44 AM, Mark Lawrence <breamore
... @yahoo.co.uk> wrote:
> What happened to freedom of speech? If I want to talk to a bot, I'll talk
> to a bot. Besides I'm not convinced it/he/she is a bot. Plus if you read
> my post carefully, add in several years experience of Python the language
> and Python the comedy, you might come to the conclusion that a certain
> amount of urine extraction was going on :)
Coupled with a bit of bot-seeding, which is always fun.
One of these days I'm going to mail Dihedral a whole pile of Gilbert
and Sullivan operetta and see if any of it comes back in his posts...
Dihedral might be a bot and might not. I've come to the conclusion
that it's not worth trying to find out, given that a good bot can
outdo a lot of humans in useful conversation.
ChrisA
You must
Sign in before you can post messages.
You do not have the permission required to post.
Newsgroups: comp.lang.python
From:
Mark Lawrence <breamore... @yahoo.co.uk>
Date: Tue, 02 Oct 2012 19:35:55 +0100
Local: Tues, Oct 2 2012 2:35 pm
Subject: Re: Slicing iterables in sub-generators without loosing elements
On 02/10/2012 18:58, Chris Angelico wrote:
> Dihedral might be a bot and might not. I've come to the conclusion
> that it's not worth trying to find out, given that a good bot can
> outdo a lot of humans in useful conversation.
> ChrisA
Try telling that to the newbies on the Python tutor mailing list and
they simply won't believe ya :)
-- Cheers.
Mark Lawrence.
You must
Sign in before you can post messages.
You do not have the permission required to post.
Newsgroups: comp.lang.python
From:
Terry Reedy <tjre... @udel.edu>
Date: Tue, 02 Oct 2012 14:59:04 -0400
Local: Tues, Oct 2 2012 2:59 pm
Subject: Re: Slicing iterables in sub-generators without loosing elements
On 10/2/2012 1:58 PM, Chris Angelico wrote:
> On Wed, Oct 3, 2012 at 2:44 AM, Mark Lawrence <breamore
... @yahoo.co.uk> wrote:
>> What happened to freedom of speech? If I want to talk to a bot, I'll talk
>> to a bot. Besides I'm not convinced it/he/she is a bot. Plus if you read
>> my post carefully, add in several years experience of Python the language
>> and Python the comedy, you might come to the conclusion that a certain
>> amount of urine extraction was going on :)
> Coupled with a bit of bot-seeding, which is always fun.
> One of these days I'm going to mail Dihedral a whole pile of Gilbert
> and Sullivan operetta and see if any of it comes back in his posts...
> Dihedral might be a bot and might not. I've come to the conclusion
> that it's not worth trying to find out, given that a good bot can
> outdo a lot of humans in useful conversation.
I just read that bots playing Unreal Tournament with an 'act human'
module seem more human to at least some humans than humans. Perhaps that
is because humans have to become bot-like to play UT well.
-- Terry Jan Reedy
You must
Sign in before you can post messages.
You do not have the permission required to post.
Newsgroups: comp.lang.python
From:
88888 Dihedral <dihedral88... @googlemail.com>
Date: Tue, 2 Oct 2012 13:54:20 -0700 (PDT)
Local: Tues, Oct 2 2012 4:54 pm
Subject: Re: Slicing iterables in sub-generators without loosing elements
On Monday, October 1, 2012 4:17:50 PM UTC+8, Mark Lawrence wrote:
> On 01/10/2012 01:58, 88888 Dihedral wrote:
> > Your question seems vague to me. If you know you are storing
> > only immutable tuples in a list, then the way to iterate is simple.
> Does Python have a magic method that let's me use mutable tuples? I'd
> also like immutable lists. Is it worth raising a feature request on the
> bug tracker?
> --
> Cheers.
> Mark Lawrence.
Python resolves objects by names and dynamical types.
This will force the programmer to write structured programs.
You must
Sign in before you can post messages.
You do not have the permission required to post.
Newsgroups: comp.lang.python
From:
88888 Dihedral <dihedral88... @googlemail.com>
Date: Tue, 2 Oct 2012 13:54:20 -0700 (PDT)
Local: Tues, Oct 2 2012 4:54 pm
Subject: Re: Slicing iterables in sub-generators without loosing elements
On Monday, October 1, 2012 4:17:50 PM UTC+8, Mark Lawrence wrote:
> On 01/10/2012 01:58, 88888 Dihedral wrote:
> > Your question seems vague to me. If you know you are storing
> > only immutable tuples in a list, then the way to iterate is simple.
> Does Python have a magic method that let's me use mutable tuples? I'd
> also like immutable lists. Is it worth raising a feature request on the
> bug tracker?
> --
> Cheers.
> Mark Lawrence.
Python resolves objects by names and dynamical types.
This will force the programmer to write structured programs.
You must
Sign in before you can post messages.
You do not have the permission required to post.
Newsgroups: comp.lang.python
From:
Steven D'Aprano <steve+comp.lang.pyt... @pearwood.info>
Date: 03 Oct 2012 00:57:20 GMT
Local: Tues, Oct 2 2012 8:57 pm
Subject: Re: Slicing iterables in sub-generators without loosing elements
On Wed, 03 Oct 2012 03:58:02 +1000, Chris Angelico wrote:
> Dihedral might be a bot and might not. I've come to the conclusion that
> it's not worth trying to find out, given that a good bot can outdo a lot
> of humans in useful conversation.
Oh, I'm convinced that it's a bot.
The fact that Dihedral never responds to conversations about him/it is a give away: nearly all people are far to egotistical to let accusations of bot-hood go unchallenged. And even though its responses are grammatically correct, they often are semantically meaningless. Even the best AIs (Evie, CleverBot, Alice) don't come close to passing the Turing test. It helps that Dihedral only talks about computer programming, but even so, it has well and truly failed my personal Turing test.
Mind you, some human beings fail the Turing test too:
http://northernplanets.blogspot.com.au/2006/06/failing-turing-test.html
Of course, Dihedral's creator wanted to really confound us, the occasional human response would probably muddy the waters sufficiently.
-- Steven
You must
Sign in before you can post messages.
You do not have the permission required to post.
Newsgroups: comp.lang.python
From:
88888 Dihedral <dihedral88... @googlemail.com>
Date: Tue, 2 Oct 2012 18:11:20 -0700 (PDT)
Local: Tues, Oct 2 2012 9:11 pm
Subject: Re: Slicing iterables in sub-generators without loosing elements
Steven D'Aprano於 2012年10月3日星期三UTC+8上午8時57分20秒寫道:
> On Wed, 03 Oct 2012 03:58:02 +1000, Chris Angelico wrote:
> > Dihedral might be a bot and might not. I've come to the conclusion that
> > it's not worth trying to find out, given that a good bot can outdo a lot
> > of humans in useful conversation.
> Oh, I'm convinced that it's a bot.
> The fact that Dihedral never responds to conversations about him/it is a
> give away: nearly all people are far to egotistical to let accusations of
> bot-hood go unchallenged. And even though its responses are grammatically
> correct, they often are semantically meaningless. Even the best AIs
> (Evie, CleverBot, Alice) don't come close to passing the Turing test. It
> helps that Dihedral only talks about computer programming, but even so,
> it has well and truly failed my personal Turing test.
> Mind you, some human beings fail the Turing test too:
> http://northernplanets.blogspot.com.au/2006/06/failing-turing-test.html
> Of course, Dihedral's creator wanted to really confound us, the
> occasional human response would probably muddy the waters sufficiently.
> --
> Steven
Are you still not getting the generator part in Python?
Do you really test any generator before?
You must
Sign in before you can post messages.
You do not have the permission required to post.
Newsgroups: comp.lang.python
From:
Steven D'Aprano <steve+comp.lang.pyt... @pearwood.info>
Date: 03 Oct 2012 01:24:13 GMT
Local: Tues, Oct 2 2012 9:24 pm
Subject: Re: Slicing iterables in sub-generators without loosing elements
On Tue, 02 Oct 2012 18:11:20 -0700, 88888 Dihedral wrote:
> Steven D'Aprano於 2012年10月3日星期三UTC+8上午8時57分20秒寫道:
>> Oh, I'm convinced that it's a bot.
>> The fact that Dihedral never responds to conversations about him/it is
>> a give away: nearly all people are far to egotistical to let
>> accusations of bot-hood go unchallenged.
[...]
>> Of course, Dihedral's creator wanted to really confound us, the
>> occasional human response would probably muddy the waters sufficiently.
> Are you still not getting the generator part in Python? Do you really
> test any generator before?
I rest my case :)
I have many problems with generators. Can you tell me more about them? I really need your help. Sometimes, when it is very cold, the diesel freezes and the generator raises StopIteration at the wrong place.
-- Steven
You must
Sign in before you can post messages.
You do not have the permission required to post.
Newsgroups: comp.lang.python
From:
88888 Dihedral <dihedral88... @googlemail.com>
Date: Tue, 2 Oct 2012 18:42:19 -0700 (PDT)
Local: Tues, Oct 2 2012 9:42 pm
Subject: Re: Slicing iterables in sub-generators without loosing elements
Steven D'Aprano於 2012年10月3日星期三UTC+8上午9時24分13秒寫道:
> On Tue, 02 Oct 2012 18:11:20 -0700, 88888 Dihedral wrote:
> > Steven D'Aprano於 2012年10月3日星期三UTC+8上午8時57分20秒寫道:
> >> Oh, I'm convinced that it's a bot.
> >> The fact that Dihedral never responds to conversations about him/it is
> >> a give away: nearly all people are far to egotistical to let
> >> accusations of bot-hood go unchallenged.
> [...]
> >> Of course, Dihedral's creator wanted to really confound us, the
> >> occasional human response would probably muddy the waters sufficiently.
> > Are you still not getting the generator part in Python? Do you really
> > test any generator before?
> I rest my case :)
> I have many problems with generators. Can you tell me more about them? I
> really need your help. Sometimes, when it is very cold, the diesel
> freezes and the generator raises StopIteration at the wrong place.
> --
> Steven
Please work out your own templates as I showed in the decorator part.
You must
Sign in before you can post messages.
You do not have the permission required to post.
Newsgroups: comp.lang.python
From:
Ramchandra Apte <maniandra... @gmail.com>
Date: Tue, 2 Oct 2012 20:21:17 -0700 (PDT)
Local: Tues, Oct 2 2012 11:21 pm
Subject: Re: Slicing iterables in sub-generators without loosing elements
On Tuesday, 2 October 2012 22:13:20 UTC+5:30, Mark Lawrence wrote:
> On 02/10/2012 17:12, Ramchandra Apte wrote:
> > On Monday, 1 October 2012 13:47:50 UTC+5:30, Mark Lawrence wrote:
> >> On 01/10/2012 01:58, 88888 Dihedral wrote:
> >>> Your question seems vague to me. If you know you are storing
> >>> only immutable tuples in a list, then the way to iterate is simple.
> >> Does Python have a magic method that let's me use mutable tuples? I'd
> >> also like immutable lists. Is it worth raising a feature request on the
> >> bug tracker?
> >> --
> >> Cheers.
> >> Mark Lawrence.
> > Mark, you are talking to a bot.
> What happened to freedom of speech? If I want to talk to a bot, I'll
> talk to a bot. Besides I'm not convinced it/he/she is a bot. Plus if
> you read my post carefully, add in several years experience of Python
> the language and Python the comedy, you might come to the conclusion
> that a certain amount of urine extraction was going on :)
> --
> Cheers.
> Mark Lawrence.
Never said you can't.
You must
Sign in before you can post messages.
You do not have the permission required to post.
Newsgroups: comp.lang.python
From:
Ramchandra Apte <maniandra... @gmail.com>
Date: Tue, 2 Oct 2012 20:21:17 -0700 (PDT)
Local: Tues, Oct 2 2012 11:21 pm
Subject: Re: Slicing iterables in sub-generators without loosing elements
On Tuesday, 2 October 2012 22:13:20 UTC+5:30, Mark Lawrence wrote:
> On 02/10/2012 17:12, Ramchandra Apte wrote:
> > On Monday, 1 October 2012 13:47:50 UTC+5:30, Mark Lawrence wrote:
> >> On 01/10/2012 01:58, 88888 Dihedral wrote:
> >>> Your question seems vague to me. If you know you are storing
> >>> only immutable tuples in a list, then the way to iterate is simple.
> >> Does Python have a magic method that let's me use mutable tuples? I'd
> >> also like immutable lists. Is it worth raising a feature request on the
> >> bug tracker?
> >> --
> >> Cheers.
> >> Mark Lawrence.
> > Mark, you are talking to a bot.
> What happened to freedom of speech? If I want to talk to a bot, I'll
> talk to a bot. Besides I'm not convinced it/he/she is a bot. Plus if
> you read my post carefully, add in several years experience of Python
> the language and Python the comedy, you might come to the conclusion
> that a certain amount of urine extraction was going on :)
> --
> Cheers.
> Mark Lawrence.
Never said you can't.
You must
Sign in before you can post messages.
You do not have the permission required to post.