python - Python 3:将元组转换为字典
问题描述
试图转换这个:
word_vomit = [(('best', 'cat', 'breed'), 3), (('dogs', 'wearing', 'hats'), 3), (('did', 'you', 'eat'), 2), (('cats', 'are', 'evil'), 1), (('i', 'hate', 'lists'), 1)]
进入这个:
goal = {'best cat breed': '3','dogs wearing hats': '3','did you eat': '2','cats are evil': '1','i hate lists': '1'}
在此先感谢您的任何和所有评论
解决方案
这是您可以使用的方法,
word_vomit = [(('best', 'cat', 'breed'), 3), (('dogs', 'wearing', 'hats'), 3), (('did', 'you', 'eat'), 2), (('cats', 'are', 'evil'), 1), (('i', 'hate', 'lists'), 1)]
dict1={}
for i in word_vomit:
str1=""
str1=" ".join(i[0])
dict1[str1]=str(i[1])
print(dict1)
输出:
{'best cat breed': '3', 'dogs wearing hats': '3', 'did you eat': '2', 'cats are evil': '1', ' i hate lists': '1'}
希望这对你有帮助!
推荐阅读
- latex - 使一行粗体 Stata esttab 回归到 LaTeX
- python - python3.8.10 源代码构建错误 [Makefile:617: sharedmods] 错误 139
- r - 使用ggplot2从两列分组箱线图
- python - 通过 python 运行 CMD 命令失败
- flutter - 如何不打印从 sqflite 数据库接收到的空值?
- docker - 没有从树莓派托管 docker 的路由
- linux - 如何使用名称中的计数将不同文件夹中的所有文件重命名为另一个文件夹?
- google-cloud-platform - 更新 GKE 上的 HPA 冷却时间
- html - 如何全选在一个班级里面
- javascript - 在 Angular 项目中导入 CdkDragDrop 时出现问题 - 类型“事件”缺少来自类型“CdkDragDrop...”的以下属性