Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #54512

Iterate through a list of tuples for processing

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <spk265@nyu.edu>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.003
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'else:': 0.03; 'received:209.85.223': 0.03; 'value,': 0.04; 'element': 0.07; 'modified': 0.07; 'string': 0.09; 'iterate': 0.09; 'rows': 0.09; 'sql,': 0.09; 'def': 0.12; '\'"\'': 0.16; 'columns': 0.16; 'simplest': 0.16; 'tuple': 0.16; 'index': 0.16; '&lt;': 0.19; 'this:': 0.26; 'values': 0.27; 'compared': 0.30; 'subject:list': 0.30; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; 'concise': 0.31; 'tuples': 0.31; 'skip:m 30': 0.32; 'received:209.85': 0.35; 'convert': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'i.e.': 0.36; 'should': 0.36; 'list': 0.37; 'received:209': 0.37; 'checks': 0.38; 'skip:m 40': 0.38; 'skip:[ 10': 0.38; 'to:addr :python-list': 0.38; 'list,': 0.38; 'quote': 0.39; 'skip:. 10': 0.39; 'to:addr:python.org': 0.39; 'new': 0.61; 'charset:windows-1252': 0.65; 'containing': 0.69; 'results,': 0.84; 'skip:\x91 10': 0.84; '8bit%:33': 0.91
X-Google-DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:date:message-id:subject:from:to :content-type; bh=P4TMl4/S25qE/+twl6T9PChZgJlZI8ptbtkJjmTddQY=; b=byjdL9/t3HGRHGuDY2IzsQqJpPbaJJc2wEdDvXU4q91AxDnZj/g8UqoWueGq7kv9FB uSY/JGJVaOAavndZC77ZbwtXBnx4RJkDPp9GhOEBAvNTjmEtEfdpl3khLQr5eryWBlGz ff6Pw+8V8/sXBJToXYS+FCVBIAyGSwVrIgI0sr6yd9DzQZk5t5WsZxXwTW9H/SybmI3e yM9Z8ZfChydN0cM6Cb8QK+LWhA2BVC+z4fKOzhHXwNyzJRm0V5ESHtgmmfBzMThcH3zV mhPTGdNV3gaB4OBhr2GoEQGs7Y2aWe7yCzeiuZVhp4cfrYr2+w+xA98EC1tCcGKV1UAH 79Jw==
X-Gm-Message-State ALoCoQkwzmzgBFQ5wNQkXBd/wcYm5o2I0DG3TZGqSB0fQGyKkFbZhJoLQwsmBj4D3Kb2iiq1da3E
MIME-Version 1.0
X-Received by 10.43.10.198 with SMTP id pb6mr441847icb.40.1379712732522; Fri, 20 Sep 2013 14:32:12 -0700 (PDT)
Date Fri, 20 Sep 2013 17:32:12 -0400
Subject Iterate through a list of tuples for processing
From Shyam Parimal Katti <spk265@nyu.edu>
To python-list@python.org
Content-Type multipart/alternative; boundary=bcaec50e5ce7507c1604e6d76707
X-Mailman-Approved-At Fri, 20 Sep 2013 23:54:45 +0200
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.201.1379714085.18130.python-list@python.org> (permalink)
Lines 197
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1379714085 news.xs4all.nl 15905 [2001:888:2000:d::a6]:37665
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:54512

Show key headers only | View raw


[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 comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Iterate through a list of tuples for processing Shyam Parimal Katti <spk265@nyu.edu> - 2013-09-20 17:32 -0400

csiph-web