Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Slicing iterables in sub-generators without loosing elements (http://www.velocityreviews.com/forums/t952851-slicing-iterables-in-sub-generators-without-loosing-elements.html)

Thomas Bach 09-29-2012 04:14 PM

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.

Paul Rubin 09-29-2012 04:26 PM

Re: Slicing iterables in sub-generators without loosing elements
 
Thomas Bach <thbach@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.

Thomas Bach 09-29-2012 04:44 PM

Re: Slicing iterables in sub-generators without loosing elements
 
On Sat, Sep 29, 2012 at 09:26:00AM -0700, Paul Rubin wrote:
> Thomas Bach <thbach@students.uni-mainz.de> writes:
>
> itertools.groupby(data, lambda (x,y) : x)
>
> is basically what you want.


True!

Thanks,
Thomas Bach

88888 Dihedral 10-01-2012 12:58 AM

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





88888 Dihedral 10-01-2012 12:58 AM

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





Mark Lawrence 10-01-2012 08:19 AM

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.


Ramchandra Apte 10-02-2012 04:12 PM

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.

Ramchandra Apte 10-02-2012 04:12 PM

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.

Mark Lawrence 10-02-2012 04:44 PM

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.


Chris Angelico 10-02-2012 05:58 PM

Re: Slicing iterables in sub-generators without loosing elements
 
On Wed, Oct 3, 2012 at 2:44 AM, Mark Lawrence <breamoreboy@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


All times are GMT. The time now is 07:18 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.