python - 从列表中填充字典

标签 python list python-3.x dictionary

我有一个字符串列表(来自 .tt 文件),如下所示:

list1 = ['have\tVERB', 'and\tCONJ', ..., 'tree\tNOUN', 'go\tVERB']

我想把它变成一个看起来像这样的字典:

dict1 = { 'have':'VERB', 'and':'CONJ', 'tree':'NOUN', 'go':'VERB' }

我在考虑替代,但效果不佳。有没有办法将制表符字符串 '\t' 标记为分隔符?

最佳答案

尝试以下操作:

dict1 = dict(item.split('\t') for item in list1)

输出:

>>>dict1
{'and': 'CONJ', 'go': 'VERB', 'tree': 'NOUN', 'have': 'VERB'}

关于python - 从列表中填充字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40767647/

相关文章:

python - 在 OpenCV 中查找子像素中的轮廓点

python - 获取问题评论 JIRA python

python - 在python中选择名称列表中的最大迭代名称

r - 轴标签列表中下标错误

python - 我可以根据将键与两个互斥键列表匹配来从单个 python 字典理解中产生两个总和吗?

python - SQLAlchemy 在执行查询中缓存 now()

python - 使用 Beautiful Soup 查找包含搜索词的链接

r - 导出到 R 中列表的 JSON 列表

python - 将 rbf 与 scipy 一起使用时出现内存错误

python - Python 中的函数和可变列表