這個for循環應該迭代我的輸入中的'行',因爲它保存了用戶名,我想把它放在字典中作爲關鍵字,並在該關鍵字中包含一個字典。我的輸出確實包括我需要的信息,但也包括我不想要的奇數行。 我是新來的python,所以我仍然試圖理解語法。 這裏是我的代碼:我的for循環出了什麼問題? Python
def get_name(string_input):
split_fullstop = string_input.split('.')
list = [] #creates a list
for line in split_fullstop:
count = 0
if count % 2 == 0: #if count is even
list.append(line.split('is connected to')) #add info to 'list'
count += 1 #increase count
names = {name[0]:{} for name in list}
return names
在這裏,打印功能之後的輸出:
{'': {}, 'Levi ': {}, 'Bryant ': {}, 'Jennie likes to play Super Mushroom Man, Dinosaur Diner, Call of Arms': {}, 'Olive likes to play The Legend of Corgi, Starfleet Commander': {}, 'Debra likes to play Seven Schemers, Pirates in Java Island, Dwarves and Swords': {}, 'Levi likes to play The Legend of Corgi, Seven Schemers, City Comptroller: The Fiscal Dilemma': {}, 'Walter ': {}, 'Robin ': {}, 'John ': {}, 'Walter likes to play Seahorse Adventures, Ninja Hamsters, Super Mushroom Man': {}, 'Debra ': {}, 'Freda likes to play Starfleet Commander, Ninja Hamsters, Seahorse Adventures': {}, 'Mercedes likes to play The Legend of Corgi, Pirates in Java Island, Seahorse Adventures': {}, 'Ollie ': {}, 'Robin likes to play Call of Arms, Dwarves and Swords': {}, 'Bryant likes to play City Comptroller: The Fiscal Dilemma, Super Mushroom Man': {}, 'Freda ': {}, 'Olive ': {}, 'Mercedes ': {}, 'John likes to play The Movie: The Game, The Legend of Corgi, Dinosaur Diner': {}, 'Jennie ': {}, 'Ollie likes to play Call of Arms, Dwarves and Swords, The Movie: The Game': {}}
@AntonProtopopov您應該提到它作爲一個答案,給予好評! –
您沒有寫入輸入內容,也沒有輸出預期的內容。 – tglaria