Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Using nested lists and tables

9 views
Skip to first unread message

Zeynel

unread,
Oct 28, 2010, 2:02:15 AM10/28/10
to
I am trying to make this simple app for GAE.

I get a string s that user enters in a form.

I append that to an empty list L = [] then I test if the last saved
string is the same as the new string. If same, I write it on the same
column; if not the cursor moves to next column (I was trying to do
this with tables) and as long as the user types the same string the
cursor stays on the same column. If a new string is typed; a new
column is started; and so on.

I asked the same question at Stackoverflow and HN with no good answers
so far. Maybe you can help. Thanks.

http://news.ycombinator.com/item?id=1840335

http://news.ycombinator.com/item?id=1841536

http://stackoverflow.com/questions/4011728/conditional-statements-with-python-lists

robert

unread,
Oct 28, 2010, 2:32:16 AM10/28/10
to
Zeynel wrote:
> I am trying to make this simple app for GAE.
>
> I get a string s that user enters in a form.
>
> I append that to an empty list L = [] then I test if the last saved
> string is the same as the new string. If same, I write it on the same
> column; if not the cursor moves to next column (I was trying to do
> this with tables) and as long as the user types the same string the
> cursor stays on the same column. If a new string is typed; a new
> column is started; and so on.
>
> I asked the same question at Stackoverflow and HN with no good answers
> so far.

the reason may be that your text doesn't contain a question (mark).
Also in your essay you mix perhaps unnecessary UI complications with python issues and it is not very clear what it is all about.
perhaps drill down to a question on python-level.
lists in python can grow arbitrarily at any time - the memory is the limit. and due to dynamic typing each object in a list can be anything - e.g. another list.


r

Peter Otten

unread,
Oct 28, 2010, 4:49:48 AM10/28/10
to
Zeynel wrote:

>>> columns = []
>>> def add(s):
... if columns and columns[-1][0] == s:
... columns[-1].append(s)
... else:
... columns.append([s])
...
>>> while True:
... s = raw_input()
... if not s: break
... add(s)
...
abc
abc
abc
xy
xy
that's all, folks

>>> columns
[['abc', 'abc', 'abc'], ['xy', 'xy'], ["that's all, folks"]]
>>> for row in range(max(len(c) for c in columns)):
... print " | ".join(c[row] if len(c) > row else " "*len(c[0]) for c in
columns)
...
abc | xy | that's all, folks
abc | xy |
abc | |


Zeynel

unread,
Oct 28, 2010, 9:19:43 AM10/28/10
to
On Oct 28, 4:49 am, Peter Otten <__pete...@web.de> wrote:

Thank you this is great; but I don't know how to modify this code so
that when the user types the string 's' on the form in the app he sees
what he is typing. So, this will be in GAE. But I have a couple of
other questions, for learning purposes. Thanks again, for the help.

> ...     if columns and columns[-1][0] == s:

Here, how do you compare "columns" (a list?) and columns[-1][0] (an
item in a list)?

>>> for row in range(max(len(c) for c in columns)):
> ...     print " | ".join(c[row] if len(c) > row else " "*len(c[0]) for c in
> columns)

What is "c" here?

Zeynel

unread,
Oct 28, 2010, 9:30:16 AM10/28/10
to
On Oct 28, 2:32 am, robert <nos...@nospam.invalid> wrote:
> the reason may be that your text doesn't contain a question (mark).
...

> perhaps drill down to a question on python-level.

Thanks, I realize that what I was trying to ask is not too clear. I am
learning to GAE using Python and I want to deploy a simple app. The
app will have a form. The user enters a sentence to the form and
presses enter. The sentence is displayed. The user types in the same
sentence; the sentence is displayed on the same column; the user types
in a different sentence; the different sentence is displayed on the
next column; as long as the user types in the same sentence; the
sentence is displayed on the same column; otherwise it is displayed on
the next column.

Maybe nested lists are not the right tool for this. Any suggestions?

Iain King

unread,
Oct 28, 2010, 9:35:06 AM10/28/10
to
On Oct 28, 2:19 pm, Zeynel <azeyn...@gmail.com> wrote:
> On Oct 28, 4:49 am, Peter Otten <__pete...@web.de> wrote:
>
> Thank you this is great; but I don't know how to modify this code so
> that when the user types the string 's' on the form in the app he sees
> what he is typing. So, this will be in GAE. But I have a couple of
> other questions, for learning purposes. Thanks again, for the help.
>
> > ...     if columns and columns[-1][0] == s:
>
> Here, how do you compare "columns" (a list?) and columns[-1][0] (an
> item in a list)?
>

It's equivalent to:

if columns:
if columns[-1][0] == s:
dostuff()

i.e. check columns is not empty and then check if the last item
startswith 's'.

(a) I don't know if the order of resolution is predicated left-to-
right in the language spec of if it's an implementation detail
(b) columns[-1].startswith('s') would be better

Iain

Iain King

unread,
Oct 28, 2010, 9:37:23 AM10/28/10
to
On Oct 28, 2:35 pm, Iain King <iaink...@gmail.com> wrote:
...

> (a) I don't know if the order of resolution is predicated left-to-
> right in the language spec of if it's an implementation detail
> (b) columns[-1].startswith('s') would be better
>
...

Ignore (b), I didn't read the original message properly.

Iain


Zeynel

unread,
Oct 28, 2010, 9:42:56 AM10/28/10
to
On Oct 28, 9:35 am, Iain King <iaink...@gmail.com> wrote:
> It's equivalent to:
>
> if columns:
>     if columns[-1][0] == s:
>         dostuff()
>
> i.e. check columns is not empty and then check if the last item
> startswith 's'.

Thanks!

Peter Otten

unread,
Oct 28, 2010, 11:27:56 AM10/28/10
to pytho...@python.org
Zeynel wrote:

> Thank you this is great; but I don't know how to modify this code so
> that when the user types the string 's' on the form in the app he sees
> what he is typing. So, this will be in GAE.

I've no idea what GAE is. In general the more precise your question is the
better the answers tend to be. Don't make your readers guess.

>>>> for row in range(max(len(c) for c in columns)):
>> ... print " | ".join(c[row] if len(c) > row else " "*len(c[0]) for c
>> in columns)
>
> What is "c" here?

Sorry, I was trying to do too much in a single line.

result = [c for c in columns]

is a way to iterate over a list. It is roughly equivalent to

result = []
for c in columns:
result.append(c)

Then, if columns is a list of lists of strings 'c' is a list of strings.
You normally don't just append the values as you iterate over them, you do
some calculation. For example

column_lengths = [len(c) for c in columns]

would produce a list containing the individual lengths of the columns.

A simpler and more readable version of the above snippet from the
interactive python session is

columns = [


['abc', 'abc', 'abc'],
['xy', 'xy'],
["that's all, folks"]]

column_lengths = [len(column) for column in columns]
num_rows = max(column_lengths)
for row_index in range(num_rows):
row_data = []
for column in columns:
# check if there is a row_index-th row in the
# current column
if len(column) > row_index:
value = column[row_index]
else:
# there is no row_index-th row for this
# column; use the appropriate number of
# spaces instead
value = " " * len(column[0])
row_data.append(value)
print " | ".join(row_data)


Rhodri James

unread,
Oct 28, 2010, 5:52:09 PM10/28/10
to

It still really isn't clear what your *question* is here. Perhaps if
you showed us what you have tried already we might be better able to
divine what problem you're facing?

--
Rhodri James *-* Wildebeest Herder to the Masses

0 new messages