[Tutor] constructing tupples in 1.5.2
Alexandre Ratti
alex@gabuzomeu.net
Sat, 16 Mar 2002 15:39:01 +0100
Hello Pijus,
At 04:33 16/03/2002 -0500, you wrote:
>From: "Pijus Virketis" <virketis@fas.harvard.edu>
>To: <tutor@python.org>
>Date: Sat, 16 Mar 2002 01:13:00 -0500
>Subject: [Tutor] constructing tupples in 1.5.2
>I am writting a MySQL script, which will be deployed on Py1.5.2. The call
>to the database made through MySQLdb module must be in the form of a list
>of tupples (e.g. [("John", 33, "M"), ("Sally", 35, "F"), ...].
>The thing is, I need to construct these tupples on the fly: I have a
>dictionary of values (e.g. {"name": "John", "age": 33, ... })
Do you have a lot of information in these dictionaries (eg. do you need to
store a lot of information for every person)?
>and I want to put the right values in the right places in a tupple. In
>Py2.2 this is easy, because the tupple object has a handy __add__ method,
>which concatenates two tupples. Py1.5.2, unfortunately, does not support
>this method. What would be the most effective way of doing this? A
>workaround I have though of is updating the table entry-by-entry, rather
>than making one big update call, but it's obviously less efficient to run all
>these INSERT querries.
Can you modify your dictionary structure if needed? Eg. could you use a
tuple as dictionary key?
toto = {(1, "name"): "John", (2, "age"): 33, (3, "truc"): "muche"}
I haven't thought this out, but it might be useful to reconstruct the
correct tuple order. Example:
titi = map(lambda x,y : x + (y,), toto.keys(), toto.values())
titi.sort()
[(1, 'name', 'John'), (2, 'age', 33), (3, 'truc', 'muche')]
Just a stab in the dark...
Cheers.
Alexandre