我有一个字典,我必须根据值集中的单词替换所有键。所以我的字典是:
{ 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'} }
如果词典的值部分,即关键词是肯定的,那么它应该用+1代替键23,对于中性,键56用0代替,对于负代表,键34用-1代替。
输出结果如下:
{ +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'} }
这是我的代码:
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")
它不能正常工作并给我这个错误:
Traceback (most recent call last):
File "./linear.py", line 33, in <module>
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
答案 0 :(得分:2)
您将错误的密钥传递给re.match
。如果缺少键dict.get
,则返回None
,请使用d.get('word')
。
>>> import re
>>> re.match('foo', None)
Traceback (most recent call last):
File "<ipython-input-43-c75223170494>", line 1, in <module>
re.match('foo', None)
File "/usr/lib/python3.3/re.py", line 156, in match
return _compile(pattern, flags).match(string)
TypeError: expected string or buffer
您可以使用==
匹配字符串或值:
if d.get('word') == 'positive':
#do something
elif d.get('word') == 'negative'
#do something else
<强>代码:强>
import sys, ast
for line in sys.stdin:
d = ast.literal_eval(line)
key = list(d)[0] #dictionary with just one key.
if d[key]['word'] == 'positive':
print {'+1': d[key]}
elif d[key]['word'] == 'negative':
print {'-1': d[key]}
elif d[key]['word'] == 'neutral':
print {'0': d[key]}
<强>输出:强>
{'+1': {'char': False, 'score': -8.639, 'word': 'positive'}}
{'0': {'char': False, 'score': -5.6, 'word': 'neutral'}}
{'+1': {'char': False, 'score': -8.9, 'word': 'positive'}}
{'-1': {'char': True, 'score': -2.3, 'word': 'negative'}}
答案 1 :(得分:0)
问题是d.get('word')
没有查看嵌套字典。
答案 2 :(得分:0)
也许是这样的:
#!/usr/local/cpython-3.3/bin/python
import pprint
import collections
dict_ = { 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': True, 'word': 'negative'},
}
new_dict = collections.defaultdict(list)
for key, value in dict_.items():
if value['word'] == 'positive':
key = '+1'
elif value['word'] == 'neutral':
key = '0'
elif value['word'] == 'negative':
key = '-1'
else:
raise ValueError('word not positive, neutral or negative')
new_dict[key].append(value)
pprint.pprint(new_dict)