2016-03-01 51 views
2

下面是代碼:Python是混合兩個列表,我不知道爲什麼

import requests 
import bs4 

response = requests.get('http://discoverygc.com/forums/serverinterface.php?action=players_online') #Loads page 
soup = bs4.BeautifulSoup(response.text) 
table = soup.find("div", {"id": "forum"}) 
rowsNo = (str(table).count('<tr>') - 2) #Number of players online. Minus 2 to remove leading title and column description rows 
players = systems = [] #Define lists 
for i in range(3, (rowsNo + 3)): 
    rows = table.findAll('tr')[i] 
    cols = rows.findAll('td') 
    player = cols[0].get_text() 
    system = cols[1].get_text() 
    players.append(player) 
    systems.append(system) 

print(players) 
print(systems) 

如果我刪除或者players.append(player)systems.append(system)代碼工作正常,輸出正確的列表:

['-Vasqez-', '[-=XTF=-]Neon.Bunny-[R]', "[SV]-Valley'", '<-JohnyWalker->', '~VP)Bad.Tibira', 'Alkanius', 'Apex91', 'Araroba', 'Baldor', 'Benediction', 'Black_Bird', 'Boost', 'Caelius.Moya[X]', 'Core|APM-Maverick', 'Daftwagen', 'Dee.Leers', 'Emiko:Hayashi', 'Gamma-6', 'Gauri', 'Gigi.7', 'GMG|GTS-Komahashi-Maru', 'Grawmod', 'GrazySlon', 'Hunor', 'Jakob-Schleiter', 'Joyita', 'Judge_BigJo', 'JulyJalwa', 'Kruger|KMS-Lankow', 'Luxor', 'monitor91', 'Morgulis', 'Nuggets', 'OSI-Mendes', 'Ronny.Rochester', 'Samura|-Arata', 'Samura|-Ichikawa', 'Shpritzen', 'Stardrifter', 'The_Altair', 'The.Liner.of.Dreams', 'Tony.Sosa', 'Wilde.RNC-Nestor'] 

或:

['Omega-11', 'Omega-49', 'Pennsylvania', 'Magellan', 'Omicron Gamma', 'Kyushu', 'Pennsylvania', 'Kyushu', 'Omega-5', 'Manchester', 'Cassini', 'Newcastle', 'Connecticut', 'Omega-47', 'Stuttgart', 'Stuttgart', 'Munich', 'New York', 'Hudson', 'Sigma-13', 'Languedoc', 'Colorado', 'Virginia', 'Stuttgart', 'New London', 'Magellan', 'New York', 'New Tokyo', 'Manchester', 'New York', 'Pennsylvania', 'Omega-3', 'Omega-49', 'New Berlin', 'California', 'Nagano', 'New Berlin', 'Okinawa', 'Magellan', 'Texas', 'Ontario', 'New Berlin', 'Stuttgart'] 

但是,如果我把兩條線都混合在一起的兩個列表:

['-Vasqez-', 'Omega-11', "[SV]-Valley'", 'Omega-49', '<-JohnyWalker->', 'Pennsylvania', '=Z=Exositas', 'Magellan', '~VP)Death.Incarnator', 'Omicron Gamma', 'Alkanius', 'Shikoku', 'Apex91', 'Pennsylvania', 'Baldor', 'Kyushu', 'Benediction', 'Omega-5', 'Black_Bird', 'Manchester', 'Boost', 'Cassini', 'Caelius.Moya[X]', 'Connecticut', 'Core|APM-Maverick', 'Omega-47', 'Daftwagen', 'Stuttgart', 'Darf.Acour', 'Texas', 'Dee.Leers', 'New Berlin', 'Emiko:Hayashi', 'Munich', 'Gamma-6', 'New York', 'Gauri', 'Hudson', 'Gigi.7', 'Orkney', 'GMG|GTS-Komahashi-Maru', 'Colorado', 'Grawmod', 'Virginia', 'GrazySlon', 'Stuttgart', 'Hunor', 'Manchester', 'Jakob-Schleiter', 'New Berlin', 'Joyita', 'Magellan', 'Judge_BigJo', 'New York', 'Kruger|KMS-Lankow', 'New Tokyo', 'Luxor', 'Manchester', 'monitor91', 'New York', 'Morgulis', 'Pennsylvania', 'Nuggets', 'Omega-3', 'OSI-Mendes', 'Omega-49', 'Ronny.Rochester', 'California', 'Samura|-Arata', 'Nagano', 'Samura|-Ichikawa', 'New Berlin', 'Stardrifter', 'Okinawa', 'The_Altair', 'Magellan', 'Tony.Sosa', 'Ontario', 'Wilde.RNC-Nestor', 'Omega-7'] 

這是爲什麼?我看不出爲什麼會發生這種情況。

+5

'玩家=系統= []'意味着它們是相同的列表。如果你想要一個單線程玩家,系統= [],[]'。或者你可以把它分成兩行。 –

回答

6

不這樣做,他們將指向同一個列表:

players = systems = [] #Define lists 

但它們分割:

players = [] 
systems = [] #Define lists 

然後,你將有兩個單獨的列表。

你的風格是用於創建一個列表(在某些情況下,它可能是有用的)的名稱別名,創建兩個不同的列表

+0

啊,當然,這是愚蠢的我:D謝謝! – Grinchell

+0

@Biggles太棒了!很高興你發現答案有幫助。 ;) – Ian

2

,你必須players = systems = []改變它分離作業。

players = [] 
systems = [] 
2

如果你想保持它在同一行:

players, systems = [], [] 
相關問題