首页 > 解决方案 > 如何将此元组更改为另一个全字元组?

问题描述

我有元组:

wordsTuple = [(('431949',['python',
                          'print',
                          'hellow',
                          'world',
                          'at',
                          'py',
                          'file',
                          ...]

我想把它改成[(python, 1), (print, 1) ...]. 我怎么能只使用 PySpark 中的一行代码或某些功能来实现这一点?

counts = wordsTuple._________________

标签: pythonpysparkword-count

解决方案


如果你真的想要一个固定的“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


推荐阅读