Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>So here is an example of how I use list comprehensions to do this sort of thing. If you don't know, the list comprehension is <code>something = [explorer(x) for x in something]</code>. That's also where the recursion is happening. What you get back is a list of the exact same structure, but you've "been to" every endpoint and can check for and replace things. I did a couple arbitrary replacements.</p> <pre><code>&gt;&gt;&gt; tree = ['S', ['NP', ['DET', 'There']], ['S', ['VP', ['VERB', 'is'], ['VP', ['NP', ['DET', 'no'], ['NOUN', 'asbestos']], ['VP', ['PP', ['ADP', 'in'], ['NP', ['PRON', 'our'], ['NOUN', 'products']]], ['ADVP', ['ADV', 'now']]]]], ['.', '.']]] &gt;&gt;&gt; def explorer(something): if type(something) == list: something = [explorer(x) for x in something] else: # You may want to check other conditions here, like if it's a string if something == 'asbestos': something = 'Oh my' if something == 'S': something = 'Z' return something &gt;&gt;&gt; explorer(tree) ['Z', ['NP', ['DET', 'There']], ['Z', ['VP', ['VERB', 'is'], ['VP', ['NP', ['DET', 'no'], ['NOUN', 'Oh my']], ['VP', ['PP', ['ADP', 'in'], ['NP', ['PRON', 'our'], ['NOUN', 'products']]], ['ADVP', ['ADV', 'now']]]]], ['.', '.']]] &gt;&gt;&gt; </code></pre> <p>I just noticed something after reading your words more carefully. The reason you are not able to "propagate replacements upwards" is because your loop is structured something like this:</p> <pre><code>for x in aList: if x = somethingSpecial: x = somethingElse </code></pre> <p>That does not work in Python, but this does:</p> <pre><code>for i,x in enumerate(aList): if x = somethingSpecial: aList[i] = somethingElse </code></pre> <p>Now <code>aList</code> has ben modified the way you want. If you don't know what <code>enumerate()</code> does, just copy/paste this:</p> <pre><code>aList = ['a','b','c'] for i,x in enumerate(aList): print(i,x) </code></pre>
    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.
    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