python - 如何将此元组更改为另一个全字元组?
问题描述
我有元组:
wordsTuple = [(('431949',['python',
'print',
'hellow',
'world',
'at',
'py',
'file',
...]
我想把它改成[(python, 1), (print, 1) ...]
. 我怎么能只使用 PySpark 中的一行代码或某些功能来实现这一点?
counts = wordsTuple._________________
解决方案
如果你真的想要一个固定的“1”作为每个元组的第二项,那么它很简单
wordsTuple = ('431949',['python', 'print', 'hellow', 'world', 'at', 'py', 'file'])
counts = [(x,1) for x in wordsTuple[1]]
counts
[('python', 1), ('print', 1), ('hellow', 1), ('world', 1), ('at', 1), ('py', 1), ('file', 1)]
相反,如果您正在寻找每个世界的出现次数,请检查collections.Counter
类
推荐阅读
- reactjs - Dialog打开时如何关闭MenuItem
- go - 检查键是否存在于地图中作为“if语句”条件
- boolean - 仅在 NAND 中的等效表达式
- node.js - 密码查询未给出结果
- html - 使用 ::after 选择器为按钮设置动画
- javascript - 同一行中的两个字符串
- swift - iOS 13.2 从 MapKit 中删除覆盖导致地图闪烁
- symfony - services.yaml 中的 SetTemplate 不适用于我自己的模板
- rust - 如何在 Rust 中克隆 Rc 的选项?
- python - 无法安装 python bcs 它说这个版本已经存在但是这个版本不存在