Friday, December 27, 2024
Google search engine
HomeLanguagesPython | Decimal step range in list

Python | Decimal step range in list

Sometimes, while working with a Python list we can have a problem in which we require to populate the list in a range of decimals. Integral ranges are easier to handle using inbuilt constructs, but having a decimal value to provide to range value doesn’t work. Let’s discuss a way in which this problem can be solved.

Method 1: Using list comprehension

One way to solve this problem is by using list comprehension which can solve this problem in an easier way by iterating the list and multiplying the range element to the index of the element.

Python3




# initializing start value
strt = 5
 
# initializing step value
step = 0.4
 
# using list comprehension
# Decimal step range
test_list = [strt + (x * step)
             for x in range(0, 5)]
 
# Printing result
print("The list after decimal range\
                value initialization : " + str(test_list))


Output:

The list after decimal range value initialization : [5.0, 5.4, 5.8, 6.2, 6.6]

Time Complexity: O(n), where n is the length of the list test_list 
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the test_list list 

Method 2: Using Numpy.linspace

The numpy.linspace() function returns number spaces evenly w.r.t interval. Similar to numpy.arange() function but instead of step it uses sample number.

Python3




import numpy as geek
 
# restep set to True
print("Decimal range value",
      geek.linspace(2.0, 3.0, num=5))


Output:

Decimal range value [2.   2.25 2.5  2.75 3.  ]

Time Complexity: O(n), where n is the length of the list test_list 
Auxiliary Space: O(1) additional constant space is required

Method 3: Using Numpy.arrange

The np.arange([start,] stop[, step,][, dtype]), Returns an array with evenly spaced elements as per the interval. The interval mentioned is half-opened i.e. [Start, Stop) 

Python3




import numpy as np
 
# Printing all numbers from 1 to
# 2 in steps of 0.1
print(np.arange(1, 2, 0.1))


Output:

[1.  1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9]

Method 4:  Using itertools: 

The python library itertools provides a function called “count” which can be used to create a sequence of numbers starting from the value “start” and incrementing by the value “step”. Then it uses the itertools.islice() function to take the first “length” number of elements from that sequence and convert it into a list. The resulting list will be a list of decimal numbers with a specific step. The time and space complexity of this approach would be O(n) for both, where n is the number of elements in the list.

Python3




import itertools
 
# initializing start value
start = 5.0
 
# initializing step value
step = 0.4
 
# initializing length
length = 5
 
# Using itertools to create a list of decimal numbers with a specific step
test_list = list(itertools.islice(itertools.count(start, step), length))
 
# Printing result
print("The list after decimal range value initialization : " + str(test_list))
#This code is contributed by Edula Vinay Kumar Reddy


Output

The list after decimal range value initialization : [5.0, 5.4, 5.800000000000001, 6.200000000000001, 6.600000000000001]

Time complexity: O(n) where n is the length of the final list. The itertools.count() function generates an infinite iterator of numbers starting from the given start value and incrementing by the given step value. The itertools.islice() function is used to slice this iterator and limit the number of items returned to the specified length. These operations are performed in a single pass over the iterator, resulting in a time complexity of O(n) where n is the length of the final list.

Auxiliary Space: O(n) where n is the length of the final list. A new list is created and populated with the n elements generated by the iterator. The space complexity is O(n) as a list of n elements is created.

RELATED ARTICLES

Most Popular

Recent Comments