Saturday, November 16, 2024
Google search engine
HomeLanguagesPython | Exponentiate Kth Record Index

Python | Exponentiate Kth Record Index

Many times, while working with records, we can have a problem in which we need to change the value of tuple elements. This is a common problem while working with tuples. Let’s discuss certain ways in which N can be exponentiated to Kth element of tuple in list. 

Method #1 : Using loop Using loops this task can be performed. In this, we just iterate the list to change the Kth element by predefined value N in code. 

Python3




# Python3 code to demonstrate working of
# Exponentiate Kth Record Index
# Using loop
 
# Initializing list
test_list = [(4, 5, 6), (7, 4, 2), (9, 10, 11)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Initializing N
N = 3
 
# Initializing K
K = 1
 
# Exponentiate Kth Record Index
# Using loop
res = []
for i in range(0, len(test_list)):
    res.append((test_list[i][0], test_list[i][K] ** N, test_list[i][2]))
 
# printing result
print("The tuple after Exponentiating N to Kth element : " + str(res))


Output

The original list is : [(4, 5, 6), (7, 4, 2), (9, 10, 11)]
The tuple after Exponentiating N to Kth element : [(4, 125, 6), (7, 64, 2), (9, 1000, 11)]

Time Complexity: O(n*n) where n is the number of elements in the string list. The loop is used to perform the task and it takes O(n*n) time.
Auxiliary Space: O(n*n) additional space of size n is created where n is the number of elements in the string list.

  Method #2 : Using list comprehension This method is having the same approach as the above method, just reduces lines of code using list comprehension functionality to make code compact by size. 

Python3




# Python3 code to demonstrate working of
# Exponentiate Kth Record Index
# Using list comprehension
 
# Initializing list
test_list = [(4, 5, 6), (7, 4, 2), (9, 10, 11)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Initializing N
N = 3
 
# Initializing K
K = 1
 
# Exponentiate Kth Record Index
# Using list comprehension
res = [(a, b ** N, c) for a, b, c in test_list]
 
# printing result
print("The tuple after Exponentiating N to Kth element : " + str(res))


Output : 

The original list is : [(4, 5, 6), (7, 4, 2), (9, 10, 11)]
The tuple after Exponentiating N to Kth element : [(4, 125, 6), (7, 64, 2), (9, 1000, 11)]

Time Complexity: O(n), where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n), where n is the number of elements in the list “test_list”.

Method #3 : Using numpy

In this approach, we are using numpy library to perform the exponentiation on the Kth element of each tuple in the list. We first convert the original list of tuples into a numpy array, then we use the res[:,K] = res[:,K] ** N to perform the exponentiation on the Kth element of each tuple. The time complexity of this approach is O(n) and the space complexity is O(n).

Python3




import numpy as np
 
# Initializing list
test_list = [(4, 5, 6), (7, 4, 2), (9, 10, 11)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Initializing N
N = 3
 
# Initializing K
K = 1
 
# Exponentiate Kth Record Index using numpy
res = np.array(test_list)
res[:,K] = res[:,K] ** N
 
# printing result
print("The tuple after Exponentiating N to Kth element : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy


Output:

The original list is : [(4, 5, 6), (7, 4, 2), (9, 10, 11)]
The tuple after Exponentiating N to Kth element : [[   4  125    6]
[   7   64    2]
[   9 1000   11]]

Method #4 : Using math.pow() and tuple() methods

Approach

  1. Initiate a for loop to traverse list of tuples
  2. For each tuple change the middle element by raising it to the power of N
  3. Append the elements as it is to a list and convert it to a tuple
  4. Append tuple to output list
  5. Display output list

Python3




# Python3 code to demonstrate working of
# Exponentiate Kth Record Index
# Using loop
 
# Initializing list
test_list = [(4, 5, 6), (7, 4, 2), (9, 10, 11)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Initializing N
N = 3
 
# Initializing K
K = 1
import math
# Exponentiate Kth Record Index
# Using loop
res = []
for i in range(0, len(test_list)):
    x=[]
    a=math.pow(test_list[i][1],N)
    x.append(test_list[i][0])
    x.append(int(a))
    x.append(test_list[i][2])
    res.append(tuple(x))
     
# printing result
print("The tuple after Exponentiating N to Kth element : " + str(res))


Output

The original list is : [(4, 5, 6), (7, 4, 2), (9, 10, 11)]
The tuple after Exponentiating N to Kth element : [(4, 125, 6), (7, 64, 2), (9, 1000, 11)]

Time Complexity : O(M*N) M – length of tuples list N – length of each tuple

Auxiliary Space : O(M*N) M – length of tuples list N – length of each tuple

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments