Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #54512 > unrolled thread
| Started by | Shyam Parimal Katti <spk265@nyu.edu> |
|---|---|
| First post | 2013-09-20 17:32 -0400 |
| Last post | 2013-09-20 17:32 -0400 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.python
Iterate through a list of tuples for processing Shyam Parimal Katti <spk265@nyu.edu> - 2013-09-20 17:32 -0400
| From | Shyam Parimal Katti <spk265@nyu.edu> |
|---|---|
| Date | 2013-09-20 17:32 -0400 |
| Subject | Iterate through a list of tuples for processing |
| Message-ID | <mailman.201.1379714085.18130.python-list@python.org> |
[Multipart message — attachments visible in raw view] — view raw
I have a list of tuples where the number of rows in the list and the number
of columns in tuples of the list will not be constant. i.e.
list = [(a1,b1, …z1), (a2,b2, …, z2),…. ,(am,bm, … , zm )]. It can be
compared to the SQL results, as the number of columns change in the sql,
the number of columns change in the list of tuples as well.
I have to iterate through each element of each tuple in the list, perform
some checks on the value, convert it and return the modified values as a
new list of tuples.
i.e.
list_value = [(‘name1’, 1234, ‘address1’ ), (‘name2’, 5678, ‘address2’),
(‘name3’, 1011, ‘addre”ss3’)]
I need to access each value to check if the value contains a double quote
and enclose the string containing double quote with double quotes. The
transformation should return
list_value = [(‘name1’, 1234, ‘address1’ ), (‘name2’, 5678, ‘address2’),
(‘name3’, 1011, ‘”addre”ss3”’)]
The simplest approach for me would be to do this:
mod_val = [transform(row) for row in list_value]
def transform(row):
mod_list=[]
while index < len(row):
... if isinstance(row[index],basestring):
... if '"' in row[index]:
... mod_list.append('"'+row[index]+'"')
... else:
... mod_list.append(row[index])
... index = index+1
... return mod_list
Is there a way to make the code concise using list comprehension?
Back to top | Article view | comp.lang.python
csiph-web