本文介绍了从列表中填充字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个字符串列表(来自 .tt
文件),如下所示:
I have a list of strings (from a .tt
file) that looks like this:
list1 = ['have\tVERB', 'and\tCONJ', ..., 'tree\tNOUN', 'go\tVERB']
我想把它变成一个字典,如下所示:
I want to turn it into a dictionary that looks like:
dict1 = { 'have':'VERB', 'and':'CONJ', 'tree':'NOUN', 'go':'VERB' }
我正在考虑替换,但是不行。有没有办法将标签字符串'\t'
标记为分隔符?
I was thinking of substitution, but it doesn't work that well. Is there a way to tag the tab string '\t'
as a divider?
推荐答案
尝试以下操作:
dict1 = dict(item.split('\t') for item in list1)
输出:
>>>dict1
{'and': 'CONJ', 'go': 'VERB', 'tree': 'NOUN', 'have': 'VERB'}
这篇关于从列表中填充字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!