我有一个位置字典,然后是属性 - 值对,如下所示:
{"Russia":
{"/location/statistical_region/size_of_armed_forces": 65700.0,
"/location/statistical_region/gni_per_capita_in_ppp_dollars": 42530.0,
"/location/statistical_region/gdp_nominal": 1736050505050.0,
"/location/statistical_region/foreign_direct_investment_net_inflows": 8683048195.0,
"/location/statistical_region/life_expectancy": 80.929, ...
依旧,对于每个国家。
然后是一个包含单个数组的字典,数组中的每个值都是一个包含3个键的字典:
{
"sentences": [
{
"location-value-pair": {
"Russia": 6.1
},
"parsedSentence": "On Tuesday , the Federal State Statistics Service -LRB- Rosstat -RRB- reported that consumer price inflation in LOCATION_SLOT hit a historic post-Soviet period low of NUMBER_SLOT percent in 2011 , citing final data .",
"sentence": "On Tuesday , the Federal State Statistics Service -LRB- Rosstat -RRB- reported that consumer price inflation in Russia hit a historic post-Soviet period low of 6.1 percent in 2011 , citing final data ."
},
{
"location-value-pair": {
"Russia": 8.8
},
"parsedSentence": "In 2010 , annual inflation in LOCATION_SLOT hit NUMBER_SLOT percent due to the summer drought , exceeding forecasts and equalling the figure for 2009 , the year of the global financial meltdown .",
"sentence": "In 2010 , annual inflation in Russia hit 8.8 percent due to the summer drought , exceeding forecasts and equalling the figure for 2009 , the year of the global financial meltdown ."
}, ...
我想要做的是比较每个句子,并为该句子中的每个位置和值,计算与第一个字典中的位置 - 值对匹配的最接近的匹配值,然后返回它对应的顶级统计属性并将其添加为句子词典的新键。
例如:
对于第1句,我看到我们正在看俄罗斯,其值为6.1。我想索引到第一个字典,找到"俄罗斯",并浏览所有存在的值,例如65700.0,42530.0,1736050505050.0,8683048195.0。然后我想找到每个属性的平均绝对误差,例如size_of_armed_forces值为23%,gni_per_capita属性为10%等。然后,我想找到最小的一个,并将其作为第二个字典的键添加,所以:
{
"location-value-pair": {
"Russia": 6.1
},
"predictedRegion": "/location/statistical_region/gni_in_ppp_dollars"
"meanabserror": 2%
"parsedSentence": "On Tuesday , the Federal State Statistics Service -LRB- Rosstat -RRB- reported that consumer price inflation in LOCATION_SLOT hit a historic post-Soviet period low of NUMBER_SLOT percent in 2011 , citing final data .",
"sentence": "On Tuesday , the Federal State Statistics Service -LRB- Rosstat -RRB- reported that consumer price inflation in Russia hit a historic post-Soviet period low of 6.1 percent in 2011 , citing final data ."
},
在考虑编写本文时,我的困惑就是如何访问另一个字典的键值作为另一个字典的条件。我目前的想法是:
def predictRegion(sentenceArray,trueDict):
absPercentageErrors = {}
for location, property2value in trueDict.items():
print location
absPercentageErrors['location'] = {}
for property,trueValue in property2value.iteritems():
print property
absError = abs(sentenceArray['sentences']['location-value-pair'].key() - trueValue)
absPercentageErrors['location']['property'] = absError/numpy.abs(trueValue)
for index, dataTriples in enumerate(sentenceArray["sentences"]):
for location, trueValue in dataTriples['location-value-pair'].items():
print location
但显然我无法访问此行中的sentenceArray['sentences']['location-value-pair'].key()
:absError = abs(sentenceArray['sentences']['location-value-pair'].key() - trueValue)
因为它位于循环之外。
如何从一个引用完全不同的变量的循环中访问此键?
答案 0 :(得分:1)
将来请阅读如何制定一个好问题:https://stackoverflow.com/help/mcve
最低,完整且可验证。
我认为这就是你要找的东西。
countries = {'Canada': {'a': 10, 'b': 150, 'c': 1000},
'Russia': {'d': 9, 'e': 5, 'f': 1e5}}
sentences = [
{"location-value-pair": {"Russia": 6.1},
"parsedSentence": "bob loblaw",
"sentence": "lobs law bomb"
},
{"location-value-pair": {"Russia": 8.8},
"parsedSentence": "some sentence",
"sentence": "lorem ipsum test"
}]
def absError(numer,denom):
return abs(numer-denom)/float(denom)
def findMatch(target, country):
return min(country, key= lambda x: absError(target, country.get(x)))
def update(sentence):
(c,target), = sentence.get("location-value-pair").items()
country = countries[c]
matched = findMatch(target,country)
error = absError(target, country.get(matched))
res = sentence.copy()
res.update({'predictedRegion': matched, 'meanabserror': "{:.2f}%".format(100*error)})
return res
updated = [update(sentence) for sentence in sentences]
updated
输出:
[{'location-value-pair': {'Russia': 6.1},
'meanabserror': '22.00%',
'parsedSentence': 'bob loblaw',
'predictedRegion': 'e',
'sentence': 'lobs law bomb'},
{'location-value-pair': {'Russia': 8.8},
'meanabserror': '2.22%',
'parsedSentence': 'some sentence',
'predictedRegion': 'd',
'sentence': 'lorem ipsum test'}]