2017-09-30 79 views
2

我想创建一个列表对象,其中包含有关职业球手的数据。不同的数据点是高尔夫球员的名字,并把不同距离的百分比。我希望在为每个玩家对象输入所有数据后,按名称对这个对象列表进行排序。这些对象的列表被称为PlayerNumber。当我尝试按属性'name'排序PlayerNumber时。我得到一个错误,指出'int'没有属性,我不确定为什么PlayerNumber被称为整数而不是列表。'诠释'对象没有属性

任何帮助,将不胜感激。这里是代码:

import operator 
import numpy as np 
import statistics 
import matplotlib.pyplot as plt 
from colour import Color 
from bs4 import BeautifulSoup 
import urllib3 

############### ACCESS WEBPAGES  #################### 
def makeSoup(url): 
    http = urllib3.PoolManager() 
    response = http.request('GET', url) 
    soupdata = BeautifulSoup(response.data) 
    return soupdata 

siteURL = [] 
for i in range(7): 
    siteURL.append(i) 
siteURL[0] = '' 
siteURL[1] = 'http://www.pgatour.com/stats/stat.408.html' #>25 
siteURL[2] = 'http://www.pgatour.com/stats/stat.407.html' #20-25 
siteURL[3] = 'http://www.pgatour.com/stats/stat.406.html' #15-20 
siteURL[4] = 'http://www.pgatour.com/stats/stat.405.html' #10-15 
siteURL[5] = 'http://www.pgatour.com/stats/stat.404.html' #5-10 
siteURL[6] = 'http://www.pgatour.com/stats/stat.02427.html' #3-5 

############### ACCESS TABLE DATA  ################### 
def row_number(soupdata): 
    for row in table.findAll('tr'): 
     tot_row = row 
    return tot_row 

def parse_table(soupdata): 
    currRank = [] 
    prevRank = [] 
    playerName = [] 
    rounds = [] 
    pctMake = [] 
    attempts = [] 
    puttsMade = [] 
    table = soupdata.find('tbody') 
    tot_row = 0 
    for row in table.findAll('tr'): 
     #for col in row.findAll('td'): 
     col = row.find_all('td') 
     #column_1 = col[0] 
     #currRank.append(column_1) 
     #column_2 = col[1] 
     #prevRank.append(column_2) 
     column_3 = col[2].text 
     column_3.strip() 
     playerName.append(column_3) 
     #column_4 = col[3] 
     #rounds.append(column_4) 
     column_5 = col[4].text 
     pctMake.append(column_5)    
     #column_6 = col[5] 
     #attempts.append(column_6)  
     #column_7 = col[6] 
     #puttsMade.append(column_7) 
     tot_row += 1 
    #return currRank, prevRank, playerName, rounds, pctMake, attempts, puttsMade 
    return playerName, pctMake, tot_row 

""" 
>25 ft: distance1 
20-25 ft: distance2 
15-20 ft: distance3 
10-15 ft: distance4 
5-10 ft: distance5 
3-5 ft: distance6 
""" 
############### CLASS DEFINITION  ################### 
class Player: 
    id_list={} 
    def __init__(self,name, id, dis1=0.0, dis2=0.0, dis3=0.0, dis4=0.0, dis5=0.0, dis6=0.0): 
     self.name = name 
     self.dis1 = dis1 
     self.dis2 = dis2 
     self.dis3 = dis3 
     self.dis4 = dis4 
     self.dis5 = dis5 
     self.dis6 = dis6 
     self.id = id 
     Player.id_list[self.name] = self # save the id as key and self as he value 
    def addDis1(self,distance1): 
     self.dis1 = float(distance1) 
    def addDis2(self,distance2): 
     self.dis2 = float(distance2) 
    def addDis3(self,distance3): 
     self.dis3 = float(distance3) 
    def addDis4(self,distance4): 
     self.dis4 = float(distance4) 
    def addDis5(self,distance5): 
     self.dis5 = float(distance5) 
    def addDis6(self,distance6): 
     self.dis6 = float(distance6) 
    def displayPlayer(self): 
     print("Player: ", self.name, '\n' 
       ">25 Ft %: ", self.dis1, '\n' 
       "20-25 Ft %: ", self.dis2, '\n' 
       "15-20 Ft %: ", self.dis3, '\n' 
       "10-15 Ft %: ", self.dis4, '\n' 
       "5-10 Ft %: ", self.dis5, '\n' 
       "3-5 Ft %: ", self.dis6, '\n') 
    @classmethod 
    def lookup_player_name_by_id(cls, name): 
     try: 
      return cls.id_list[name] # return the instance with the id 
     except KeyError: # error check for if id does not exist 
      raise KeyError("No user with id %s" % str(id)) 

############### DATA POPULATION  ################### 
PlayerNumber=[] 
for i in range(0,195): 
    PlayerNumber.append(i) 
for i in range(1,7): 
    soupdata = makeSoup(siteURL[i]) 
    playerName, pctMake, tot_row = parse_table(soupdata) 
    for x in range(0,tot_row): 
     #PlayerNumber.append(x) 
     name = playerName[x] 
     name = name.replace("\xa0", " ") 
     name = name.replace("\n", "") 
     if i == 1: 
      PlayerNumber[x] = Player(name, x) 
      Player.addDis1(PlayerNumber[x],pctMake[x]) 
     if i == 2: 
      val = Player.lookup_player_name_by_id(name) 
      Player.addDis2(PlayerNumber[val.id],pctMake[x]) 
     if i == 3: 
      val = Player.lookup_player_name_by_id(name) 
      Player.addDis3(PlayerNumber[val.id],pctMake[x]) 
     if i == 4: 
      val = Player.lookup_player_name_by_id(name) 
      Player.addDis4(PlayerNumber[val.id],pctMake[x]) 
     if i == 5: 
      val = Player.lookup_player_name_by_id(name) 
      Player.addDis5(PlayerNumber[val.id],pctMake[x]) 
     if i == 6: 
      val = Player.lookup_player_name_by_id(name) 
      Player.addDis6(PlayerNumber[val.id],pctMake[x]) 

PlayerNumber.sort(key = operator.attrgetter("name"))   
#PlayerNumber[2].displayPlayer() 

我使用Python 3.4 spyder IDE。作为FYI,我相对比较新。

谢谢!

+0

'x = 4; x.name' - 代码有效地做到了这一点。为什么?信任错误消息并向后工作。 – user2864740

+0

是不是整数x只是指的是对象被存储在列表中的索引位置?我不明白代码试图将属性名称附加到x的位置。 – Will

+0

那个'.name'上的那一行会抛出错误?它被试图抵抗的价值是什么?错误消息说这两个。为什么此时的值是一个整数? – user2864740

回答

2

这并不是说PlayerNumber被称为整数,而是PlayerNumber是整数列表,并且该列表(和整数)的每个元素都没有属性“name”,该排序()试图访问(为了对它们进行排序)。

编辑:

为了详细说明,在倒数第二行的样品中:

PlayerNumber.sort(键= operator.attrgetter( “名字”))

是尝试使用比较函数:operator.attrgetter(“name”)来排序PlayerNumber,这意味着它必须在PlayerNumber的每个元素上调用该函数以获得排序数组中的排名。这就是为什么你试图从PlayerNumber中的整数中获取一个.name属性。

相关问题