Python - 使用2个值之间的数字创建列表?

lor*_*rde 304 python list

如何创建一个列表,其中包含我输入的两个值之间的值?例如,为11到16的值生成以下列表:

list = [11, 12, 13, 14, 15, 16]
Run Code Online (Sandbox Code Playgroud)

Jar*_*red 544

使用range.在Python 2.x中它返回一个列表,所以你需要的只是:

>>> range(11, 17)
[11, 12, 13, 14, 15, 16]
Run Code Online (Sandbox Code Playgroud)

在Python中,3.x range是一个迭代器.因此,您需要将其转换为列表:

>>> list(range(11, 17))
[11, 12, 13, 14, 15, 16]
Run Code Online (Sandbox Code Playgroud)

注意:第二个数字是独占的.所以,这里需要16+1=17

编辑:

要回答关于递增的问题0.5,最简单的选择可能是使用arange()'s .tolist(),

>>> import numpy as np
>>> np.arange(11, 17, 0.5).tolist()

[11.0, 11.5, 12.0, 12.5, 13.0, 13.5,
 14.0, 14.5, 15.0, 15.5, 16.0, 16.5]
Run Code Online (Sandbox Code Playgroud)

  • 惊人的!正是我要找的!有没有办法增加更小的值,比如 0.5 而不是 1?所以 [11.0, 11.5, 12.0 ...... 16.0] (2认同)
  • @lorde你可以使用第三个`step`参数增加1以上,但这仍然是一个int - 而不是float.你不能完全在标准的lib中做到这一点. (2认同)
  • 很好地讲述Python 2.x和3.x. (2认同)

dev*_*ull 18

你似乎在寻找range():

>>> x1=11
>>> x2=16
>>> range(x1, x2+1)
[11, 12, 13, 14, 15, 16]
>>> list1 = range(x1, x2+1)
>>> list1
[11, 12, 13, 14, 15, 16]
Run Code Online (Sandbox Code Playgroud)

用于递增0.5而不是1,例如:

>>> list2 = [x*0.5 for x in range(2*x1, 2*x2+1)]
>>> list2
[11.0, 11.5, 12.0, 12.5, 13.0, 13.5, 14.0, 14.5, 15.0, 15.5, 16.0]
Run Code Online (Sandbox Code Playgroud)

  • 在python`list`是一个内置类,因此有点不鼓励它作为变量使用 (2认同)

Bha*_*lli 6

在 python 中使用列表理解。由于您也想要列表中的 16.. 使用 x2+1。范围函数不包括函数中的上限。

list=[x for x in range(x1,x2+1)]

  • 如果您使用`range()` 则无需使用列表推导式 (2认同)

v2b*_*v2b 6

假设您想要 x 到 y 之间的范围

range(x,y+1)

>>> range(11,17)
[11, 12, 13, 14, 15, 16]
>>>
Run Code Online (Sandbox Code Playgroud)

使用 3.x 支持列表


Mik*_*sky 5

尝试:

range(x1,x2+1)  
Run Code Online (Sandbox Code Playgroud)

这是Python 2.x中的列表,其行为大多类似于Python 3.x中的列表.如果您正在运行Python 3并且需要一个可以修改的列表,那么使用:

list(range(x1,x2+1))
Run Code Online (Sandbox Code Playgroud)


Raj*_*ana 5

如果您正在寻找适用于浮点类型的范围之类的函数,那么这是一篇非常不错的文章

def frange(start, stop, step=1.0):
    ''' "range()" like function which accept float type''' 
    i = start
    while i < stop:
        yield i
        i += step
# Generate one element at a time.
# Preferred when you don't need all generated elements at the same time. 
# This will save memory.
for i in frange(1.0, 2.0, 0.5):
    print i   # Use generated element.
# Generate all elements at once.
# Preferred when generated list ought to be small.
print list(frange(1.0, 10.0, 0.5))    
Run Code Online (Sandbox Code Playgroud)

输出:

1.0
1.5
[1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0, 5.5, 6.0, 6.5, 7.0, 7.5, 8.0, 8.5, 9.0, 9.5]
Run Code Online (Sandbox Code Playgroud)


小智 5

在python中你可以很容易地做到这一点

start=0
end=10
arr=list(range(start,end+1))
output: arr=[0,1,2,3,4,5,6,7,8,9,10]
Run Code Online (Sandbox Code Playgroud)

或者您可以创建一个递归函数,该函数返回一个数组,最多给定数字:

ar=[]
def diff(start,end):
    if start==end:
        d.append(end)
        return ar
    else:
        ar.append(end)
        return diff(start-1,end) 
Run Code Online (Sandbox Code Playgroud)

输出:ar=[10,9,8,7,6,5,4,3,2,1,0]


Jos*_*ose 5

我来到这里是因为我想使用列表理解以 0.1 为增量创建 -10 到 10 之间的范围。我没有像上面的大多数答案那样执行过于复杂的功能,而是这样做了

simple_range = [ x*0.1 for x in range(-100, 100) ]
Run Code Online (Sandbox Code Playgroud)

通过将范围计数更改为 100,我现在可以使用标准范围函数获得 -10 到 10 的范围。因此,如果您需要 0.2,那么只需执行 range(-200, 200) 等等