Python | N element incremental tuples
Last Updated :
17 Apr, 2023
Sometimes, while working with data, we can have a problem in which we require to gather a data that is of the form of sequence of increasing element tuple with each tuple containing the element N times. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using generator expression + tuple() The combination of above functions can be used to perform this task. In this, we need to iterate through N using generator expression and construction of tuple using tuple().
Python3
N = 3
print ( "Number of times to repeat : " + str (N))
res = tuple ((ele, ) * N for ele in range ( 1 , 6 ))
print ( "Tuple sequence : " + str (res))
|
Output :
Number of times to repeat : 3
Tuple sequence : ((1, 1, 1), (2, 2, 2), (3, 3, 3), (4, 4, 4), (5, 5, 5))
Method #2 : Using repeat() + list comprehension This task can also be performed using combination of above functions. In this, we use repeat() to repeat elements N times. And iteration is handled using list comprehension.
Python3
from itertools import repeat
N = 3
print ( "Number of times to repeat : " + str (N))
res = tuple ( tuple (repeat(ele, N)) for ele in range ( 1 , 6 ))
print ( "Tuple sequence : " + str (res))
|
Output :
Number of times to repeat : 3
Tuple sequence : ((1, 1, 1), (2, 2, 2), (3, 3, 3), (4, 4, 4), (5, 5, 5))
Method #3 : Using for loop, while loop and tuple() method
Python3
N = 3
print ( "Number of times to repeat : " + str (N))
res = []
for i in range ( 1 , 6 ):
x = []
j = 0
while (j<N):
x.append(i)
j + = 1
res.append( tuple (x))
res = tuple (res)
print ( "Tuple sequence : " + str (res))
|
Output
Number of times to repeat : 3
Tuple sequence : ((1, 1, 1), (2, 2, 2), (3, 3, 3), (4, 4, 4), (5, 5, 5))
Method #4 : Using * operator and tuple() method
Python3
N = 3
print ( "Number of times to repeat : " + str (N))
res = []
for i in range ( 1 , 6 ):
a = [i] * N
res.append( tuple (a))
res = tuple (res)
print ( "Tuple sequence : " + str (res))
|
Output
Number of times to repeat : 3
Tuple sequence : ((1, 1, 1), (2, 2, 2), (3, 3, 3), (4, 4, 4), (5, 5, 5))
Time complexity: O(n) where n is the number of elements in the range from 1 to 6, as the loop iterates n times and each iteration takes constant time.
Auxiliary space: O(n) to store the list of tuples where n is the number of tuples in the list.
Method 5: Using numpy library
Note: Install numpy module using command “pip install numpy”
Python3
import numpy as np
N = 3
print ( "Number of times to repeat : " + str (N))
res = [ tuple (np.full(N, i)) for i in range ( 1 , 6 )]
print ( "Tuple sequence : " + str (res))
|
Output:
Number of times to repeat : 3
Tuple sequence : [(1, 1, 1), (2, 2, 2), (3, 3, 3), (4, 4, 4), (5, 5, 5)]
Time Complexity: O(n^2), where n is the number of elements in the result.
Auxiliary Space: O(n^2)
Method 6: using itertools module
Step-by-step algorithm for implementing the approach
- Initialize the variable N to the given value.
- Define the range of numbers to choose from, using the range() function.
- Use the product() function from the itertools library to generate all possible tuples of length N whose elements are chosen from the numbers iterable.
- Filter the resulting list of tuples to only those where all elements are the same, using a list comprehension and the all() function.
- Print the value of N to the console.
- Print the resulting list of tuples to the console.
- Return the list of tuples.
Python3
from itertools import product
N = 3
numbers = range ( 1 , 6 )
res = list (product(numbers, repeat = N))
res = [t for t in res if all (x = = t[ 0 ] for x in t)]
print ( "Number of times to repeat : " + str (N))
print ( "Tuple sequence : " + str (res))
|
Output
Number of times to repeat : 3
Tuple sequence : [(1, 1, 1), (2, 2, 2), (3, 3, 3), (4, 4, 4), (5, 5, 5)]
Time complexity:
- The product() function generates all possible tuples of length N whose elements are chosen from the numbers iterable. The number of possible tuples is len(numbers)^N.
- The filter operation uses a list comprehension and the all() function to filter the tuples where all elements are the same. This operation takes O(N) time for each tuple.
- Therefore, the overall time complexity of the algorithm is O(N*len(numbers)^N).
Auxiliary space:
- The product() function uses O(N*len(numbers)) auxiliary space to generate all possible tuples.
- The list comprehension used to filter the tuples uses O(N) auxiliary space for each tuple.
- Therefore, the overall auxiliary space complexity of the algorithm is O(N*len(numbers)).
Share your thoughts in the comments
Please Login to comment...