Я расширил ответ @ Уибла несколькими незначительными способами:
- Получает данные прямо со страницы Википедии
- Использует itertools.product вместо пользовательской make_combination ()
- Автоматически перебирает доступные атрибуты
- Показывает элементы top-N и bottom-N для каждого атрибута для каждого размера всадника
.
from BeautifulSoup import BeautifulSoup
import urllib2
import collections
import itertools
def value(s):
"Parse string to integer (return 0 on bad string)"
try:
return int(s)
except ValueError:
return 0
attrs = ('speed','weight','acceleration','handling','drift','offroad','turbo')
Stats = collections.namedtuple('Stats', attrs)
def add_stats(xs,ys):
return Stats(*(x+y for x,y in zip(xs,ys)))
class ThingWithStats(object):
def __init__(self, stats):
super(ThingWithStats,self).__init__()
self.stats = Stats(*stats)
def __str__(self):
return str(self.stats)
class Driver(ThingWithStats):
@classmethod
def fromRow(cls,size,row):
name = row.th.getText().strip()
stats = [value(col.getText()) for col in row.findAll('td')]
return cls(name, size, stats)
def __init__(self, name, size, stats):
super(Driver,self).__init__(stats)
self.name = name
self.size = size
def __str__(self):
return "{0:32} ({1}): {2}".format(self.name, self.size, self.stats)
class Vehicle(ThingWithStats):
@classmethod
def fromRow(cls, size, kind, row):
items = [col.getText() for col in row.findAll('td')]
name = items[0].strip()
stats = [value(item) for item in items[1:8]]
return cls(name, size, kind, stats)
def __init__(self, name, size, kind, stats):
super(Vehicle,self).__init__(stats)
self.name = name
self.size = size
self.kind = kind
def __str__(self):
return "{0:30} ({1} {2}): {3}".format(self.name, self.size, self.kind, self.stats)
class DrivenVehicle(ThingWithStats):
def __init__(self, driver, vehicle):
if driver.size != vehicle.size:
raise ValueError('Driver {0} cannot fit vehicle {1}'.format(driver.name, vehicle.name))
self.driver = driver
self.vehicle = vehicle
self.stats = add_stats(driver.stats, vehicle.stats)
def __str__(self):
return "{0} riding {1}: {2}".format(self.driver.name, self.vehicle.name, self.stats)
def getDrivers(table):
rows = table.findAll('tr')[2:] # skip two header rows
sizes = 'm'*8 + 's'*8 + 'l'*8 + 'sml' # this is cheating a bit, but I don't see any way to get it from the table
return [Driver.fromRow(size,row) for size,row in zip(sizes,rows)]
def getVehicles(table):
sz = {'Small':'s', 'Medium':'m', 'Large':'l'}
kd = {'Karts':'k', 'Bikes':'b'}
size,kind = None,None
cars = []
for row in table.findAll('tr'):
heads = row.findAll('th')
if len(heads)==1: # main table heading
pass
elif len(heads)==10: # sub-heading
s,k = heads[0].getText().strip().split()
size,kind = sz[s], kd[k]
else: # data
cars.append(Vehicle.fromRow(size,kind,row))
return cars
def getData():
url = 'http://www.mariowiki.com/Mario_Kart_Wii' # page to look at
data = urllib2.urlopen(url).read() # get raw html
soup = BeautifulSoup(data) # parse
drivers = []
cars = []
for table in soup.findAll('table'): # look at all tables in page
try:
head = table.th.getText()
if 'Character Bonuses' in head:
drivers = getDrivers(table)
elif 'Vehicle Stats' in head:
cars = getVehicles(table)
except AttributeError:
pass
return drivers,cars
def binBy(attr, lst):
res = collections.defaultdict(list)
for item in lst:
res[getattr(item,attr)].append(item)
return res
def main():
drivers,cars = getData()
drivers = binBy('size', drivers)
cars = binBy('size', cars)
sizes = list(set(drivers.keys()) & set(cars.keys()))
sizes.sort()
combos = {}
for size in sizes:
combos[size] = [DrivenVehicle(driver,car) for driver,car in itertools.product(drivers[size], cars[size])]
topN = 3
for attr in attrs:
print "By {0}:".format(attr)
for size in sizes:
combos[size].sort(key=lambda dv: getattr(dv.stats,attr), reverse=True)
print " ({0})".format(size)
for dv in combos[size][:topN]:
print ' '+str(dv)
print ' ...'
for dv in combos[size][-topN:]:
print ' '+str(dv)
if __name__=="__main__":
main()
и окончательные результаты:
By speed:
(l)
Funky Kong riding Aero Glider/ Jetsetter: Stats(speed=73, weight=56, acceleration=21, handling=17, drift=27, offroad=19, turbo=16)
Rosalina riding Aero Glider/ Jetsetter: Stats(speed=72, weight=56, acceleration=21, handling=20, drift=27, offroad=16, turbo=19)
Large Mii riding Aero Glider/ Jetsetter: Stats(speed=72, weight=56, acceleration=24, handling=20, drift=30, offroad=16, turbo=19)
...
Donkey Kong riding Wario Bike: Stats(speed=37, weight=62, acceleration=53, handling=58, drift=21, offroad=45, turbo=51)
King Boo riding Wario Bike: Stats(speed=37, weight=59, acceleration=51, handling=61, drift=21, offroad=48, turbo=48)
Dry Bowser riding Wario Bike: Stats(speed=37, weight=59, acceleration=51, handling=56, drift=21, offroad=51, turbo=54)
(m)
Daisy riding B Dasher Mk. 2/ Sprinter: Stats(speed=68, weight=48, acceleration=27, handling=26, drift=37, offroad=21, turbo=27)
Medium Mii riding B Dasher Mk. 2/ Sprinter: Stats(speed=67, weight=51, acceleration=27, handling=24, drift=37, offroad=24, turbo=27)
Luigi riding B Dasher Mk. 2/ Sprinter: Stats(speed=66, weight=54, acceleration=27, handling=24, drift=37, offroad=21, turbo=24)
...
Birdo riding Bon Bon/ Sugarscoot: Stats(speed=32, weight=35, acceleration=54, handling=62, drift=35, offroad=54, turbo=61)
Diddy Kong riding Bon Bon/ Sugarscoot: Stats(speed=32, weight=32, acceleration=57, handling=62, drift=38, offroad=51, turbo=61)
Bowser Jr. riding Bon Bon/ Sugarscoot: Stats(speed=32, weight=32, acceleration=54, handling=62, drift=35, offroad=54, turbo=59)
(s)
Baby Luigi riding Blue Falcon: Stats(speed=65, weight=37, acceleration=35, handling=29, drift=43, offroad=24, turbo=29)
Baby Daisy riding Blue Falcon: Stats(speed=65, weight=35, acceleration=35, handling=29, drift=43, offroad=24, turbo=32)
Baby Peach riding Blue Falcon: Stats(speed=63, weight=35, acceleration=38, handling=32, drift=43, offroad=24, turbo=29)
...
Toad riding Nano Bike/ Bit Bike: Stats(speed=25, weight=18, acceleration=65, handling=67, drift=46, offroad=56, turbo=62)
Koopa Troopa riding Nano Bike/ Bit Bike: Stats(speed=25, weight=18, acceleration=59, handling=70, drift=40, offroad=56, turbo=68)
Dry Bones riding Nano Bike/ Bit Bike: Stats(speed=25, weight=18, acceleration=62, handling=67, drift=43, offroad=56, turbo=68)
By weight:
(l)
Bowser riding Piranha Prowler: Stats(speed=57, weight=72, acceleration=29, handling=35, drift=38, offroad=29, turbo=27)
Wario riding Piranha Prowler: Stats(speed=55, weight=70, acceleration=29, handling=35, drift=35, offroad=32, turbo=33)
Donkey Kong riding Piranha Prowler: Stats(speed=55, weight=70, acceleration=31, handling=37, drift=35, offroad=29, turbo=30)
...
Waluigi riding Twinkle Star/ Shooting Star: Stats(speed=50, weight=48, acceleration=35, handling=32, drift=64, offroad=30, turbo=59)
King Boo riding Twinkle Star/ Shooting Star: Stats(speed=50, weight=48, acceleration=29, handling=37, drift=59, offroad=30, turbo=59)
Dry Bowser riding Twinkle Star/ Shooting Star: Stats(speed=50, weight=48, acceleration=29, handling=32, drift=59, offroad=33, turbo=65)
(m)
Luigi riding Wild Wing: Stats(speed=59, weight=57, acceleration=21, handling=29, drift=59, offroad=24, turbo=59)
Mario riding Wild Wing: Stats(speed=57, weight=57, acceleration=23, handling=31, drift=62, offroad=24, turbo=59)
Luigi riding B Dasher Mk. 2/ Sprinter: Stats(speed=66, weight=54, acceleration=27, handling=24, drift=37, offroad=21, turbo=24)
...
Peach riding Bon Bon/ Sugarscoot: Stats(speed=34, weight=32, acceleration=59, handling=62, drift=41, offroad=51, turbo=56)
Diddy Kong riding Bon Bon/ Sugarscoot: Stats(speed=32, weight=32, acceleration=57, handling=62, drift=38, offroad=51, turbo=61)
Bowser Jr. riding Bon Bon/ Sugarscoot: Stats(speed=32, weight=32, acceleration=54, handling=62, drift=35, offroad=54, turbo=59)
(s)
Baby Luigi riding Rally Romper/ Tiny Titan: Stats(speed=51, weight=43, acceleration=43, handling=43, drift=29, offroad=64, turbo=40)
Baby Mario riding Rally Romper/ Tiny Titan: Stats(speed=46, weight=43, acceleration=43, handling=49, drift=29, offroad=64, turbo=40)
Baby Daisy riding Rally Romper/ Tiny Titan: Stats(speed=51, weight=41, acceleration=43, handling=43, drift=29, offroad=64, turbo=43)
...
Toad riding Quacker: Stats(speed=32, weight=17, acceleration=73, handling=60, drift=68, offroad=48, turbo=57)
Koopa Troopa riding Quacker: Stats(speed=32, weight=17, acceleration=67, handling=63, drift=62, offroad=48, turbo=63)
Dry Bones riding Quacker: Stats(speed=32, weight=17, acceleration=70, handling=60, drift=65, offroad=48, turbo=63)
By acceleration:
(l)
Waluigi riding Wario Bike: Stats(speed=37, weight=59, acceleration=57, handling=56, drift=26, offroad=48, turbo=48)
Waluigi riding Offroader: Stats(speed=39, weight=64, acceleration=54, handling=54, drift=23, offroad=46, turbo=45)
Large Mii riding Wario Bike: Stats(speed=40, weight=59, acceleration=54, handling=59, drift=24, offroad=45, turbo=51)
...
Rosalina riding Flame Flyer: Stats(speed=65, weight=59, acceleration=16, handling=24, drift=48, offroad=18, turbo=51)
King Boo riding Flame Flyer: Stats(speed=62, weight=59, acceleration=16, handling=26, drift=48, offroad=21, turbo=48)
Dry Bowser riding Flame Flyer: Stats(speed=62, weight=59, acceleration=16, handling=21, drift=48, offroad=24, turbo=54)
(m)
Peach riding Nostalgia 1/ Classic Dragster: Stats(speed=39, weight=43, acceleration=64, handling=54, drift=60, offroad=40, turbo=51)
Diddy Kong riding Nostalgia 1/ Classic Dragster: Stats(speed=37, weight=43, acceleration=62, handling=54, drift=57, offroad=40, turbo=56)
Mario riding Nostalgia 1/ Classic Dragster: Stats(speed=37, weight=49, acceleration=61, handling=56, drift=57, offroad=40, turbo=51)
...
Birdo riding Wild Wing: Stats(speed=57, weight=54, acceleration=21, handling=29, drift=59, offroad=27, turbo=64)
Daisy riding Wild Wing: Stats(speed=61, weight=51, acceleration=21, handling=31, drift=59, offroad=24, turbo=62)
Bowser Jr. riding Wild Wing: Stats(speed=57, weight=51, acceleration=21, handling=29, drift=59, offroad=27, turbo=62)
(s)
Toad riding Quacker: Stats(speed=32, weight=17, acceleration=73, handling=60, drift=68, offroad=48, turbo=57)
Toad riding Cheep Charger: Stats(speed=34, weight=24, acceleration=70, handling=56, drift=65, offroad=45, turbo=54)
Baby Peach riding Quacker: Stats(speed=35, weight=23, acceleration=70, handling=63, drift=62, offroad=48, turbo=57)
...
Small Mii riding Concerto/ Mini Beast: Stats(speed=58, weight=35, acceleration=29, handling=32, drift=67, offroad=27, turbo=67)
Toadette riding Concerto/ Mini Beast: Stats(speed=58, weight=32, acceleration=29, handling=32, drift=64, offroad=33, turbo=64)
Koopa Troopa riding Concerto/ Mini Beast: Stats(speed=55, weight=32, acceleration=29, handling=35, drift=64, offroad=27, turbo=70)
By handling:
(l)
King Boo riding Wario Bike: Stats(speed=37, weight=59, acceleration=51, handling=61, drift=21, offroad=48, turbo=48)
Large Mii riding Wario Bike: Stats(speed=40, weight=59, acceleration=54, handling=59, drift=24, offroad=45, turbo=51)
Rosalina riding Wario Bike: Stats(speed=40, weight=59, acceleration=51, handling=59, drift=21, offroad=45, turbo=51)
...
Wario riding Aero Glider/ Jetsetter: Stats(speed=69, weight=59, acceleration=21, handling=17, drift=27, offroad=19, turbo=22)
Funky Kong riding Aero Glider/ Jetsetter: Stats(speed=73, weight=56, acceleration=21, handling=17, drift=27, offroad=19, turbo=16)
Dry Bowser riding Aero Glider/ Jetsetter: Stats(speed=69, weight=56, acceleration=21, handling=17, drift=27, offroad=22, turbo=22)
(m)
Mario riding Bon Bon/ Sugarscoot: Stats(speed=32, weight=38, acceleration=56, handling=64, drift=38, offroad=51, turbo=56)
Daisy riding Bon Bon/ Sugarscoot: Stats(speed=36, weight=32, acceleration=54, handling=64, drift=35, offroad=51, turbo=59)
Peach riding Bon Bon/ Sugarscoot: Stats(speed=34, weight=32, acceleration=59, handling=62, drift=41, offroad=51, turbo=56)
...
Yoshi riding B Dasher Mk. 2/ Sprinter: Stats(speed=64, weight=51, acceleration=27, handling=24, drift=40, offroad=26, turbo=24)
Birdo riding B Dasher Mk. 2/ Sprinter: Stats(speed=64, weight=51, acceleration=27, handling=24, drift=37, offroad=24, turbo=29)
Bowser Jr. riding B Dasher Mk. 2/ Sprinter: Stats(speed=64, weight=48, acceleration=27, handling=24, drift=37, offroad=24, turbo=27)
(s)
Baby Mario riding Nano Bike/ Bit Bike: Stats(speed=25, weight=26, acceleration=59, handling=73, drift=40, offroad=56, turbo=62)
Baby Peach riding Nano Bike/ Bit Bike: Stats(speed=28, weight=24, acceleration=62, handling=70, drift=40, offroad=56, turbo=62)
Koopa Troopa riding Nano Bike/ Bit Bike: Stats(speed=25, weight=18, acceleration=59, handling=70, drift=40, offroad=56, turbo=68)
...
Baby Daisy riding Blue Falcon: Stats(speed=65, weight=35, acceleration=35, handling=29, drift=43, offroad=24, turbo=32)
Small Mii riding Blue Falcon: Stats(speed=63, weight=32, acceleration=35, handling=29, drift=46, offroad=24, turbo=32)
Toadette riding Blue Falcon: Stats(speed=63, weight=29, acceleration=35, handling=29, drift=43, offroad=30, turbo=29)
By drift:
(l)
Waluigi riding Twinkle Star/ Shooting Star: Stats(speed=50, weight=48, acceleration=35, handling=32, drift=64, offroad=30, turbo=59)
Large Mii riding Twinkle Star/ Shooting Star: Stats(speed=53, weight=48, acceleration=32, handling=35, drift=62, offroad=27, turbo=62)
Bowser riding Twinkle Star/ Shooting Star: Stats(speed=52, weight=53, acceleration=29, handling=32, drift=62, offroad=27, turbo=59)
...
Wario riding Phantom: Stats(speed=43, weight=54, acceleration=43, handling=48, drift=17, offroad=59, turbo=46)
Funky Kong riding Phantom: Stats(speed=47, weight=51, acceleration=43, handling=48, drift=17, offroad=59, turbo=40)
Dry Bowser riding Phantom: Stats(speed=43, weight=51, acceleration=43, handling=48, drift=17, offroad=62, turbo=46)
(m)
Peach riding Mach Bike: Stats(speed=57, weight=37, acceleration=29, handling=32, drift=68, offroad=27, turbo=62)
Mario riding Mach Bike: Stats(speed=55, weight=43, acceleration=26, handling=34, drift=65, offroad=27, turbo=62)
Diddy Kong riding Mach Bike: Stats(speed=55, weight=37, acceleration=27, handling=32, drift=65, offroad=27, turbo=67)
...
Medium Mii riding Turbo Blooper/ Super Blooper: Stats(speed=53, weight=43, acceleration=35, handling=37, drift=21, offroad=57, turbo=38)
Birdo riding Turbo Blooper/ Super Blooper: Stats(speed=50, weight=43, acceleration=35, handling=37, drift=21, offroad=57, turbo=40)
Bowser Jr. riding Turbo Blooper/ Super Blooper: Stats(speed=50, weight=40, acceleration=35, handling=37, drift=21, offroad=57, turbo=38)
(s)
Toad riding Bullet Bike: Stats(speed=53, weight=24, acceleration=38, handling=35, drift=73, offroad=29, turbo=67)
Dry Bones riding Bullet Bike: Stats(speed=53, weight=24, acceleration=35, handling=35, drift=70, offroad=29, turbo=73)
Small Mii riding Bullet Bike: Stats(speed=56, weight=27, acceleration=32, handling=35, drift=70, offroad=29, turbo=70)
...
Baby Luigi riding Rally Romper/ Tiny Titan: Stats(speed=51, weight=43, acceleration=43, handling=43, drift=29, offroad=64, turbo=40)
Baby Daisy riding Rally Romper/ Tiny Titan: Stats(speed=51, weight=41, acceleration=43, handling=43, drift=29, offroad=64, turbo=43)
Toadette riding Rally Romper/ Tiny Titan: Stats(speed=49, weight=35, acceleration=43, handling=43, drift=29, offroad=70, turbo=40)
By offroad:
(l)
Dry Bowser riding Phantom: Stats(speed=43, weight=51, acceleration=43, handling=48, drift=17, offroad=62, turbo=46)
Waluigi riding Phantom: Stats(speed=43, weight=51, acceleration=49, handling=48, drift=22, offroad=59, turbo=40)
King Boo riding Phantom: Stats(speed=43, weight=51, acceleration=43, handling=53, drift=17, offroad=59, turbo=40)
...
Bowser riding Aero Glider/ Jetsetter: Stats(speed=71, weight=61, acceleration=21, handling=17, drift=30, offroad=16, turbo=16)
Rosalina riding Aero Glider/ Jetsetter: Stats(speed=72, weight=56, acceleration=21, handling=20, drift=27, offroad=16, turbo=19)
Donkey Kong riding Aero Glider/ Jetsetter: Stats(speed=69, weight=59, acceleration=23, handling=19, drift=27, offroad=16, turbo=19)
(m)
Yoshi riding Rapide/ Zip Zip: Stats(speed=41, weight=38, acceleration=45, handling=51, drift=32, offroad=67, turbo=45)
Medium Mii riding Rapide/ Zip Zip: Stats(speed=44, weight=38, acceleration=45, handling=51, drift=29, offroad=65, turbo=48)
Birdo riding Rapide/ Zip Zip: Stats(speed=41, weight=38, acceleration=45, handling=51, drift=29, offroad=65, turbo=50)
...
Diddy Kong riding B Dasher Mk. 2/ Sprinter: Stats(speed=64, weight=48, acceleration=30, handling=24, drift=40, offroad=21, turbo=29)
Daisy riding B Dasher Mk. 2/ Sprinter: Stats(speed=68, weight=48, acceleration=27, handling=26, drift=37, offroad=21, turbo=27)
Luigi riding B Dasher Mk. 2/ Sprinter: Stats(speed=66, weight=54, acceleration=27, handling=24, drift=37, offroad=21, turbo=24)
(s)
Toadette riding Magikruiser: Stats(speed=46, weight=24, acceleration=45, handling=45, drift=32, offroad=73, turbo=43)
Toadette riding Rally Romper/ Tiny Titan: Stats(speed=49, weight=35, acceleration=43, handling=43, drift=29, offroad=70, turbo=40)
Toad riding Magikruiser: Stats(speed=43, weight=24, acceleration=51, handling=45, drift=38, offroad=67, turbo=43)
...
Koopa Troopa riding Blue Falcon: Stats(speed=60, weight=29, acceleration=35, handling=32, drift=43, offroad=24, turbo=35)
Baby Luigi riding Blue Falcon: Stats(speed=65, weight=37, acceleration=35, handling=29, drift=43, offroad=24, turbo=29)
Baby Daisy riding Blue Falcon: Stats(speed=65, weight=35, acceleration=35, handling=29, drift=43, offroad=24, turbo=32)
By turbo:
(l)
Dry Bowser riding Twinkle Star/ Shooting Star: Stats(speed=50, weight=48, acceleration=29, handling=32, drift=59, offroad=33, turbo=65)
Wario riding Twinkle Star/ Shooting Star: Stats(speed=50, weight=51, acceleration=29, handling=32, drift=59, offroad=30, turbo=65)
Dry Bowser riding Dragonetti/ Honeycoupe: Stats(speed=53, weight=62, acceleration=27, handling=29, drift=56, offroad=30, turbo=62)
...
King Boo riding Aero Glider/ Jetsetter: Stats(speed=69, weight=56, acceleration=21, handling=22, drift=27, offroad=19, turbo=16)
Funky Kong riding Aero Glider/ Jetsetter: Stats(speed=73, weight=56, acceleration=21, handling=17, drift=27, offroad=19, turbo=16)
Bowser riding Aero Glider/ Jetsetter: Stats(speed=71, weight=61, acceleration=21, handling=17, drift=30, offroad=16, turbo=16)
(m)
Birdo riding Mach Bike: Stats(speed=55, weight=40, acceleration=24, handling=32, drift=62, offroad=30, turbo=67)
Diddy Kong riding Mach Bike: Stats(speed=55, weight=37, acceleration=27, handling=32, drift=65, offroad=27, turbo=67)
Medium Mii riding Mach Bike: Stats(speed=58, weight=40, acceleration=24, handling=32, drift=62, offroad=30, turbo=65)
...
Peach riding B Dasher Mk. 2/ Sprinter: Stats(speed=66, weight=48, acceleration=32, handling=24, drift=43, offroad=21, turbo=24)
Mario riding B Dasher Mk. 2/ Sprinter: Stats(speed=64, weight=54, acceleration=29, handling=26, drift=40, offroad=21, turbo=24)
Luigi riding B Dasher Mk. 2/ Sprinter: Stats(speed=66, weight=54, acceleration=27, handling=24, drift=37, offroad=21, turbo=24)
(s)
Dry Bones riding Bullet Bike: Stats(speed=53, weight=24, acceleration=35, handling=35, drift=70, offroad=29, turbo=73)
Koopa Troopa riding Bullet Bike: Stats(speed=53, weight=24, acceleration=32, handling=38, drift=67, offroad=29, turbo=73)
Small Mii riding Bullet Bike: Stats(speed=56, weight=27, acceleration=32, handling=35, drift=70, offroad=29, turbo=70)
...
Baby Mario riding Blue Falcon: Stats(speed=60, weight=37, acceleration=35, handling=35, drift=43, offroad=24, turbo=29)
Baby Peach riding Blue Falcon: Stats(speed=63, weight=35, acceleration=38, handling=32, drift=43, offroad=24, turbo=29)
Baby Luigi riding Blue Falcon: Stats(speed=65, weight=37, acceleration=35, handling=29, drift=43, offroad=24, turbo=29)