如何在字符串单词和数字的 RDD 中将数字字符串转换为 int?
How to convert numeric string to int in a RDD of string words and numbers?
所以我有一个 RDD,其中包含字符串格式的单词和数字,我已经拆分并删除了标点符号和空格:
['Hi', 'today', 'is', 'a', 'great', 'day', 'to', 'gather', 'flowers', 'lets', 'collect', '50', 'Roses', '400', 'Tulips', 'and', '20', 'Sunflowers', 'today']
我想计算不同单词的数量并按字母顺序和数字顺序对它们进行排序,这样输出将如下所示:
(20, 1)
(50, 1)
(400, 1)
('Hi', 1)
('today, 2)
我试过使用 sortby 但我怀疑因为数字是字符串,它只按第一个数字排序,因此数字 400 在 50 之前。我该如何解决这个问题?
您必须将 RDD 一分为二并执行归约和排序,然后合并结果
import re
numbers = (rdd.filter(lambda l: re.match('^[0-9]+$', l))
.map(lambda l: (int(l), 1))
.reduceByKey(lambda a,b: a+b)
.sortByKey())
text = (rdd.filter(lambda l: not re.match('^[0-9]+$', l))
.map(lambda l: (l, 1))
.reduceByKey(lambda a,b: a+b)
.sortByKey())
然后合并两者:
numbers.union(text).collect()
[(20, 1),
(50, 1),
(400, 1),
('Hi', 1),
('Roses', 1),
('Sunflowers', 1),
('Tulips', 1),
('a', 1),
('and', 1),
('collect', 1),
('day', 1),
('flowers', 1),
('gather', 1),
('great', 1),
('is', 1),
('lets', 1),
('to', 1),
('today', 2)]
这是因为您无法使用不同的比较对扁平 RDD 进行排序。
所以我有一个 RDD,其中包含字符串格式的单词和数字,我已经拆分并删除了标点符号和空格:
['Hi', 'today', 'is', 'a', 'great', 'day', 'to', 'gather', 'flowers', 'lets', 'collect', '50', 'Roses', '400', 'Tulips', 'and', '20', 'Sunflowers', 'today']
我想计算不同单词的数量并按字母顺序和数字顺序对它们进行排序,这样输出将如下所示:
(20, 1)
(50, 1)
(400, 1)
('Hi', 1)
('today, 2)
我试过使用 sortby 但我怀疑因为数字是字符串,它只按第一个数字排序,因此数字 400 在 50 之前。我该如何解决这个问题?
您必须将 RDD 一分为二并执行归约和排序,然后合并结果
import re
numbers = (rdd.filter(lambda l: re.match('^[0-9]+$', l))
.map(lambda l: (int(l), 1))
.reduceByKey(lambda a,b: a+b)
.sortByKey())
text = (rdd.filter(lambda l: not re.match('^[0-9]+$', l))
.map(lambda l: (l, 1))
.reduceByKey(lambda a,b: a+b)
.sortByKey())
然后合并两者:
numbers.union(text).collect()
[(20, 1),
(50, 1),
(400, 1),
('Hi', 1),
('Roses', 1),
('Sunflowers', 1),
('Tulips', 1),
('a', 1),
('and', 1),
('collect', 1),
('day', 1),
('flowers', 1),
('gather', 1),
('great', 1),
('is', 1),
('lets', 1),
('to', 1),
('today', 2)]
这是因为您无法使用不同的比较对扁平 RDD 进行排序。