Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I don't really get where you're heading with those two lists structure. It's unclear, and I don't think you can get a proper algorithm out of that. </p> <p>You say: "For a given text if any of the words in position_list occurs, it must be replaced with specific words in position", that means that '<em>front</em>' has to be replaced by '<em>down</em>', '<em>frnt</em>' by '<em>right</em>' and '<em>rhs</em>' has no replacement. That makes no sense!</p> <p>So I'm guessing, from the rest of your question, that you want the words that follows '<em>front</em>' to be replaced by '<em>front</em>', and the words that follows '<em>back</em>' to be replaced by '<em>back</em>'. But then, there's no information to help an algorithm know what words are the replacements, and what words are to be replaced.</p> <p>So the only solution would be to change your structures in a more pythonic way to make an easy and elegant algorithm out of it. Then, you may want to try with a structure like this:</p> <pre><code>position = ['front', 'back'] position_list = [('frnt', 'ft'), ('bck')] </code></pre> <p>and then the algorithm would look like:</p> <pre><code>replaces = zip(position, position_list) for new_word, old_words in replaces: for old_word in old_words: str = str.replace(old_word, new_word) </code></pre> <p>You can also use a dictionary:</p> <pre><code>positions = {'front': ['frnt', 'ft'], 'back': ['bck']} for new_word, old_words in positions.iteritems(): for old_word in old_words: str = str.replace(old_word, new_word) </code></pre> <p>In other words, try avoiding creating structures that would lead in the end making an algorithm dealing with indexes of the list...</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. This table or related slice is empty.
    1. 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