Reading 3 objects at a time from list
clp2 at rebertia.com
Sat Apr 11 10:51:24 CEST 2009
On Sat, Apr 11, 2009 at 1:44 AM, Matteo <tadwelessar at gmail.com> wrote:
> Hi all,
> let's see if there is a more "pythonic" way of doing what I'm trying
> to do.
> I have a lot of strings with numbers like this one:
> string = "-1 1.3 100.136 1 2.6 100.726 1 3.9 101.464 -1 5.2 102.105"
> I need to pass the numbers to a function, but three at a time, until
> the string ends. The strings are of variable length, but always a
> multiple of three.
> That's what I did:
> num = string.split()
> for triple in zip(num[::3], num[1::3], num[2::3]):
> it works and I like slices, but I was wondering if there was another
> way of doing the same thing, maybe reading the numbers in groups of
> arbitrary length n...
See the grouper() recipe in the `itertools` module --
I have a blog:
More information about the Python-list