Note that there are some explanatory texts on larger screens.

plurals
  1. POMulti-line dictionaries: Replace the key as per a word in value
    text
    copied!<p>I have a dictionary in which I have to replace all the keys depending on a word in the value set. So my dictionary is: <br></p> <pre><code> { 23: {'score': -8.639, 'char': False, 'word': 'positive'} } { 56: {'score': -5.6, 'char': False, 'word': 'neutral'} } { 89: {'score': -8.9, 'char': False, 'word': 'positive'} } { 34: {'score': -2.3, 'char': Tru, 'word': 'negative'} } </code></pre> <p>If the values part of dictionary i.e. the key word is positive then it should replace the key 23 with +1, for neutral's, the key 56 with 0 and for negative, the key 34 with -1. The output will look like:<br></p> <pre><code> { +1: {'score': -8.639, 'char': False, 'word': 'positive'} } { 0: {'score': -5.6, 'char': False, 'word': 'neutral'} } { +1: {'score': -8.9, 'char': False, 'word': 'positive'} } { -1: {'score': -2.3, 'char': Tru, 'word': 'negative'} } </code></pre> <p>Here is my code:</p> <pre><code>for n, line in enumerate(sys.stdin,1): d = ast.literal_eval(line) items = d.values()[0].items() if re.match("positive",d.get('sentimentoftweet')): n = str.replace(str(n),"+1") else: n = str.replace(str(n),"0") </code></pre> <p>Its not working and giving me this error:</p> <pre><code>Traceback (most recent call last): File "./linear.py", line 33, in &lt;module&gt; for thing in d: File "./linear.py", line 22, in gen_with_appropriate_name if re.match("positive",d.get('sentimentoftweet')): File "/usr/lib/python2.7/re.py", line 137, in match return _compile(pattern, flags).match(string) TypeError: expected string or buffer </code></pre>
 

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