Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #61761 > unrolled thread
| Started by | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| First post | 2013-12-12 21:55 +0000 |
| Last post | 2013-12-12 21:55 +0000 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: Code suggestion - List comprehension Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-12-12 21:55 +0000
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2013-12-12 21:55 +0000 |
| Subject | Re: Code suggestion - List comprehension |
| Message-ID | <mailman.4029.1386885377.18130.python-list@python.org> |
On 12/12/2013 20:40, Shyam Parimal Katti wrote:
> 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.
>
I don't think this can be done with a list comprehension. As you appear
to have a perfectly good piece of code, if it ain't broke, don't fix it
:) Maybe change one line.
if query_holder[-1] == ';':
--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.
Mark Lawrence
Back to top | Article view | comp.lang.python
csiph-web