Python for循环在第一次迭代后停止

2024-06-01 03:54:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我在调试下面的get_secondary_connections函数时遇到问题。由于某些原因,我的for friend in network[user][‘connections’]循环总是在列表中的第一个值处停止,而不是在整个列表中循环。我不明白为什么会这样。有人能给我一些指导吗?非常感谢!在

这是我的网络:

network = { 

'Freda': {'connections': ['Olive', 'John', 'Debra'], 'favorite games': ['Starfleet Commander', 'Ninja Hamsters', 'Seahorse Adventures']}, 

'Ollie': {'connections': ['Mercedes', 'Freda', 'Bryant'], 'favorite games': ['Call of Arms', 'Dwarves and Swords', 'The Movie: The Game']}, 

'Debra': {'connections': ['Walter', 'Levi', 'Jennie', 'Robin'], 'favorite games': ['Seven Schemers', 'Pirates in Java Island', 'Dwarves and Swords']}, 

'Olive': {'connections': ['John', 'Ollie'], 'favorite games': ['The Legend of Corgi', 'Starfleet Commander']}, 

'Levi': {'connections': ['Ollie', 'John', 'Walter'], 'favorite games': ['The Legend of Corgi', 'Seven Schemers', 'City Comptroller: The Fiscal Dilemma']}, 

'Jennie': {'connections': ['Levi', 'John', 'Freda', 'Robin'], 'favorite games': ['Super Mushroom Man', 'Dinosaur Diner', 'Call of Arms']}, 

'Mercedes': {'connections': ['Walter', 'Robin', 'Bryant'], 'favorite games': ['The Legend of Corgi', 'Pirates in Java Island', 'Seahorse Adventures']}, 

'John': {'connections': ['Bryant', 'Debra', 'Walter'], 'favorite games': ['The Movie: The Game', 'The Legend of Corgi', 'Dinosaur Diner']}, 

'Robin': {'connections': ['Ollie'], 'favorite games': ['Call of Arms', 'Dwarves and Swords']}, 

'Bryant': {'connections': ['Olive', 'Ollie', 'Freda', 'Mercedes'], 'favorite games': ['City Comptroller: The Fiscal Dilemma', 'Super Mushroom Man']},

'Walter': {'connections': ['John', 'Levi', 'Bryant'], 'favorite games': ['Seahorse Adventures', 'Ninja Hamsters', 'Super Mushroom Man']} }

我的代码是:

^{pr2}$

当我运行get_secondary_connections(网络,“Mercedes”)时,我得到以下输出:

[‘John’, ‘Levi’, ‘Bryant’]

如果你在我的网络上查过的话,那只是沃尔特的联系人名单。我应该得到梅赛德斯二次连接的完整列表,那就是:

[‘John’, ‘Levi’, ‘Bryant’, ‘Ollie’, ‘Olive’, Freda’, ‘Mercedes’]

有人能帮帮我吗?在


Tags: ofthejohnconnectionsfavoritegamesmercedesrobin
1条回答
网友
1楼 · 发布于 2024-06-01 03:54:15

我复制粘贴了你的代码,运行程序,得到了输出:

['John', 'Levi', 'Bryant', 'Ollie', 'Olive', 'Freda', 'Mercedes']

这个输出就是你想要的,对吧?你的代码没有问题。如果您愿意,代码如下所示:

^{pr2}$

相关问题 更多 >