Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #30579
| Newsgroups | comp.lang.python |
|---|---|
| Date | 2012-09-30 17:58 -0700 |
| References | <mailman.1636.1348935354.27098.python-list@python.org> |
| Subject | Re: Slicing iterables in sub-generators without loosing elements |
| From | 88888 Dihedral <dihedral88888@googlemail.com> |
| Message-ID | <mailman.1687.1349053129.27098.python-list@python.org> (permalink) |
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
Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll thread
Slicing iterables in sub-generators without loosing elements Thomas Bach <thbach@students.uni-mainz.de> - 2012-09-29 18:14 +0200
Re: Slicing iterables in sub-generators without loosing elements Paul Rubin <no.email@nospam.invalid> - 2012-09-29 09:26 -0700
Re: Slicing iterables in sub-generators without loosing elements Thomas Bach <thbach@students.uni-mainz.de> - 2012-09-29 18:44 +0200
Re: Slicing iterables in sub-generators without loosing elements 88888 Dihedral <dihedral88888@googlemail.com> - 2012-09-30 17:58 -0700
Re: Slicing iterables in sub-generators without loosing elements Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-10-01 09:19 +0100
Re: Slicing iterables in sub-generators without loosing elements Ramchandra Apte <maniandram01@gmail.com> - 2012-10-02 09:12 -0700
Re: Slicing iterables in sub-generators without loosing elements Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-10-02 17:44 +0100
Re: Slicing iterables in sub-generators without loosing elements Ramchandra Apte <maniandram01@gmail.com> - 2012-10-02 20:21 -0700
Re: Slicing iterables in sub-generators without loosing elements Ramchandra Apte <maniandram01@gmail.com> - 2012-10-02 20:21 -0700
Re: Slicing iterables in sub-generators without loosing elements Chris Angelico <rosuav@gmail.com> - 2012-10-03 03:58 +1000
Re: Slicing iterables in sub-generators without loosing elements Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-10-03 00:57 +0000
Re: Slicing iterables in sub-generators without loosing elements 88888 Dihedral <dihedral88888@googlemail.com> - 2012-10-02 18:11 -0700
Re: Slicing iterables in sub-generators without loosing elements Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-10-03 01:24 +0000
Re: Slicing iterables in sub-generators without loosing elements 88888 Dihedral <dihedral88888@googlemail.com> - 2012-10-02 18:42 -0700
Re: Slicing iterables in sub-generators without loosing elements Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-10-02 19:35 +0100
Re: Slicing iterables in sub-generators without loosing elements Terry Reedy <tjreedy@udel.edu> - 2012-10-02 14:59 -0400
Re: Slicing iterables in sub-generators without loosing elements Ramchandra Apte <maniandram01@gmail.com> - 2012-10-02 09:12 -0700
Re: Slicing iterables in sub-generators without loosing elements 88888 Dihedral <dihedral88888@googlemail.com> - 2012-10-02 13:54 -0700
Re: Slicing iterables in sub-generators without loosing elements 88888 Dihedral <dihedral88888@googlemail.com> - 2012-10-02 13:54 -0700
Re: Slicing iterables in sub-generators without loosing elements 88888 Dihedral <dihedral88888@googlemail.com> - 2012-09-30 17:58 -0700
csiph-web