Using nested lists and tables

Discussion in 'Python' started by Zeynel, Oct 28, 2010.

  1. Zeynel

    Zeynel Guest

    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
    Zeynel, Oct 28, 2010
    #1
    1. Advertising

  2. Zeynel

    robert Guest

    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
    robert, Oct 28, 2010
    #2
    1. Advertising

  3. Zeynel

    Peter Otten Guest

    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/4011728/conditional-statements-with-

    python-lists

    >>> columns = []
    >>> def add(s):

    .... if columns and columns[-1][0] == s:
    .... columns[-1].append(s)
    .... else:
    .... columns.append()
    ....
    >>> 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 | |
    Peter Otten, Oct 28, 2010
    #3
  4. Zeynel

    Zeynel Guest

    On Oct 28, 4:49 am, Peter Otten <> 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, Oct 28, 2010
    #4
  5. Zeynel

    Zeynel Guest

    On Oct 28, 2:32 am, robert <> 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?
    Zeynel, Oct 28, 2010
    #5
  6. Zeynel

    Iain King Guest

    On Oct 28, 2:19 pm, Zeynel <> wrote:
    > On Oct 28, 4:49 am, Peter Otten <> 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, Oct 28, 2010
    #6
  7. Zeynel

    Iain King Guest

    On Oct 28, 2:35 pm, Iain King <> 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
    Iain King, Oct 28, 2010
    #7
  8. Zeynel

    Zeynel Guest

    On Oct 28, 9:35 am, Iain King <> 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!
    Zeynel, Oct 28, 2010
    #8
  9. Zeynel

    Peter Otten Guest

    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)
    Peter Otten, Oct 28, 2010
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==

    List of lists of lists of lists...

    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==, May 8, 2006, in forum: Python
    Replies:
    5
    Views:
    404
    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==
    May 15, 2006
  2. Chris Brat
    Replies:
    5
    Views:
    695
    =?iso-8859-1?q?Luis_M._Gonz=E1lez?=
    Aug 22, 2006
  3. Daniel Nogradi
    Replies:
    3
    Views:
    349
    Dennis Lee Bieber
    Nov 10, 2006
  4. Gabriel Zachmann

    Bug with lists of pairs of lists and append()

    Gabriel Zachmann, Sep 28, 2007, in forum: Python
    Replies:
    2
    Views:
    233
    Gabriel Zachmann
    Oct 1, 2007
  5. Gabriel Zachmann

    Bug with lists of pairs of lists and append()

    Gabriel Zachmann, Sep 28, 2007, in forum: Python
    Replies:
    5
    Views:
    265
    Gabriel Zachmann
    Oct 1, 2007
Loading...

Share This Page