带有+ = b的语法难题

时间:2018-08-11 16:39:14

标签: python recursion

我被告知a += b等于a = a + b。我收到了一篇论文:https://www.python.org/doc/essays/graphs/。根据我使用path = path + [start]path += [start]的不同,结果也有所不同。在第一种情况下,我得到了预期的结果[['a', 'b', 'c', 'd'], ['a', 'b', 'd'], ['a', 'c', 'd']]。但是在另一种情况下,输出则不同:[['a', 'b', 'c', 'd']]。为什么? 这是代码:

graph = {}
graph['a'] = ['b', 'c']
graph['b'] = ['c', 'd']
graph['c'] = ['d']
graph['d'] = ['c']
graph['e'] = ['f']
graph['f'] = ['c']

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

    path = path + [start]   # result is [['a', 'b', 'c', 'd'], ['a', 'b', 'd'], ['a', 'c', 'd']]
    # path += [start]       # result is [['a', 'b', 'c', 'd']]

    if start == end:
        return [path]

    if not start in graph:       # I'm using python3
    # if not graph.has_key(start):
        return []

    paths = []

    for node in graph[start]:

        if node not in path:

            newpaths = find_all_paths(graph, node, end, path)

            for newpath in newpaths:
                paths.append(newpath)

    return paths

print(find_all_paths(graph, 'a', 'd'))

我试图检查以下内容:

a = 4
b = 5
a += b  # 9

c = 4
d = 5
c = c + d # 9

print(a == c) # True

a_1 = [4]
b_1 = [5]
a_1 += b_1 # [4, 5]

a_2 = [4]
b_2 = [5]
a_2 = a_2 + b_2 # [4, 5]

print(a_1 == a_2) # True

如果您能告诉我此a += b的正确术语,我将非常感谢您的帮助。有这种情况的说明吗?

0 个答案:

没有答案