Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>That way works fine, but I'd do it slightly differently, using <code>list</code> instead of <code>set</code> so we can order the words correctly (<code>set</code> will output words in alphabetical order, not quite what we want):</p> <p><strong>File: q_15125575.py</strong></p> <pre><code>#-*- encoding: utf8 -*- from collections import defaultdict INPUT = """you have requested a debate on this subject in the course of the next few days , during this part-session . {##} sus señorías han solicitado un debate sobre el tema para los próximos días , en el curso de este período de sesiones . {##} 0-0 0-1 1-2 2-3 3-4 4-5 5-6 6-7 7-8 8-9 12-10 13-11 14-11 15-12 16-13 17-14 9-15 10-16 11-17 18-18 17-19 19-21 20-22""" if __name__ == "__main__": english, spanish, trans = INPUT.split(" {##} ") eng_words = english.split(' ') spa_words = spanish.split(' ') transtable = defaultdict(list) for e, s in [i.split('-') for i in trans.split(' ')]: transtable[eng_words[int(e)]].append(spa_words[int(s)]) print(transtable['course']) print(transtable['you']) print(" ".join(transtable['course'])) print(" ".join(transtable['you'])) </code></pre> <p><strong>Output:</strong><br> ['curso']<br> ['sus', 'se\xc3\xb1or\xc3\xadas']<br> curso<br> sus señorías</p> <p>It's slightly longer code as I'm using the actual words instead of the indexes - but this allows you to directly lookup from <code>transtable</code></p> <p><strong>However</strong>, both your method and my method both fail on the same issue: Repeating words.<br> <code>print(" ".join(transtable['this'])</code><br> gives:<br> <code>el este</code><br> It's at least in the order that the words appear though, so it's workable. Want the first occurrence of <code>'this'</code> translated?<br> <code>transtable['this'][0]</code> would give you the first word.</p> <p>And using your code: </p> <pre><code>tt = defaultdict(set) for e, s in [i.split('-') for i in trans.split(' ')]: tt[int(e)].add(int(s)) query = 'this' for i in tt[eng_words.index(query)]: print i </code></pre> <p>Gives:<br> <code>7</code></p> <p>Your code will only print the index of the <em>first</em> occurrence of a word.</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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