【问题标题】:how can i call two dictionaries in files?如何在文件中调用两个字典?
【发布时间】:2015-12-17 02:19:58
【问题描述】:

从集合导入 OrderedDict 导入json

def read_classification_from_file(dict_file1,dict_file2): 使用 open(dict_file1,'r') 作为 f: dict1 = json.load(f, object_pairs_hook=OrderedDict) 使用 open(dict_file2,'r') 作为 f: data2 = json.load(f, object_pairs_hook=OrderedDict)

# Creates list of lists pairing each value in 
# dict1 with each value in dict2
return [[value1,value2] 
        for value1 in dict1.values() 
        for value2 in dict2.values()]

【问题讨论】:

  • 你能澄清你的帖子吗?完全不清楚你想做什么。
  • 第一个函数在您提供 truth_dict 和 pred_dict 字典时起作用,所以我想通过在 read_classification_from_file() 中打开 example1.txt 和 example2.txt 以不同的方式使用:然后我想要 compute_confusion_matrix(truth_dict , pred_dict, pos_tag=True, neg_tag=False): 在 read_classification_from_file() 中使用返回的字典:
  • 或者如果我可以在compute_confusion_matrix中打开两个文件(truth_dict, pred_dict, pos_tag=True, neg_tag=False):如果可能的话会更好

标签: python


【解决方案1】:

尚不完全清楚您想要什么,如果我对您的理解正确,那么完成您正在尝试做的事情的理想方法是采用不同的方法,但您想坚持这种方法,您可以使用 eval() 函数。您从 .txt 文件中读取字符串,但要使用该字符串,您需要将其作为参数传递给 eval() 函数,以便将其作为 Python 表达式进行解析和评估:

with open('example1.txt') as f:
    call = f.readline()

truth_dict = eval(call)

所以这可能有效:

from collections import namedtuple

def compute_confusion_matrix(file1, file2, pos_tag=True, neg_tag=False):
    TP=0
    FP=0
    TN=0
    FN=0
    with open(file1) as f:
        call = f.readline()
        truth_dict = eval(call)
    with open(file2) as f:
        call - f.readline()
        pred_dict = eval(call)

    for key,value in truth_dict.items():
        if truth_dict[key]==pred_dict[key] == neg_tag:
            TP +=1
        if pred_dict[key]==truth_dict[key] == pos_tag:
            FP +=1
        if truth_dict[key]==pred_dict[key] != neg_tag :
            TN +=1
        if truth_dict[key]==pred_dict[key] != pos_tag:
            FN +=1
    ConfMat = namedtuple('ConfMat', 'tp tn fp fn')

    p=ConfMat(TP, FP, TN, FN)
    return p

【讨论】:

  • 我可以在 compute_confusion_matrix(truth_dict, pred_dict, pos_tag=True, neg_tag=False) 中打开两个字典:不使用 read_classification_from_file():???
  • 您不是从 .txt 文件中“打开字典”。您正在阅读字符串,而不是字典。您需要将它们制作成字典,并且通过使用 eval 函数,您可以将这些字符串视为 Python 表达式,并像在“测试”中一样创建字典。否则,你只有字符串。
  • 好的,但是 .txt 文件已经是字典了,所以我想在 compute_confusion_matrix 中阅读它们,这样我就可以通过打印来测试函数
  • .txt 文件不是字典。它们是字符串。要将对象保存在文件中,您需要使用带有“pickle”模块的对象序列化:wiki.python.org/moin/UsingPickle
  • 抱歉,刚刚我意识到了,谢谢,但是您能否阅读 compute_confusion_matrix(truth_dict, pred_dict, pos_tag=True, neg_tag=False) 中的两个 .txt 文件并通过提供 .txt 文件来打印它参数
【解决方案2】:

如果您的数据是由其他来源创建或与其他来源共享的,请使用 json(仅使用双引号!)。可能希望使用 OrderedDict 来确保正确的顺序,或者更好的是,将您的数据重新组织为一个简单的值列表,因为键似乎并不那么重要:

from collections import OrderedDict
import json

def read_classification_from_file(dict_file1,dict_file2):
    with open(dict_file1,'r') as f:
        dict1 = json.load(f, object_pairs_hook=OrderedDict)
    with open(dict_file2,'r') as f:
        data2 = json.load(f, object_pairs_hook=OrderedDict)

    # Creates list of lists pairing each value in 
    # dict1 with each value in dict2
    return [[value1,value2] 
            for value1 in dict1.values() 
            for value2 in dict2.values()]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-28
    • 2012-03-21
    • 2017-12-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多