2017-08-02 159 views
2

我写了一个for循环,假设这是一个足球队的胜利纪录并将其分开,以获得赢得比赛和比赛失败的价值。不幸的是我的分割(' - ')命令似乎没有在我写的for循环中使用时返回一个列表。.Split()for for循环不返回列表

该数据集从维基百科上获取并且数据在熊猫数据框内。

这里是我得到的数据框:

test = pd.read_html('https://en.m.wikipedia.org/wiki/ 
    List_of_Michigan_Wolverines_football_seasons') 

year_football = test[-1].T.set_index(0).T.dropna(axis=0, thresh=3) 

,我想通过迭代名单是:

#format W-L 
win_loss = ['7–1' '6–2' '2–6' '1–7' '3–5' '6–2' '6–2' '3–5' '3–5' '6–2' '7–2'] 

其中我做了一下清洁,然后调用:

print(year_football['Conference'].values) 

我的for循环:

wins = [] 
games = [] 

for season in year_football['Conference'].values: 
     win_loss = season.split('-') 
     wins.append(win_loss[0]) 
     games.append(int(win_loss[0])) + int(win_loss[1])) 
     print(season) 
     print(type(season)) 
     print(win_loss) 

输出为列表的第一个成员是:

7–1 #print(season) 
<class 'str'> #print(type(season)) 
['7–1'] #print(win_loss) 

我无法弄清楚我做错了什么,.split()工作的罚款之外循环。希望不是拼写错误。 (另外,在Jupyter运行是否有帮助)

+8

要拆分的'-'分裂,但你'season'包含'-' - 看到其中的差别?不一样的性格。 –

+8

'ord(' - ')'是8211; 'ord(' - ')'是45. – DyZ

+0

只是为了澄清,它*是*返回一个列表,它只是不分裂你想要的地方。 –

回答

4

更改您的for循环来对实际字符

for season in year_football['Conference'].values: 
     win_loss = season.split(chr(8211)) # I changed this line 
     wins.append(win_loss[0]) 
     games.append(int(win_loss[0])) + int(win_loss[1])) 
     print(season) 
     print(type(season)) 
     print(win_loss)