Home  >  Article  >  Backend Development  >  How to create a list of numbers in a specified range in Python? (code example)

How to create a list of numbers in a specified range in Python? (code example)

青灯夜游
青灯夜游Original
2019-03-20 15:50:0013499browse

In Python, given two numbers r1 and r2 (defining range, maximum and minimum values), how to create a list of numbers with a given range? The following article will show you how to create a list of numbers in a specified range. I hope it will be helpful to you.

How to create a list of numbers in a specified range in Python? (code example)

Method 1: Use a for loop

A way to create a list of numbers within a given range The simple way is: first create an empty list and append the subsequent items of each integer on each iteration of the for loop.

Example:

def createList(r1, r2): 
  
    # 判断范围R1和R2是否相等
    if (r1 == r2): 
        return r1 
  
    else: 
  
        # 创建空列表 
        res = [] 
  
        # 循环以将后续任务追加到列表,直到到达范围r2
        while(r1 < r2+1 ): 
              
            res.append(r1) 
            r1 += 1
        return res 
      
r1, r2 = -1, 1
print(createList(r1, r2))

Output:

[-1, 0, 1]
[-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6]

Method 2: Use list comprehension

We can also use list comprehension to achieve our goal. Just iterate "item" from r1 to r2 in a for loop and return all "item" as a list.

Example:

def createList(r1, r2): 
    return [item for item in range(r1, r2+1)] 
r1, r2 = -2, 4
print(createList(r1, r2))
r1, r2 = -4,6
print(createList(r1, r2))

Output:

[-2, -1, 0, 1, 2, 3, 4]
[-4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6]

Method 3: Using Pythonrange()

python There is a range() function in which creates a sequence of numbers from start to end and outputs each item in the sequence. We use range() with r1 and r2 and then convert the sequence to a list.

Example:

def createList(r1, r2): 
    return list(range(r1, r2+1)) 
r1, r2 = -2, 4
print(createList(r1, r2))
r1, r2 = -4,6
print(createList(r1, r2))

Output:

[-2, -1, 0, 1, 2, 3, 4]
[-4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6]

Method 4: Use numpy.arange()

python numpy.arange() will return a list with evenly spaced elements according to the interval. Here we set the interval to 1 as required to get the desired output.

Example:

import numpy as np 
def createList(r1, r2): 
    return np.arange(r1, r2+1, 1) 
r1, r2 = -2, 3
print(createList(r1, r2))

Output:

[-2 -1  0  1  2  3]

Related video tutorial recommendation: "Python Tutorial"

The above is this article The entire content, I hope it will be helpful to everyone's study. For more exciting content, you can pay attention to the relevant tutorial columns of the PHP Chinese website! ! !

The above is the detailed content of How to create a list of numbers in a specified range in Python? (code example). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn