有谁知道解决此列表问题方案的简便方法?

时间:2020-04-02 23:42:48

标签: python list

1 个答案:

答案 0 :(得分:0)

问题只是计算斐波那契数列的三个元素的另一个选择。

您的备用代码

def append_sum(lst):
  for i in range(3):        # Loop 3 times
    lst += [sum(lst[-2:])]  # add sum of last two elements to list
                            # lst[-2:] is last two elements of list
                            # sum(...) is the sum
                            # x += y appends y to x when x and y are lists
  return lst

print(append_sum([1, 1, 2]))

输出

[1, 1, 2, 3, 5, 8]