gpt4 book ai didi

python Spark根据值对元素进行排序

转载 作者:太空宇宙 更新时间:2023-11-03 17:40:22 25 4
gpt4 key购买 nike

我是 python Spark 新手,需要您的帮助,提前致谢!

所以我们开始吧,我有这段脚本:

from datetime import datetime
from pyspark import SparkContext

def getNormalizedDate(dateOfCL):
#the result will be in [0,1]
dot=datetime.now()
od=datetime.strptime("Jan 01 2010", "%b %d %Y")

return (float((dateOfCL-od).days)/float((dot-od).days))

def addition(a, b):
a1=a
b1=b
if not type(a) is float:
a1=getNormalizedDate(a)
if not type(b) is float:
b1=getNormalizedDate(b)

return float(a1+b1)

def debugFunction(x):
print "x[0]: " + str(type(x[0]))
print "x[1]: " + str(type(x[1])) + " --> " + str(x[1])
return x[1]



if __name__ == '__main__':
sc = SparkContext("local", "File Scores")

textFile = sc.textFile("/data/spark/file.csv")
#print "Number of lines: " + str(textFile.count())

test1 = textFile.map(lambda line: line.split(";"))
# result of this:
# [u'01', u'01', u'add', u'fileName', u'Path', u'1', u'info', u'info2', u'info3', u'Sep 24 2014']

test2 = test1.map(lambda line: (line[3], datetime.strptime(line[len(line)-1], "%b %d %Y")))

test6=test2.reduceByKey(addition)
#print test6
test6.persist()

result=sorted(test6.collect(), key=debugFunction)

这以错误结束:

Traceback (most recent call last):
File "/data/spark/script.py", line 40, in <module>
result=sorted(test6.collect(), key=lambda x:x[1])
TypeError: can't compare datetime.datetime to float

有关信息,test6.collect() 提供此内容

[(u'file1', 0.95606060606060606), 
(u'file2', 0.91515151515151516),
(u'file3', 0.8797979797979798),
(u'file4', 0.0),
(u'file5', 0.94696969696969702),
(u'file6', 0.95606060606060606),
(u'file7', 0.98131313131313136),
(u'file8', 0.86161616161616161)]

我想根据浮点值(不是键)对其进行排序请问应该如何进行?

谢谢大家。

最佳答案

对于那些可能感兴趣的人,我发现了这个问题。我按键减少,然后执行值列表中包含的项目的添加。一些文件是唯一的,不会受到此减少的影响,因此它们仍然具有日期而不是 float 。

我现在做的是

test2 = test1.map(lambda line: (line[3], line[len(line)-1])).map(getNormalizedDate)

这将生成 (file, float) 对

然后,我按键减少

最后一步

result=sorted(test6.collect(), key=lamba x:x[1])

为我提供了我正在寻找的正确排序。

我希望这有帮助!

关于python Spark根据值对元素进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30648449/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com