3 ms·
I've been trying to get an answer to my Python question: http://news.ycombinator.com/item?id=1841536 http://news.ycombinator.com/item?id=1841536 Maybe you can
by zeynel1 16y ago
I've been trying to get an answer to my Python question: http://news.ycombinator.com/item?id=1841536 http://news.ycombinator.com/item?id=1841536
Maybe you can take a look at that?
- anthonyb 16y agoLooks to me like you're trying to shoehorn everything into the one list, which is going to make your code hard to write. Something to note is that you're adding blank strings ('') to your list to pad it out, which is a bit of a warning sign that your data structure is wrong. Rather than do that, I'd use a dictionary keyed off either an id or the original text, store the user input as a list after that, then sort out how to display this in the display part of your code. Something like: typing = { 'Hello World': ['Hello World', 'Hello World', 'Hello World'] 'Hello Universe': ['Hello Universe', 'Hello Universe', 'Hello Universe', ...] } Bear in mind that dictionaries are unordered though, so you might need to either use a list to order them, or use sorteddict from the collections library.
- zeynel1 16y agoThanks. I am sorry that my question is not clear. In this case, the user enters a string s in a form and 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. In your example, if I understand correctly; it is assumed that there is a list of 3 "Hello World"s. In reality, each should be entered and then displayed (I use Mako templates). This looks like a simple problem but I was unable to make it work. Thanks again.
- anthonyb 16y agoI was just showing you what the data structure to store your text might look like. In practice there are a number of ways to do it, but you should be guided by what'll make it easiest to get your data out. In other words, you might be able to make it work with a list of lists, but you're trying to cut 'across' the list, rather than with it. So with your existing list: list = [ ['Hello World', 'Hello World'], ['Hello Universe', ...], ... ] you'll need to display the first item of each on one row, then the second, and so on, which is a hassle. Another way might be to pregenerate your list of lists with blank items (assuming that you know how many different strings you have and how many tries they get) then track the current row and column number. If they enter it successfully then your current row increments by one. If they choose a different one, then increment your column and reset But you seem to be overly focused on the details of your implementation (ie. rows and columns), rather than the simplest way to achieve what you want. A third way would be to have each series on a separate page, so you only need to track the current string. Once they're ready, they can click a button to get to the next one/enter a new string. A fourth way - don't use a table, use <divs> with a set width and display-inline. Each <div> has the previous entries separated by line breaks, with a form for the final one. With this sort of structure you can process each list in turn, without having to worry about indexing.