Python 如何生成连续数字列表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29558007/
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-19 04:43:18  来源:igfitidea点击:

How can I generate a list of consecutive numbers?

pythonlistpython-3.x

提问by yezi3

Say if you had a number input 8in python and you wanted to generate a list of consecutive numbers up to 8like

说,如果你有多个输入8Python和你想生成连续的数字高达名单8

[0, 1, 2, 3, 4, 5, 6, 7, 8]

How could you do this?

你怎么能这样做?

采纳答案by thefourtheye

In Python 3, you can use the builtin rangefunction like this

在 Python 3 中,您可以range像这样使用内置函数

>>> list(range(9))
[0, 1, 2, 3, 4, 5, 6, 7, 8]

Note 1:Python 3.x's rangefunction, returns a rangeobject. If you want a list you need to explicitly convert that to a list, with the listfunction like I have shown in the answer.

注 1:Python 3.x 的range函数,返回一个range对象。如果您想要一个列表,则需要使用list我在答案中显示的功能将其显式转换为列表。

Note 2:We pass number 9 to rangefunction because, rangefunction will generate numbers till the given number but not including the number. So, we give the actual number + 1.

注意 2:我们将数字 9 传递给range函数,因为range函数会生成数字直到给定的数字但不包括数字。所以,我们给出实际数字+1。

Note 3:There is a small difference in functionality of rangein Python 2 and 3. You can read more about that in this answer.

注 3:rangePython 2 和 3中的功能略有不同。您可以在此答案中阅读更多相关信息。

回答by ds_chem

Using Python's built in range function:

使用 Python 的内置 range 函数:

Python 2

蟒蛇 2

input = 8
output = range(input + 1)

print output
[0, 1, 2, 3, 4, 5, 6, 7, 8]

Python 3

蟒蛇 3

input = 8
output = list(range(input + 1))

print(output)
[0, 1, 2, 3, 4, 5, 6, 7, 8]

回答by jester112358

Just to give you another example, although range(value) is by far the best way to do this, this might help you later on something else.

再举一个例子,虽然 range(value) 是迄今为止最好的方法,但这可能对你以后的其他事情有所帮助。

list = []
calc = 0

while int(calc) < 9:
    list.append(calc)
    calc = int(calc) + 1

print list
[0, 1, 2, 3, 4, 5, 6, 7, 8]

回答by CannedScientist

Depending on how you want the result, you can also print each number in a for loop:

根据您想要的结果,您还可以在 for 循环中打印每个数字:

def numbers():
    for i in range(int(input('How far do you wanna go? '))+1):
        print(i)

So if the user input was 7 for example:

因此,如果用户输入为 7,例如:

How far do you wanna go? 7
0
1
2
3
4
5
6
7

You can also delete the '+1' in the for loop and place it on the print statement, which will change it to starting at 1 instead of 0.

您还可以删除 for 循环中的“+1”并将其放在打印语句中,这会将其更改为从 1 开始而不是从 0 开始。

回答by Diyar T Alzuhairi

Note :-Certainly in python-3x you need to use Range functionIt works to generate numbers on demand, standard method to use Range functionto make a list of consecutive numbers is

注意:-当然在python-3x中你需要使用Range函数它可以按需生成数字,使用Range函数制作连续数字列表的标准方法是

x=list(range(10))
#"list"_will_make_all_numbers_generated_by_range_in_a_list
#number_in_range_(10)_is_an_option_you_can_change_as_you_want
print (x)
#Output_is_ [0,1,2,3,4,5,6,7,8,9]

Also if you want to make an functionto generate a list of consecutive numbers by using Range functionwatch this code !

此外,如果你想使一个功能,通过使用产生连续的号码清单范围功能看这个代码!

def  consecutive_numbers(n) :
    list=[i for i in range(n)]
    return (list)
print(consecutive_numbers(10))

Good Luck!

祝你好运!