简体   繁体   English

从 Python 中的数字序列创建列表

[英]Create a list from a sequence of numbers in Python

I have written a code that returns the first 100 Fibonacci-numbers.我编写了一个返回前 100 个斐波那契数的代码。 Now, I want to make a list that contains all of those numbers, in square brackets and commas between all numbers, like a list.现在,我想创建一个包含所有这些数字的列表,在方括号和所有数字之间的逗号中,就像一个列表。 Obviously i can't just copy the returned numbers and paste them in a list with square brackets, since that won't give me any commas between the numbers.显然我不能只复制返回的数字并将它们粘贴到带有方括号的列表中,因为这不会给我数字之间的任何逗号。

What can I do?我能做些什么?

Just append those numbers to a list the loop like my_list.append(<your fibonacci numbers>)只需 append 将这些数字列在循环列表中,例如my_list.append(<your fibonacci numbers>)

How does your function return the numbers?您的 function 如何返回数字?

If it takes an argument so that fib(n) returns the nth fibonacci number, you can use a comprehension to generate a list directly:如果它接受一个参数以便fib(n)返回第 n 个斐波那契数,您可以使用推导式直接生成一个列表:

fiblist = [fib(n) for n in range(100)]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM