Reading 3 objects at a time from list

Matteo tadwelessar at gmail.com
Sat Apr 11 04:44:24 EDT 2009


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]):
    func(*triple)

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...

any ideas?



More information about the Python-list mailing list