我在Udacity上了一节课,我正在准备我的最后一个项目。你知道吗
我必须通过解析一个字符串来创建一个数据结构,这个字符串是:-
example_input="John is connected to Bryant, Debra, Walter.\ John likes to play The Movie: The Game, The Legend of Corgi, Dinosaur Diner.\ Bryant is connected to Olive, Ollie, Freda, Mercedes.\ Bryant likes to play City Comptroller: The Fiscal Dilemma, Super Mushroom Man.\ Mercedes is connected to Walter, Robin, Bryant.\ Mercedes likes to play The Legend of Corgi, Pirates in Java Island, Seahorse Adventures.\ Olive is connected to John, Ollie.\ Olive likes to play The Legend of Corgi, Starfleet Commander.\ Debra is connected to Walter, Levi, Jennie, Robin.\ Debra likes to play Seven Schemers, Pirates in Java Island, Dwarves and Swords.\ Walter is connected to John, Levi, Bryant.\ Walter likes to play Seahorse Adventures, Ninja Hamsters, Super Mushroom Man.\ Levi is connected to Ollie, John, Walter.\ Levi likes to play The Legend of Corgi, Seven Schemers, City Comptroller: The Fiscal Dilemma.\ Ollie is connected to Mercedes, Freda, Bryant.\ Ollie likes to play Call of Arms, Dwarves and Swords, The Movie: The Game.\ Jennie is connected to Levi, John, Freda, Robin.\ Jennie likes to play Super Mushroom Man, Dinosaur Diner, Call of Arms.\ Robin is connected to Ollie.\ Robin likes to play Call of Arms, Dwarves and Swords.\ Freda is connected to Olive, John, Debra.\ Freda likes to play Starfleet Commander, Ninja Hamsters, Seahorse Adventures."
我成功地做到了这一点,我的输出数据结构显示为如下所示:你知道吗
[['John', ['Bryant', 'Debra', 'Walter'], ['The Movie: The Game', 'The Legend of Corgi', 'Dinosaur Diner']], ['Bryant', ['Olive', 'Ollie', 'Freda', 'Mercedes'], ['City Comptroller: The Fiscal Dilemma', 'Super Mushroom Man']], ['Mercedes', ['Walter', 'Robin', 'Bryant'], ['The Legend of Corgi', 'Pirates in Java Island', 'Seahorse Adventures']], ['Olive', ['John', 'Ollie'], ['The Legend of Corgi', 'Starfleet Commander']], ['Debra', ['Walter', 'Levi', 'Jennie', 'Robin'], ['Seven Schemers', 'Pirates in Java Island', 'Dwarves and Swords']], ['Walter', ['John', 'Levi', 'Bryant'], ['Seahorse Adventures', 'Ninja Hamsters', 'Super Mushroom Man']], ['Levi', ['Ollie', 'John', 'Walter'], ['The Legend of Corgi', 'Seven Schemers', 'City Comptroller: The Fiscal Dilemma']], ['Ollie', ['Mercedes', 'Freda', 'Bryant'], ['Call of Arms', 'Dwarves and Swords', 'The Movie: The Game']], ['Jennie', ['Levi', 'John', 'Freda', 'Robin'], ['Super Mushroom Man', 'Dinosaur Diner', 'Call of Arms']], ['Robin', ['Ollie'], ['Call of Arms', 'Dwarves and Swords']], ['Freda', ['Olive', 'John', 'Debra'], ['Starfleet Commander', 'Ninja Hamsters', 'Seahorse Adventures']]]
问题是,我需要定义一个过程来查找连接的连接,我这样做了,但它没有正常运行。嵌套循环只运行一次迭代。我不知道为什么。我试着正常循环,也通过使用范围函数。我也使用了while循环,但没有任何效果。这是我的密码下图:-你知道吗
def get_secondary_connections(network, user):
found=False
for entry in network:
if entry[0]==user:
connections=entry[1]
found=True
connection_of_connections=[]
i=0
while i <len(connections):
for entry in network:
if connections[i] in entry[0]:
connection_of_connections=connection_of_connections+entry[1]
i=i+1
if found == True:
return connection_of_connections
else:
return None
print get_secondary_connections(net, 'John')
从代码的缩进中不太清楚您打算如何进行搜索,但是我用您的数据测试了以下修改的版本,它似乎可以工作:
希望这有帮助。你知道吗
相关问题 更多 >
编程相关推荐