【发布时间】:2016-05-05 21:10:22
【问题描述】:
这个 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 你应该提到它作为答案,点赞!!
-
你还没有写出输入是什么,也没有写出预期的输出是什么。
标签: python list for-loop dictionary