unpaking sequences of unknown length
Diez B. Roggisch
deets at nospam.web.de
Sun Aug 27 07:55:50 EDT 2006
> I keep working around a little problem with unpacking in cases in which I don't know how many elements I get. Consider this:
>
> def tabulate_lists (*arbitray_number_of_lists):
> table = zip (arbitray_number_of_lists)
> for record in table:
> # etc ...
>
> This does not work, because the zip function also has an *arg parameter, which expects an arbitrary length enumeration of arguments
> which it would turn into a tuple (lists in this case). Now my function does exactly the same thing ahead of zip. So, before I pass
> the tuple "arbitrary_number_of_lists" to zip, I 'd need to unpack it but the only way I know of is into variables:
>
> list1, list2, list3 = arbitrary_number_of_lists
> zip (list1, list2, list3)
I don't get your problem here. This works for me:
args = [range(5) for i in xrange(5)]
print zip(*args)
> With arbitrary number of lists it cannot be done this way.
>
> Question: Is there an unpacking mechanism for cases in which I don't know--and don't need to know--how many elements I get, or an
> argument passing mechanism that is the inverse of the tuplifier (*args)?
No.
It looks a little bit as if you aren't aware of the symetry behind the *
and **-argument-passing schemes. I suggest reading up on them.
Diez
More information about the Python-list
mailing list