By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,818 Members | 1,223 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,818 IT Pros & Developers. It's quick & easy.

nested tuples

P: n/a
-----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

iD8DBQFDIdcvHn4UHCY8rB8RAjBnAJ9hLzbMZVZf3vLZ0iCs2p tK0v6RPwCfRr1S
GHdoy/APTZSWy+rLBA+i0KE=
=vyR6
-----END PGP SIGNATURE-----
Sep 9 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"Luis P. Mendes" <lu***********@netvisaoXXX.pt> wrote in
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
Sep 9 '05 #2

P: n/a
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

Luis P. Mendes wrote:
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

Sep 9 '05 #3

P: n/a
"Luis P. Mendes" <lu***********@netvisaoXXX.pt> wrote:

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.
--
- Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Sep 10 '05 #4

P: n/a
-----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-----
Sep 10 '05 #5

P: n/a

"Luis P. Mendes" <lu***********@netvisaoXXX.pt> wrote in message
news:3o************@individual.net...
| 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

Sep 10 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.