Hello,
I am trying to learn Python lists. In this code I am trying to add a string s
coming from a form to table row as long as the string is the same. When the string is different; the new string is written to the next column.
I could write the string from column 0
to column 1
but I had problems adding the same string on column 1
correctly.
The way it is; the script works only up to placing the different string to the next column.
I realize that this is not the right way of doing this.
I would appreciate any help. Thank you. I include the template too.
EDIT2
@JerseyMike: Thanks for your answer. I don't yet understand how AddString(str)
works but trying it in IDLE I noticed that instead of adding the new string it replaces it with the new string. As I said, I have not yet studied how it works; but here is the result (I changed str
to str1
):
>>> def AddString(str1):
try:
idx = map(lambda (s, v): ('N', 'Y')[s == str1], L).index('Y')
except:
L.append((str1, 1))
else: L[idx] = (str1, L[idx][1] + 1)
>>> L = []
>>> str1 = 'hello'
>>> AddString(str1)
>>> L
[('hello', 1)]
>>> AddString(str1)
>>> L
[('hello', 2)]
>>>
EDIT
@JerseyMike:
Thanks, I am sorry, I realized the question was not clear. In this app; the user types in the same sentence; like practicing foreign language. So the input will be
Hello world
Hello world
Hello world
and if the user types in next "Hello universe" that will go to the next column:
Hello world Hello Universe
Hello world
Hello world
and if the user keeps typing "Hello Universe" they should go under the same column
Hello world Hello Universe
Hello world Hello Universe
Hello world Hello Universe
Hello Universe
Hello Universe
The list L containing this looks like this:
L = [
['Hello world', 'Hello Universe'],
['Hello world', 'Hello Universe'],
['Hello world', 'Hello Universe'],
['', 'Hello Universe'],
['', 'Hello Universe']
]
Initially the list is empty and I add the string s
with L.append(s)
.
L = [
['Hello world'],
['Hello world'],
['Hello world'],
]
If the last string s
does not match the new input I create the new column with L[0].insert(1,s)
.
L = [
['Hello world', 'Hello Universe'],
['Hello world'],
['Hello world'],
]
Now I need to write under 'Hello Universe'
This turned out to be difficult for me to figure out for several reasons. But now I think it may be better to append the new string s
to the list before checking if it is same as the previous string. To simplify the list, assume L
is like this:
L = [['A'], ['A'], ['A'], ['B']]
Now ['B']
needs to be inserted into L[0]
. To do this I search the list to the left to find the last sub-list with 1 element (or something like this). I have not looked into how to search lists yet. Thanks again for the help.
END EDİT
class Test(webapp.RequestHandler):
myList = []
def get(self):
# del self.myList[:]
s = [self.request.get('sentence')]
r = len(self.myList)
if r == 0:
self.myList.append(s)
htmlcode1 = HTML.table(self.myList)
lastItem = s
else:
if len(self.myList[0]) == 1:
lastItem = self.myList[r-1]
if s == lastItem:
self.myList.append(s)
htmlcode1 = HTML.table(self.myList)
else:
s = self.request.get('sentence')
self.myList[0].insert(1,s)
htmlcode1 = HTML.table(self.myList)
if len(self.myList[0]) == 2:
self.myList[1].insert(1,s)
htmlcode1 = HTML.table(self.myList)
elif len(self.myList[1]) == 2:
self.myList[2].insert(1,s)
htmlcode1 = HTML.table(self.myList)
template_values = {'htmlcode1': htmlcode1,
's': s,
'r': r,
'myList': self.myList,
# 'myListLen': myListLen,
'lastItem': lastItem,
}
path = os.path.join(os.path.dirname(__file__), 'test.mako')
templ = Template(filename=path)
self.response.out.write(templ.render(**template_values))
Template
<html>
<head>
</head>
<body>
<p>| <a href="/delete">CLEAR</a> |</p>
<form action="/test" method="get">
<input name="sentence" type="text" size="30"><br />
<input type="submit" value="Enter">
</form>
<p>${htmlcode1}</p>
<p>s: ${s}</p>
<p>r: ${r}</p>
<p>myList: ${myList}</p>
<p>lastItem: ${lastItem}</p>
</html>
</body>