了解列表输出

时间:2015-01-30 10:24:55

标签: python

我正在尝试测试opensource包paralex - [readme][1],我希望在给定的函数中看到结果和得分的值。

代码是:

def run_on_question(s, con):
    q = get_sent(s)
    scored_answers = defaultdict(lambda: float('-inf'))
    scored_queries = lex.parse.get_scored_queries(q, con.nl_vocab, con.lexicon, con.weights)
    i = 0
    for score, query in sorted(scored_queries, reverse=True):
        print ' Score : %s  - query :  %s' % ( score, query)
        #print "%s %s " % (score, query) this also gives same result

调用函数是:

def get_scored_queries(sent, nl_vocab, lexicon, weights):
    parses = parse(sent, nl_vocab, lexicon)
    scored = list((score_parse(p, weights), p) for p in parses)
    best_scores = defaultdict(lambda: float('-inf'))
    for s, p in scored:
        best_scores[p.meaning] = max(best_scores[p.meaning], s)
    results = list((v,k) for (k,v) in best_scores.iteritems())
    results.sort(reverse=True)
    return results

结果如下:

Score : -0.444444444449  - query :  (2, 1, 798848, 660874)
 Score : -0.444444444449  - query :  (2, 1, 787068, 660874)
 Score : -0.444444444449  - query :  (2, 1, 786980, 660874)
 Score : -0.555555555555  - query :  (2, 1, 820915, 589484)
 Score : -0.555555555555  - query :  (2, 1, 798901, 589484)
 Score : -0.666666666667  - query :  (2, 1, 725195, 660811)
 Score : -0.666666666667  - query :  (2, 1, 725195, 660727)
 Score : -0.666666666671  - query :  (2, 1, 708932, 660874)
 Score : -0.777777777777  - query :  (2, 1, 725198, 589484)
 Score : -0.777777777778  - query :  (2, 1, 798848, 2551164)
 Score : -0.777777777778  - query :  (2, 1, 798848, 996458)
 Score : -0.888888888889  - query :  (2, 1, 725196, 589484)
 Score : -0.888888888889  - query :  (2, 1, 708932, 2569298)
 Score : -0.888888888889  - query :  (2, 1, 708932, 2327035)
 Score : -0.888888888889  - query :  (2, 1, 708932, 2036311)
 Score : -1.0  - query :  (2, 1, 880853, 555134)
 Score : -1.0  - query :  (2, 1, 880797, 555134)
 Score : -1.0  - query :  (2, 1, 880794, 555134)
 Score : -1.0  - query :  (2, 1, 880791, 555134)

这种结果会以什么格式出现?

1 个答案:

答案 0 :(得分:0)

您正在看到使用格式打印scores_queries的排序输出:

  for score, query in sorted(scored_queries, reverse=True):
        print ' Score : %s  - query :  %s' % ( score, query)

results是来自best_scores dict的排序列表,其中每个元组中的第一项是来自dict的每个值,第二项是每个键。

如果您想查看结果,只需在函数中添加print(results)即可。