Using nested lists and tables

Peter Otten __peter__ at web.de
Thu Oct 28 04:49:48 EDT 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/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])  
...                                  
>>> 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 |    |





More information about the Python-list mailing list