在 PYTHON 中使用列表的斐波那契数列?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21857619/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:45:38  来源:igfitidea点击:

Fibonacci sequence using list in PYTHON?

pythonlistfibonacci

提问by Mark Rollin Borja

I have a problem about making a fibonacci sequence to a list, I'm just new to python someone help me please.

我有一个关于将斐波那契数列添加到列表中的问题,我是 python 的新手,请帮助我。

This is my code. I know this is looking wrong or something because it says invalid syntax. I don't know what to do about this really :(

这是我的代码。我知道这看起来不对,因为它说语法无效。我真的不知道该怎么办:(

This code works for a normal code without using a list!

此代码适用于不使用列表的普通代码!

myArray1 = [0] 
myArray2 = [1]

while myArray2 < 700:
    myArray1, myArray2 = b[i], myArray1+myArray2[i]
    print(myArray2)

采纳答案by LeartS

This code puts the first 700 fibonacci numbers in a list. Using meaningful variable names helps improve readability!

此代码将前 700 个斐波那契数列放入列表中。使用有意义的变量名有助于提高可读性!

fibonacci_numbers = [0, 1]
for i in range(2,700):
    fibonacci_numbers.append(fibonacci_numbers[i-1]+fibonacci_numbers[i-2])

Note: If you're using Python < 3, use xrangeinstead of range.

注意:如果您使用 Python < 3,请使用xrange代替range

回答by zhangxaochen

You may want this:

你可能想要这个:

In [77]: a = 0
    ...: b = 1
    ...: while b < 700:
    ...:     a, b = b, a+b
    ...:     print a, b
1 1
1 2
2 3
3 5
5 8
8 13
13 21
21 34
34 55
55 89
89 144
144 233
233 377
377 610
610 987

If you wanna store the results in a list, use list.append:

如果要将结果存储在列表中,请使用list.append

In [81]: a = 0
    ...: b = 1
    ...: fibo=[a, b]
    ...: while b < 70:
    ...:     a, b = b, a+b
    ...:     fibo.append(b)
    ...: print fibo
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]

回答by Eric

def fibonacci(n, results):
if n == 0:   
    return 0  
elif n == 1:  
    return 1  
else :  
    f = fibonacci(n-1, results) + fibonacci(n-2, results)
    f1 = f[:]
    results.appned(f1)


    return results

回答by Chris Kelley

There are two kinds of mistakes you are making; mistakes that are creating errors and mistakes that are affecting readability

你犯了两种错误;造成错误的错误和影响可读性的错误

Both instances of the phrase [i] should be removed. I believe that you may be thinking it has something to do with iteration or tuples, but that is part of the reason you are getting errors:

应该删除短语 [i] 的两个实例。我相信您可能认为它与迭代或元组有关,但这是您收到错误的部分原因:

myArray1 = [0] 
myArray2 = [1]

while myArray2 < 700:
    myArray1, myArray2 = b, myArray1+myArray2
    print(myArray2)

the other part of the reason you are getting errors is because of the variable b. You don't declare it and it does not belong. This code will iterate correctly if you switch out b with myArray2:

您收到错误的另一部分原因是变量 b。你不声明它,它不属于。如果您使用 myArray2 切换出 b,此代码将正确迭代:

myArray1 = [0] 
myArray2 = [1]

while myArray2 < 700:
    myArray1, myArray2 = myArray2, myArray1+myArray2
    print(myArray2)

then there are some legibility issues. I would change the phrase myArray1 and 2 to a and b respectively. First because it is just too long; second because in python it is called lists, not arrays; third because you are referring to integers, not lists or arrays:

然后还有一些易读性问题。我会将短语 myArray1 和 2 分别更改为 a 和 b。首先是因为它太长了;第二,因为在 python 中它被称为列表,而不是数组;第三,因为您指的是整数,而不是列表或数组:

a = [0] 
b = [1]

while b < 700:
    a, b = b, a+b
    print(b)

then, the variables that were myArray1 and 2, but are now a and b; those are integers and they do not need to be expressed as single object lists. so get rid of the brackets around them:

然后,变量是 myArray1 和 2,但现在是 a 和 b;这些是整数,它们不需要表示为单个对象列表。所以去掉它们周围的括号:

a = 0 
b = 1

while b < 700:
    a, b = b, a+b
    print(b)

Then, the last phrase in this code says print(b). If you have it printing b then the fibonacci sequence you get is missing its first 1. It will read (on separate lines of course) 1,2,3,5,8,13 and so on. It should read 1,1,2,3,5,8,13. You are missing the first 1. So print(b) needs to be changed to print(a):

然后,此代码中的最后一个短语是 print(b)。如果你让它打印 b 那么你得到的斐波那契数列缺少它的第一个 1。它会读取(当然在不同的行)1,2,3,5,8,13 等等。它应该是 1,1,2,3,5,8,13。您缺少第一个 1。因此需要将 print(b) 更改为 print(a):

a = 0 
b = 1

while b < 700:
    a, b = b, a+b
    print(a)

then, if you are expressing more than one variable you can just list all the variables separated by commas equal to all the values separated by commas like this:

然后,如果你要表达多个变量,你可以列出所有由逗号分隔的变量,等于所有由逗号分隔的值,如下所示:

a,b,c,d = 1,2,3,4

so for your code that would translate to:

所以对于您的代码将转换为:

a,b = 0,1

while b < 700:
    a, b = b, a+b
    print(a)

then get rid of that extra space, white space means something in python, though here it doesn't really make a difference:

然后摆脱那些额外的空间,空白在python中意味着一些东西,尽管在这里它并没有真正的区别:

a,b = 0,1
while b < 700:
    a, b = b, a+b
    print(a)

So all of this so far has just been enough to get you to your original problem: you are getting an iteration (each consecutive value on a seperate line). Below is how you can get a list to any number n:

所以到目前为止所有这些已经足以让你解决你的原始问题:你正在得到一个迭代(每个连续的值在单独的行上)。下面是如何获得任意数字 n 的列表:

def fibo(n):
    fibo_list = []
    a,b = 0,1
    while b < n:
        a,b = b,a+b
        fibo_list.append(a)
    print(fibo_list)

hope that helps

希望有帮助

回答by Ranjan Kumar

You can use below code, your problem can be solve in just one line. what are we doing is appending the

您可以使用以下代码,您的问题只需一行即可解决。我们正在做的是附加

fib_nums

fib_nums

till your given limit i.e.,

直到你给定的限制,即

700

700

and then adding the

然后添加

fib_nums

fib_nums

with the second list which is of zero length which explicitly we doing, because it is containing

使用第二个长度为零的列表,我们明确地这样做,因为它包含

None

没有任何

values, that we don't required.

值,我们不需要。

#defining variable
fib_nums = [0, 1] 

#just one line code
fib_nums = fib_nums + [fib_nums.append(fib_nums[i-1]+fib_nums[i-2]) for i in range(2,700)]*0

#printing the series
print (fib_nums)

回答by makkasi

It is not fast but it works as well:

它并不快,但也能正常工作:

def fibonacci(n):
# return a list of fibonacci numbers
if n == 0:
    fibonacci_list = []
elif n == 1:
    fibonacci_list = [0]
elif n == 2:
    fibonacci_list = [0, 1]
elif n > 2:
    fibonacci_list = [0, 1]
    for i in range(n-2):
        fibonacci_list += [0]
        fibonacci_list[-1] = fibonacci_list[-2] + fibonacci_list[-3]
return fibonacci_list