Using nested lists and tables

Z

Zeynel

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
 
R

robert

Zeynel said:
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
 
P

Peter Otten

Zeynel said:
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
columns = []
def add(s):
.... if columns and columns[-1][0] == s:
.... columns[-1].append(s)
.... else:
.... columns.append()
.... .... 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 | |
 
Z

Zeynel

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)?
...     print " | ".join(c[row] if len(c) > row else " "*len(c[0]) for c in
columns)

What is "c" here?
 
Z

Zeynel

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?
 
I

Iain King

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
 
I

Iain King

(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
 
Z

Zeynel

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!
 
P

Peter Otten

Zeynel said:
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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top