【问题标题】:How to change this tuple to another all word tuple?如何将此元组更改为另一个全字元组?
【发布时间】:2021-11-12 00:55:08
【问题描述】:

我有元组:

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

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

counts = wordsTuple._________________

【问题讨论】:

  • 您能清楚地说明您的wordsTuple 的样子吗?它只是 tupletuplelist 吗?描述显示不同的格式,而标题显示不同
  • 请澄清您的具体问题或提供其他详细信息以准确突出您的需求。正如目前所写的那样,很难准确地说出你在问什么。

标签: python pyspark word-count


【解决方案1】:

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

【讨论】:

  • 从问题中添加的wordsTuple的格式看,它不像[(('431949', [....]), ('', [...]))]的元组列表
  • 好吧,由于问题正文中的格式不清楚,我选择了标题中的格式(相反,我相信正文而不是预期输出的标题)。无论如何,我认为包含所有 2 项元组的元组的列表不是那么有用:) 也许它是一个元组列表,而不是我假设的单个元组。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-01-18
  • 2021-11-26
  • 2021-09-17
  • 2021-08-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多