A
Anthra Norell
Hi,
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)
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)?
table = zip (&arbitrary_number_of_lists) # I invent '&' to illustrate
I preclude passing a pre-zipped table as a solution, because all function-related argument processing should be done by the
function, not by the caller. Supposing my tabulator should auto-format a printout. It would need to analyze each column (e.g.
maxima, minima, max length of strings, etc.) That would be a lot simpler with column lists than with record lists, unless I undo
what the caller had to do, because the function couldn't ... a lot of in and out ...
Of course I could code a code edit and exec () it.
names_of_lists = ','.join (['list%d' % n for n in range (len (arbitrary_number_of_lists)])
exec ('"%s = arbitrary_number_of_lists"'% names_of_lists)
exec ('"table = zip (%s)"' % names_of_lists)
That should work, but it looks loathsome to me.
Has anyone come across a similar problem and found an elegant solution he might want to share?
Frederic
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)
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)?
table = zip (&arbitrary_number_of_lists) # I invent '&' to illustrate
I preclude passing a pre-zipped table as a solution, because all function-related argument processing should be done by the
function, not by the caller. Supposing my tabulator should auto-format a printout. It would need to analyze each column (e.g.
maxima, minima, max length of strings, etc.) That would be a lot simpler with column lists than with record lists, unless I undo
what the caller had to do, because the function couldn't ... a lot of in and out ...
Of course I could code a code edit and exec () it.
names_of_lists = ','.join (['list%d' % n for n in range (len (arbitrary_number_of_lists)])
exec ('"%s = arbitrary_number_of_lists"'% names_of_lists)
exec ('"table = zip (%s)"' % names_of_lists)
That should work, but it looks loathsome to me.
Has anyone come across a similar problem and found an elegant solution he might want to share?
Frederic