yez*_*zi3 53 python list python-3.x
说,如果你有多个输入8Python和你想产生连续的号码清单高达8喜欢
[0, 1, 2, 3, 4, 5, 6, 7, 8]
Run Code Online (Sandbox Code Playgroud)
你怎么能这样做的?
the*_*eye 94
在Python 3中,您可以使用这样的内置range函数
>>> list(range(9))
[0, 1, 2, 3, 4, 5, 6, 7, 8]
Run Code Online (Sandbox Code Playgroud)
注1: Python 3.x的range函数,返回一个range对象.如果你想要一个列表,你需要将它显式转换为一个列表,其list功能就像我在答案中所示.
注2:我们将数字9传递给range函数,因为range函数将生成数字,直到给定的数字但不包括数字.所以,我们给出实际数字+ 1.
注3:range Python 2和3 中的功能存在细微差别.您可以在此答案中阅读更多相关信息.
小智 8
使用Python的内置范围函数:
Python 2
input = 8
output = range(input + 1)
print output
[0, 1, 2, 3, 4, 5, 6, 7, 8]
Run Code Online (Sandbox Code Playgroud)
Python 3
input = 8
output = list(range(input + 1))
print(output)
[0, 1, 2, 3, 4, 5, 6, 7, 8]
Run Code Online (Sandbox Code Playgroud)
以下是使用 numpy 生成从 0 到 100 的 n 个连续等间隔数字的方法:
import numpy as np
myList = np.linspace(0, 100, n)
Run Code Online (Sandbox Code Playgroud)