Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If the order of the strings is important, a dictionary will be a problem. You are on the right path using lists, but I think you need more than just that. Honestly, I'm not clear on what your output will look like for different data sets. If I read your question correctly, is the following true?</p> <p>Input:</p> <pre><code>Sentence 1 Sentence 3 Sentence 1 Sentence 1 Sentence 2 Sentence 2 </code></pre> <p>Output after last line:</p> <pre><code>Sentence 1 | Sentence 3 | Sentence 2 Sentence 1 | | Sentence 2 Sentence 1 | | </code></pre> <p>If not, please rephrase your expected output with an example.</p> <hr> <p>Ok, so it looks like my take on your problem was correct. That's a good start. ;)</p> <p>I think we need to look at this from a different perspective. Currently, you are looking at this as a big list of all the data and maybe that's too much. What I see here is a list of tuples, where each tuple represents a column (string, count). The tuple will be what string should be in this column and how many of them should be there. So your example would end up looking like:</p> <p><code>L = [("Hello World", 3), ("Hello Universe", 5)]</code></p> <p>I know this isn't obvious as to how to deal with this data, but I think it's the right way to represent it internally. I'll give you some sample code to do some basic manipulations of this data type (others may have more efficient ways of doing the same thing).</p> <p>Add a new String:</p> <pre><code>def AddString(str): try: idx = map(lambda (s, v): ('N', 'Y')[s == str], L).index('Y') except: L.append( (str, 1) ) else: L[idx] = (str, L[idx][1] + 1) </code></pre> <p>Print the inside of an HTML table:</p> <pre><code>def PrintChart(): try: max = reduce(lambda x, y: [x, y][x[1] &lt; y[1]], L)[1] except: print "&lt;td&gt;&lt;/td&gt;" else: for i in range(max): print "&lt;tr&gt;" for (s, c) in L: if i+1 &lt;= c: print " &lt;td" + s + "&lt;/td&gt;" else: print " &lt;td&gt;&lt;/td&gt;" print "&lt;/tr&gt;" </code></pre> <p>So anyway, that is the way I'd do it.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload