Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #19965 > unrolled thread
| Started by | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| First post | 2012-02-07 09:03 -0700 |
| Last post | 2012-02-08 01:50 +0000 |
| Articles | 2 — 2 participants |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: iterating over list with one mising value Ian Kelly <ian.g.kelly@gmail.com> - 2012-02-07 09:03 -0700
Re: iterating over list with one mising value Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-02-08 01:50 +0000
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2012-02-07 09:03 -0700 |
| Subject | Re: iterating over list with one mising value |
| Message-ID | <mailman.5503.1328630667.27778.python-list@python.org> |
On Tue, Feb 7, 2012 at 5:27 AM, Sammy Danso <samdansobe@yahoo.com> wrote: > > Hello experts, > I am having trouble accessing the content of my list. > my list content has 2-pair value with the exception of one which has single value. here is an example ['a', 1, 'b', 1, 'c', 3, 'd'] > > I am unable to iterate through list to access invidual value pairs > > I get an error message saying ' the list should more than 1 value pairs'. I guess because 'd' has no value. How do I solve this problem? What are you using to iterate through pairs instead of individual elements? It sounds like it is not very flexible. I would recommend instead using the "grouper" recipe from the itertools documentation: http://docs.python.org/library/itertools.html#recipes Cheers, Ian
[toc] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2012-02-08 01:50 +0000 |
| Message-ID | <4f31d500$0$29992$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #19965 |
(Apologies in advance for breaking threading, but the original post in
this thread doesn't appear for me.)
> On Tue, Feb 7, 2012 at 5:27 AM, Sammy Danso <samdansobe@yahoo.com>
> wrote:
>>
>> Hello experts,
>> I am having trouble accessing the content of my list. my list content
>> has 2-pair value with the exception of one which has single value. here
>> is an example ['a', 1, 'b', 1, 'c', 3, 'd']
>>
>> I am unable to iterate through list to access invidual value pairs
Here's a recipe to collate a sequence of interleaved items. It collects
every nth item:
from itertools import islice, tee
def collate(iterable, n):
t = tee(iter(iterable), n)
slices = (islice(it, i, None, n) for (i, it) in enumerate(t))
return [list(slice) for slice in slices]
And here it is in use:
>>> seq = [1, 'a', 'A', 2, 'b', 'B', 3, 'c', 'C', 4, 'd', 'D', 5, 'e']
>>> collate(seq, 3)
[[1, 2, 3, 4, 5], ['a', 'b', 'c', 'd', 'e'], ['A', 'B', 'C', 'D']]
>>> collate(seq, 2)
[[1, 'A', 'b', 3, 'C', 'd', 5], ['a', 2, 'B', 'c', 4, 'D', 'e']]
>>> collate(seq, 9)
[[1, 4], ['a', 'd'], ['A', 'D'], [2, 5], ['b', 'e'], ['B'], [3], ['c'],
['C']]
--
Steven
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web