I have a dictionary of bigrams, obtained by importing a csv and transforming it to a dictionary:

bigram_dict = {"('key1', 'key2')": 'meaning', "('key22', 'key13')": 'mean2'}

I want keys' dictionary to be without quotation marks, i.e.:
desired_bigram_dict={('key1', 'key2'): 'meaning', ('key22', 'key13'): 'mean2'}

你能建议我怎么做吗?

最佳答案

这可以使用字典理解来完成,在这里您可以在键上调用literal_eval

from ast import literal_eval
bigram_dict = {"('key1', 'key2')": 'meaning', "('key22', 'key13')": 'mean2'}

res = {literal_eval(k): v for k,v in bigram_dict.items()}

结果:
{('key22', 'key13'): 'mean2', ('key1', 'key2'): 'meaning'}

关于python - 从字典中删除引号,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39943765/

10-10 19:17