Python for循环迭代

Python for循环迭代,python,loops,dictionary,Python,Loops,Dictionary,我试图在使用循环时填充字典,问题是循环在满足条件后停止,而不检查是否有更多可用条件需要满足 sec=[ [1, 2, 4.0, 100.0], [2, 3, 1.5, 500.0], [2, 4, 10.0, 700.0], [2, 5, 5.75, 1500.0], [3, 4, 11.4, 200.0], [4, 5, 10.5, 750.0], [4, 6, 6.75, 550.0]] 我列了这张单子,还有

我试图在使用循环时填充字典,问题是循环在满足条件后停止,而不检查是否有更多可用条件需要满足

sec=[
     [1, 2, 4.0, 100.0], 
     [2, 3, 1.5, 500.0], 
     [2, 4, 10.0, 700.0], 
     [2, 5, 5.75, 1500.0], 
     [3, 4, 11.4, 200.0], 
     [4, 5, 10.5, 750.0], 
     [4, 6, 6.75, 550.0]]
我列了这张单子,还有这本字典

graph={1:[1],2:[2],3:[3],4:[4],5:[5],6:[6]}
我想要完成的是

graph = {1: ['2'],
         2: ['3', '4','5'],
         3: ['4'],
         4: ['5','6'],
         5: [],
         6: []}
它的工作原理是收集所有
sec[x][0]
作为字典的键,而
sec[n][1]
作为字典中的值 如果sec[x][0]的值为1,sec[x][1]的值为2,则数字2将作为键1的值添加到字典中 我得到的密码是

def magic(numList): #turns string into int
   s = ''.join(map(str, numList))
   return int(s)
for i in range(1,len(loc)+1): #len of loc is 6
    for n in range(0,len(loc)+1):
        if magic(graph[i])==sec[n][0]:
            graph[i].append(sec[n][1])

但它只会添加第一个值,然后索引n将停止计数,然后索引i继续运行,因此它不会检查键中是否有更多值

您对
图形的初始定义对预期结果没有帮助。使用空列表初始化值,然后在简单循环中追加:

>>> sec=[
     [1, 2, 4.0, 100.0], 
     [2, 3, 1.5, 500.0], 
     [2, 4, 10.0, 700.0], 
     [2, 5, 5.75, 1500.0], 
     [3, 4, 11.4, 200.0], 
     [4, 5, 10.5, 750.0], 
     [4, 6, 6.75, 550.0]]
>>> graph = {i:[] for i in range(1,7)}
>>> for x,y,*z in sec: graph[x].append(str(y))

>>> graph
{1: ['2'], 2: ['3', '4', '5'], 3: ['4'], 4: ['5', '6'], 5: [], 6: []}

您对
图形的初始定义对预期结果没有帮助。使用空列表初始化值,然后在简单循环中追加:

>>> sec=[
     [1, 2, 4.0, 100.0], 
     [2, 3, 1.5, 500.0], 
     [2, 4, 10.0, 700.0], 
     [2, 5, 5.75, 1500.0], 
     [3, 4, 11.4, 200.0], 
     [4, 5, 10.5, 750.0], 
     [4, 6, 6.75, 550.0]]
>>> graph = {i:[] for i in range(1,7)}
>>> for x,y,*z in sec: graph[x].append(str(y))

>>> graph
{1: ['2'], 2: ['3', '4', '5'], 3: ['4'], 4: ['5', '6'], 5: [], 6: []}

使用“集合”模块中的defaultdict。见下文:

from collections import defaultdict

graph_dd = defaultdict(list)

for s in sec:
    from_, to_ = s[:2]
    graph_dd[from_].append(to_)
    # do-nothing reference to to_ creates empty list, so that all nodes are represented
    # in graph_dd; else 5 and 6 would not be created
    graph_dd[to_]

# convert to regular dict (not really necessary, but OP wanted a dict)
graph = dict(graph_dd.items())

for k,v in graph.items():
    print(k,':', v)
印刷品:

1 : [2]
2 : [3, 4, 5]
3 : [4]
4 : [5, 6]
5 : []
6 : []

使用“集合”模块中的defaultdict。见下文:

from collections import defaultdict

graph_dd = defaultdict(list)

for s in sec:
    from_, to_ = s[:2]
    graph_dd[from_].append(to_)
    # do-nothing reference to to_ creates empty list, so that all nodes are represented
    # in graph_dd; else 5 and 6 would not be created
    graph_dd[to_]

# convert to regular dict (not really necessary, but OP wanted a dict)
graph = dict(graph_dd.items())

for k,v in graph.items():
    print(k,':', v)
印刷品:

1 : [2]
2 : [3, 4, 5]
3 : [4]
4 : [5, 6]
5 : []
6 : []

如果不知道恰当命名的
magic
函数在做什么,很难说代码为什么不工作。还有,那些应该是字符串列表吗?抱歉,忘了提一下,def magic(numList):#将字符串转换为int s=''。join(map(str,numList))return int(s)基本上将字符串转换为int值如果不知道恰当命名的
magic
函数在做什么,很难说出代码为什么不工作。还有,那些应该是字符串列表吗?抱歉忘了提一下,def magic(numList):#将字符串转换为int s=''。join(map(str,numList))return int(s)基本上将字符串转换为int值可能值得一提的是
defaultdict(list)
@roganjosh,但这样你就不会有那些
[]
。注意:
*z
仅适用于Python 3.x;对于较旧的版本,请使用
for x,y,uu,u,u,u
谢谢@tobias_k我想知道*z对于Python2用户意味着什么可能值得一提
defaultdict(list)
@roganjosh可能吧,但这样你就不会有那些
[]
。注意:
*z
只在Python3.x中工作;对于较旧的版本,请使用
for x,y,,
谢谢@tobias_k我想知道*z作为Python2用户意味着什么