list在python中的作用_在Python中,+=在list中的意义

我目前正在研究图形遍历。find_path,路径递增

likepath = path + [start]返回[30, 3, 5, 8],而{}带有路径

incremented likepath +=[start]返回一个包含find_path2所拥有的所有值的列表

已经遍历,[30, 3, 4, 0, 1, 2, 5, 8]。在

我猜+=的作用类似于append方法,其中path = path + [start]

重新分配吗?在

有人能解释一下path +=[start]之间的区别吗

和path = path + [start]def find_path2(graph, start, end, path=[]):

path += [start]

if start == end:

return path

if not start in graph:

return None

for node in graph[start]:

if node not in path:

newpath = find_path2(graph, node, end, path)

if newpath: return newpath

return None

def find_path(graph, start, end, path=[]):

path = path + [start]

if start == end:

return path

if not start in graph:

return None

for node in graph[start]:

if node not in path:

newpath = find_path(graph, node, end, path)

if newpath: return newpath

return None

graph = {

0: [],

1: [],

2: [],

3: [4,2,5],

4: [0,1],

5:[8],

10:[5,6],

6:[],

30:[3]

}

print find_path(graph,30,8) #[30, 3, 5, 8]

print find_path2(graph,30,8) #[30, 3, 4, 0, 1, 2, 5, 8]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值