【问题标题】:Python for loops stops after first iterationPython for 循环在第一次迭代后停止
【发布时间】:2017-09-16 07:05:29
【问题描述】:

我在调试下面的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']} }

这是我的代码:

def get_secondary_connections(network, user):
    if user not in network:
        return None
    sec_connections = []
    for friend in network[user]['connections']:
        for connection in network[friend]['connections']:
            if connection not in sec_connections:
                sec_connections.append(connection)
    return sec_connections

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

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

如果您查看我的网络,这只是 Walter 的连接列表。我应该得到梅赛德斯二级连接的完整列表,即:

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

有人可以帮帮我吗?

【问题讨论】:

  • 无法复制。运行您的代码显示的结果与您想要的输出完全相同。
  • 您的代码看起来正确。你确定输入正确吗?
  • 谢谢大家,我意识到我的编辑器出了点问题,它没有正确运行我的代码。

标签: python for-loop


【解决方案1】:

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

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

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

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']} 
}

def get_secondary_connections(network, user):
    if user not in network:
        return None
    sec_connections = []
    for friend in network[user]['connections']:
        for connection in network[friend]['connections']:
            if connection not in sec_connections:
                sec_connections.append(connection)
    return sec_connections

print get_secondary_connections(network, "Mercedes")

【讨论】:

  • 除了问题中所说的之外,您没有提供解决方案。一条评论就足够了。
  • 真的很抱歉。但是这里的按钮说“发布你的答案”,我发布了我的答案。
  • 你错了。答案应添加一些原始问题中未包含的新解决方案或功能。
  • 我真的很抱歉。我不是故意侮辱你的。你看,我是 Stack Overflow 的新手。我只是在学习做这些事情:) 此外,评论用于要求澄清或指出帖子中的问题,您也不应该将评论部分用于辅助讨论。也就是说,我们现在做的事情是错误的。