DXOracle / PyADO - convert lists of tuples to list of lists 
Author Message
 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  
 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  
 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  
 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  
 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  
 
 [ 5 post ] 

 Relevant Pages 

1. Pymacs - tuples/lists .vs. proper-lists/vectors

2. how do i convert list->tuple?

3. converting tuple to list

4. converting tuple to a list

5. converting a list into a tuple?

6. How convert all list/tuple...

7. How to convert unicode list to ascii list?

8. Intersection of multiple lists/list of lists

9. tuple to list to tuple conversion

10. How can I convert a SATHER list of lists to a TCL list of lists?

11. How to convert list of tuples into a single list of unique values ?

12. How to convert a list/tuple into an function argument list easily?

 

 
Powered by phpBB® Forum Software