DXOracle / PyADO - convert lists of tuples to list of lists
Author |
Message |
Haral #1 / 5
|
 DXOracle / PyADO - convert lists of tuples to list of lists
Hello, I built an application with PyADO, connecting to oracle. Now a kind member of this newsgroup sent me prebuilt binaries of DXOracle, and I tried to exchange PyADO with DXOracle. But: when doing a query, PyADO returns a list of lists, like [["James","Curtis","singer","somewhere"], ["Sandra","Bullock","actress","ny"]] DXOracle returns a list of tuples [("James","Curtis","singer","somewhere",), ("Sandra","Bullock","actress","ny",)] So my application stumbles because of minor differences between lists and tuples. To convert I can do: a=[('nasen','baeren',23,),('nasen','drosseln',12)] #the list of tuples d=[] # empty list for i in a: ... d.append(list(i)) which returns: [['nasen', 'baeren', 23], ['nasen', 'drosseln', 12]] .. but it seems very ugly ... to move that much data around. Isn't there a faster & more elegant way?
|
Wed, 01 Jun 2005 09:40:57 GMT |
|
 |
Gerhard H?rin #2 / 5
|
 DXOracle / PyADO - convert lists of tuples to list of lists
Quote: > Hello, > I built an application with PyADO, connecting to oracle. > Now a kind member of this newsgroup sent me prebuilt binaries of > DXOracle, and I tried to exchange PyADO with DXOracle.
:-) Quote: > But: > when doing a query, PyADO returns a list of lists, like > [["James","Curtis","singer","somewhere"], > ["Sandra","Bullock","actress","ny"]] > DXOracle returns a list of tuples > [("James","Curtis","singer","somewhere",), > ("Sandra","Bullock","actress","ny",)] > So my application stumbles because of minor differences between lists and > tuples.
Lists are mutable, tuples aren't. Tuples are also hashable (because they're immutable). Unless your app depends on one of these properties, it shouldn't 'stumble'. Quote: > To convert I can do: > [complicated way snipped] > .. but it seems very ugly ... to move that much data around. Isn't there > a faster & more elegant way?
To convert a list (or any sequence) to a tuple, use tuple(seq) Similarly, to convert a tuple (or any sequence) to a list, use list(seq) It's really as easy as that. ciao, -- Gerhard
|
Wed, 01 Jun 2005 10:44:37 GMT |
|
 |
Terry Hancoc #3 / 5
|
 DXOracle / PyADO - convert lists of tuples to list of lists
Quote: > Lists are mutable, tuples aren't. Tuples are also hashable (because > they're immutable). Unless your app depends on one of these properties, > it shouldn't 'stumble'.
Take care with that statement... Quote: >>> hash( ([1,'2'], {'shinobu':1}, 'ataru') )
Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: list objects are unhashable
i.e. it depends on what's in the tuple. Hashable means immutable throughout. Cheers, Terry -- Terry Hancock ( hancock at anansispaceworks.com ) Anansi Spaceworks http://www.anansispaceworks.com "Some things are too important to be taken seriously"
|
Wed, 01 Jun 2005 14:34:48 GMT |
|
 |
Haral #4 / 5
|
 DXOracle / PyADO - convert lists of tuples to list of lists
Gerhard, the solutions from you with the timing added: d=[] # empty list for i in fetchall(): d.append(list(i)) maybe written as d=[list(i) for i in fetchall()] or as d=map(list,fetchall()) With timing of Dauer 3.98594933547 for Dauer 3.52617203411 list Dauer 2.3300359878 map best wishes Harald
|
Thu, 02 Jun 2005 16:56:43 GMT |
|
 |
Bo M. Maryniuc #5 / 5
|
 DXOracle / PyADO - convert lists of tuples to list of lists
Quote: > Isn't there a faster & more elegant way?
Yes. Use DCOracle2. ;-) -- Regards, Bogdan A computer without any MS Windows is like a fish without a bicycle. -- With apologies to Gloria Steinem
|
Fri, 03 Jun 2005 15:41:01 GMT |
|
|
|