Мне нужна помощь в выводе трех лучших городов с наибольшим доходом.Прямо сейчас у меня есть все города с общим доходом, но мне нужно ограничить этот результат только до верхних 3.
У меня есть все города с общим доходом.
"""
Python script to find the total amount of sales revenue for each city
using Map-Reduce framework (mapper, combiner, and reducer functions) with mrjob package
4/14/17
"""
from mrjob.job import MRJob
class CityRevenue(MRJob):
# each input lines consists of city, productCategory, price, and paymentMode
def mapper(self, _, line):
# create a key-value pair with key: city and value: price
line_cols = line.split(',')
yield line_cols[0], float(line_cols[2])
def combiner(self, city, counts):
# consolidates all key-value pairs of mapper function (performed at mapper nodes)
yield city, sum(counts)
def reducer(self, city, counts):
# final consolidation of key-value pairs at reducer nodes
yield city, '${:,.2f}'.format(sum(counts))
if __name__ == '__main__':
CityRevenue.run()
Фактическая:
"Albuquerque" "$1,208,490.13"
"Anaheim" "$1,264,165.71"
"Anchorage" "$1,191,057.61"
"Arlington" "$1,229,375.89"
"Atlanta" "$1,216,153.47"
"Aurora" "$1,216,807.43"
"Austin" "$1,208,925.21"
"Bakersfield" "$1,211,742.46"
"Baltimore" "$1,225,227.14"
"Baton Rouge" "$1,214,852.71"
"Birmingham" "$1,218,785.75"
"Boise" "$1,216,941.64"
"Boston" "$1,204,833.39"
"Buffalo" "$1,190,531.15"
"Chandler" "$1,192,263.53"
"Charlotte" "$1,233,641.50"
"Chesapeake" "$1,242,760.99"
"Chicago" "$1,219,848.29"
"Chula Vista" "$1,241,528.46"
"Cincinnati" "$1,218,642.84"
Ожидаемая:
"Anaheim" "$1,264,165.71"
"Chesapeake" "$1,242,760.99"
"Chula Vista" "$1,241,528.46"