473,756 Members | 9,694 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

nested tuples

-----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),(ro w2value1,
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),(ro w2value1, 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

iD8DBQFDIdcvHn4 UHCY8rB8RAjBnAJ 9hLzbMZVZf3vLZ0 iCs2ptK0v6RPwCf Rr1S
GHdoy/APTZSWy+rLBA+i0 KE=
=vyR6
-----END PGP SIGNATURE-----
Sep 9 '05 #1
5 1580
"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),(ro w2value1, 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),(ro w2value1, 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
The first question is why do you need tuples?

But this works:

import csv
filename=r'C:\t est.txt'
fp=open(filenam e,'r')
reader=csv.read er(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),(ro w2value1,
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),(ro w2value1, row2value2, row2value3),... , (rowNvalue1,
rowNvalue2, rowNvalue3)]

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

Luis P. Mendes

Sep 9 '05 #3
"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),(ro w2value1,
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),(r ow2value1, 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
-----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

iD8DBQFDIqgSHn4 UHCY8rB8RAps/AJ905JXc5naxJYW LA0JLd0ZaJfQQWA CeLXHJ
pLE9nmMH+k81ybb B1Otj0hg=
=2/LC
-----END PGP SIGNATURE-----
Sep 10 '05 #5

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
8447
by: ruari mactaggart | last post by:
can i write >>>dictionary to mean the 3rd item in the list that is the corresponding value for 'key' ?
0
1939
by: Matthew Barnes | last post by:
I was wondering if there would be any interest in extending the struct.unpack format notation to be able to express groups of data with parenthesis. For example: >>> data = struct.pack('iiii', 1, 2, 3, 4) >>> struct.unpack('i(ii)i', data) # Note the parentheses (1, (2, 3), 4)
13
5448
by: omission9 | last post by:
I have a dictionary that looks like this MY_DICT=FOO I am having a problem updating this with a simple MY_DICT.update(NEW_DICT) as update doesn't seem to care about getting into the inner dicts. Getting the keys of each and iterating through and updating each one is terribly slow as the number of keys gets bigger and bigger. What is the bst way to update my nested dicts?
8
1940
by: Wolfgang Buechel | last post by:
Hi, I want to iterate over all 2x2 matrices with elements in range 0..25 (crypto-stuff). To produce them, first I wrote a fourfold nested for loop: M=26 for a in range(M): for b in range (M):
5
1823
by: Steven Bethard | last post by:
So, I have a list of lists, where the items in each sublist are of basically the same form. It looks something like: py> data = , .... .... , .... .... ] Now, I'd like to sample down the number of items in each sublist in the
3
2055
by: Jake Emerson | last post by:
I'm attempting to build a process that helps me to evaluate the performance of weather stations. The script below operates on an MS Access database, brings back some data, and then loops through to pull out statistics. One such stat is the frequency of reports from the stations ('char_freq'). I have a collection of methods that operate on the data to return the 'char_freq' and this works great. However, when the process goes to insert the...
3
1919
by: Georg Sauthoff | last post by:
Hi, t = (1, (2, 3)) I am bit suprised, that I cannot access '3' via: t. # syntax error But t.__getitem__(1) works like expected. Why is that?
12
1459
by: Rich Shepard | last post by:
I want to code what would be nested "for" loops in C, but I don't know the most elegant way of doing the same thing in python. So I need to learn how from you folks. Here's what I need to do: build a database table of 180 rows. Each row contains 31 columns: the first is an automatically incremented integer as the primary key; the next two fields can each contain one of three strings held in dictionaries, the last 28 fields are random...
13
2960
by: jubelbrus | last post by:
Hi I'm trying to do the following. #include <vector> #include <boost/thread/mutex.hpp> #include <boost/shared_ptr.hpp> #include <boost/tuple/tuple.hpp> class {
0
9303
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9117
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9676
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8542
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7078
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6390
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4955
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5156
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3651
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.