nested tuples

L

Luis P. Mendes

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi,

I'm trying to solve this problem:

suppose I'm reading a csv file and want to create a tuple of all those
rows and values, like ((row1value1, row1value2, row1value3),(row2value1,
row2value2, row2value3),..., (rowNvalue1, rowNvalue2, rowNvalue3))

I haven't found the way to do it just using tuples. How can I do it?

Nevertheless, I can solve it like this:
a=[]

for row in reader:
~ elem = (row[0],row[1],row[2])
~ a.append(elem)

which will result in a list of tuples: [(row1value1, row1value2,
row1value3),(row2value1, row2value2, row2value3),..., (rowNvalue1,
rowNvalue2, rowNvalue3)]

Then, I get what I want with tuple(a).



Luis P. Mendes
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFDIdcvHn4UHCY8rB8RAjBnAJ9hLzbMZVZf3vLZ0iCs2ptK0v6RPwCfRr1S
GHdoy/APTZSWy+rLBA+i0KE=
=vyR6
-----END PGP SIGNATURE-----
 
M

Max Erickson

Luis P. Mendes said:
suppose I'm reading a csv file and want to create a tuple of all
those rows and values, like ((row1value1, row1value2,
row1value3),(row2value1, row2value2, row2value3),...,
(rowNvalue1, rowNvalue2, rowNvalue3))

I haven't found the way to do it just using tuples. How can I do
it?

Nevertheless, I can solve it like this:
a=[]

for row in reader:
~ elem = (row[0],row[1],row[2])
~ a.append(elem)

which will result in a list of tuples: [(row1value1, row1value2,
row1value3),(row2value1, row2value2, row2value3),...,
(rowNvalue1, rowNvalue2, rowNvalue3)]

tuple() will consume a list.
tuple([1,2,3]) (1, 2, 3)
tuple([(1,2),(3,4),(5,6)])
((1, 2), (3, 4), (5, 6))


max
 
L

Larry Bates

The first question is why do you need tuples?

But this works:

import csv
filename=r'C:\test.txt'
fp=open(filename,'r')
reader=csv.reader(fp)
tlines=tuple([tuple(x) for x in reader])
fp.close()

Larry Bates
 
T

Tim Roberts

Luis P. Mendes said:
I'm trying to solve this problem:

suppose I'm reading a csv file and want to create a tuple of all those
rows and values, like ((row1value1, row1value2, row1value3),(row2value1,
row2value2, row2value3),..., (rowNvalue1, rowNvalue2, rowNvalue3))

I haven't found the way to do it just using tuples. How can I do it?

Nevertheless, I can solve it like this:
a=[]

for row in reader:
~ elem = (row[0],row[1],row[2])
~ a.append(elem)

which will result in a list of tuples: [(row1value1, row1value2,
row1value3),(row2value1, row2value2, row2value3),..., (rowNvalue1,
rowNvalue2, rowNvalue3)]

Then, I get what I want with tuple(a).

Why? What is it about the list of tuples that you don't like?
Philosophically, it's more in line with Guido's separation of list and
tuple.
 
L

Luis P. Mendes

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


|
| Why? What is it about the list of tuples that you don't like?
| Philosophically, it's more in line with Guido's separation of list and
| tuple.
I'm not saying that I don't like, I was just curious to know if there
was a way to do it using exclusively tuples.

Regards,

Luis Mendes
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFDIqgSHn4UHCY8rB8RAps/AJ905JXc5naxJYWLA0JLd0ZaJfQQWACeLXHJ
pLE9nmMH+k81ybbB1Otj0hg=
=2/LC
-----END PGP SIGNATURE-----
 
T

Terry Reedy

Luis P. Mendes said:
| Why? What is it about the list of tuples that you don't like?
| Philosophically, it's more in line with Guido's separation of list and
| tuple.
I'm not saying that I don't like, I was just curious to know if there
was a way to do it using exclusively tuples.

A list of lists can be built top down. A tuple of tuples must be built
bottom up, and each tuple must be built with one call to tuple(). But
tuple(it) will take any iterable, so write, say, a generator that yields
lower-level tuples.

Terry J. Reedy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,588
Members
45,100
Latest member
MelodeeFaj
Top