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


Groups > comp.lang.python > #61752

Code suggestion - List comprehension

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.034
X-Spam-Evidence '*H*': 0.93; '*S*': 0.00; 'received:209.85.223': 0.03; 'explicit': 0.07; 'string': 0.09; 'indicates': 0.09; 'subject:skip:c 10': 0.09; 'eliminating': 0.16; 'elements': 0.16; 'trying': 0.19; 'split': 0.19; 'subject:Code': 0.24; 'skip:p 30': 0.29; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; 'skip:q 20': 0.31; 'table': 0.34; 'received:209.85': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'version': 0.36; 'shorter': 0.36; 'subject:List': 0.36; 'done': 0.36; 'list': 0.37; 'received:209': 0.37; 'expected': 0.38; '8bit%:4': 0.38; 'skip:[ 10': 0.38; 'to:addr:python-list': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'conversion': 0.61; 'new': 0.61; 'such': 0.63; 'temporary': 0.65; 'here': 0.66; 'sample': 0.67
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=rQ197Pb/RXOEOAeZ6qPIpwWCzVyIDMNbqjDUTU6mU84=; b=ickBldWISYWPxTPrLbxuQYkSCnsCvT3doVKIU0HuYWCzF4+LPBM7U+Zgw+AtJalr36 ulYKCgFrpS7arA8WBJbO48oQvLac7DcTm6w28BTpQgRw1os3bup9d0T8uW8FWy2g0yQZ 67oq9XtBPZTAA1KSh+JWUqcre++/y1nbnXeQ+GCiKotFZbBEYgVA/kKQVaXIQJxmeLgA +EQNBOZDLg168InF87u3mJnQYfSswcyv1wB4tSmP2Qv/jn2ic5gv4TI4rbRKvLJfARov haSPWqV9cS+oYfE20UBLXcY0OGQ0MIxpY10XyeYAu8tNp+kggjJRP/di/HyiYoNcRxov JaRA==
X-Gm-Message-State ALoCoQl33y0dKSOP7CR+rfbtGfHC2E1N+q+mxoqfO81O7iVAku0ABbNbUTpjMo8fpzMoramrMGNc
MIME-Version 1.0
X-Received by 10.42.208.211 with SMTP id gd19mr8420500icb.15.1386880805549; Thu, 12 Dec 2013 12:40:05 -0800 (PST)
Date Thu, 12 Dec 2013 15:40:05 -0500
Subject Code suggestion - List comprehension
From Shyam Parimal Katti <spk265@nyu.edu>
To python-list@python.org
Content-Type multipart/alternative; boundary=20cf303ea90ac2c6fa04ed5c595a
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.4021.1386882662.18130.python-list@python.org> (permalink)
Lines 67
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1386882662 news.xs4all.nl 2959 [2001:888:2000:d::a6]:48779
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:61752

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

Hello,

I have a list of sql queries, some which are split across multiple list
elements e.x.
['drop table sample_table;', 'create table sample_test', '(col1 int);',
'select col1 from', ' sample_test;']

A semi-colon in the string value  indicates the termination of a sql query.
So the expected out come is a conversion to a list of valid sql queries:
['drop table sample_table;', 'create table sample_test (col1 int);',
'select col1 from sample_test;']

Here is the code that does that:

sample = ['drop table sample_table;', 'create table sample_test', '(col1
int);', 'select col1 from', ' sample_test;']
pure_sqls = []
query_holder= ''
for each_line in sample:
    query_holder += each_line
    if query_holder.endswith(';'):
        pure_sqls.append(query_holder)
        query_holder = ''


Is there a way to do this by eliminating explicit creation of new
list(pure_sqls) and a temporary variable(query_holder)? Using list
comprehension? Though I don't want to put the shorter version in
production(if it is difficult to understand), I am looking if this can be
done with list comprehension since I am trying to learn list comprehension
by using it in such scenarios.

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Code suggestion - List comprehension Shyam Parimal Katti <spk265@nyu.edu> - 2013-12-12 15:40 -0500

csiph-web