Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Using nested lists and tables

Reply
Thread Tools

Using nested lists and tables

 
 
Zeynel
Guest
Posts: n/a
 
      10-28-2010
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/4...h-python-lists

 
Reply With Quote
 
 
 
 
robert
Guest
Posts: n/a
 
      10-28-2010
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
 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      10-28-2010
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. Maybe you can help. Thanks.
>
> http://news.ycombinator.com/item?id=1840335
>
> http://news.ycombinator.com/item?id=1841536
>
> http://stackoverflow.com/questions/4...atements-with-

python-lists

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


 
Reply With Quote
 
Zeynel
Guest
Posts: n/a
 
      10-28-2010
On Oct 28, 4:49*am, Peter Otten <(E-Mail Removed)> 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?

 
Reply With Quote
 
Zeynel
Guest
Posts: n/a
 
      10-28-2010
On Oct 28, 2:32*am, robert <(E-Mail Removed)> 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?
 
Reply With Quote
 
Iain King
Guest
Posts: n/a
 
      10-28-2010
On Oct 28, 2:19*pm, Zeynel <(E-Mail Removed)> wrote:
> On Oct 28, 4:49*am, Peter Otten <(E-Mail Removed)> 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
 
Reply With Quote
 
Iain King
Guest
Posts: n/a
 
      10-28-2010
On Oct 28, 2:35*pm, Iain King <(E-Mail Removed)> 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


 
Reply With Quote
 
Zeynel
Guest
Posts: n/a
 
      10-28-2010
On Oct 28, 9:35*am, Iain King <(E-Mail Removed)> 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!

 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      10-28-2010
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)




 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Lists of lists and tuples, and finding things within them Daniel Nogradi Python 3 11-10-2006 07:57 AM
How to get database metadata information (i.e. existing tables and columns in tables) Chris Brat Python 5 08-22-2006 05:25 PM
List of lists of lists of lists... =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==?= Python 5 05-15-2006 11:47 AM
Tables within tables Otuatail HTML 7 08-02-2004 04:42 AM
Text after 2 tables actually appears between 2 tables ! Peter Bassett HTML 3 08-15-2003 06:46 PM



Advertisments