第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 pprint 函數(shù)打印前五個元素

使用 pprint 函數(shù)打印前五個元素

慕工程0101907 2023-10-06 18:41:43
我正在嘗試使用 pprint 函數(shù)打印前五個元素,但我無法讓它工作。我已經(jīng)嘗試過以下代碼,但它給了我一個錯誤。如何只打印前五個元素而不是前十個元素?wordCounts = pairs.reduceByKey(lambda x, y: x + y) wordCounts.pprint(:5)
查看完整描述

1 回答

?
www說

TA貢獻(xiàn)1775條經(jīng)驗(yàn) 獲得超8個贊

由于問題沒有提供簡單的數(shù)據(jù),我想該wordCounts變量是通過以下代碼準(zhǔn)備的。


import pprint

from pyspark.context import SparkContext

sc = SparkContext('local', 'test')

pairs = sc.parallelize([("a", 1), ("b", 1), ("b", 1), ("b", 1), ("b", 1), ("b", 1), ("d", 1), ("e", 1), ("a", 1), ("f", 1), ("c", 1)])

wordCounts = pairs.reduceByKey(lambda x, y: x + y)

您可以通過以下任一方式打印 wordCounts 中的值:


print(wordCounts.collect()[:5]) #Pick 5 elements

print(wordCounts.take(5)) #Pick 5 elements

print(sorted(wordCounts.collect())[:5]) #Sort the tuples, and pick the first 5 elements

print(sorted(wordCounts.collect(), key=lambda x: x[1], reverse=False)[:5]) #Sort by the second entry (i.e. count) in ascending order, and pick the first 5 elements

哪個產(chǎn)生


[('a', 2), ('b', 5), ('d', 1), ('e', 1), ('f', 1)]

[('a', 2), ('b', 5), ('d', 1), ('e', 1), ('f', 1)]

[('a', 2), ('b', 5), ('c', 1), ('d', 1), ('e', 1)]

[('d', 1), ('e', 1), ('f', 1), ('c', 1), ('a', 2)]

強(qiáng)烈建議您下次提供一個最小的可重現(xiàn)示例。


查看完整回答
反對 回復(fù) 2023-10-06
  • 1 回答
  • 0 關(guān)注
  • 130 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號