Given a list of lists, write a Python program to replace the values in the inner lists with their ordinal values.
Examples:
Input : [[1, 2, 3], [ 4, 5, 6], [ 7, 8, 9, 10]] Output : [[0, 0, 0], [1, 1, 1], [2, 2, 2, 2]] Input : [['a'], [ 'd', 'e', 'b', 't'], [ 'x', 'l']] Output : [[0], [1, 1, 1, 1], [2, 2]]
Approach #1 : Naive Approach This method is a one-liner Naive approach in which we make use of two for loops using i and j variable and iterate through each inner list to replace it with the ith ordinal number.
Python3
# Python3 program to Replace element # in a list with its ordinal number def replaceOrdinal(lst): return [[i for j in range ( len (lst[i]))] for i in range ( len (lst))] # Driver Code lst = [[ 1 , 2 , 3 ], [ 4 , 5 , 6 ], [ 7 , 8 , 9 , 10 ]] print (replaceOrdinal(lst)) |
[[0, 0, 0], [1, 1, 1], [2, 2, 2, 2]]
Time Complexity: O(n), where n is the length of the list t
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the list
Approach #2 : Pythonic Naive This is another naive approach, but more pythonic. For every inner list, it returns the ith position (which is its ordinal number) and then multiplies it with the length of that particular inner list in order to return the desired output.
Python3
# Python3 program to Replace element # in a list with its ordinal number def replaceOrdinal(lst): return [[i] * len (lst[i]) for i in range ( len (lst))] # Driver Code lst = [[ 1 , 2 , 3 ], [ 4 , 5 , 6 ], [ 7 , 8 , 9 , 10 ]] print (replaceOrdinal(lst)) |
[[0, 0, 0], [1, 1, 1], [2, 2, 2, 2]]
Approach #3 : Using Python enumerate() method We can use list comprehension along with Python enumerate(). This method adds a counter to an iterable and returns it in a form of enumerate object. This counter index will be used as the ordinal number. And thus, we return the respective counter index for every element of inner sublist.
Python3
# Python3 program to Replace element # in a list with its ordinal number def replaceOrdinal(lst): return [[idx for _ in sublist] for idx, sublist in enumerate (lst)] # Driver Code lst = [[ 1 , 2 , 3 ], [ 4 , 5 , 6 ], [ 7 , 8 , 9 , 10 ]] print (replaceOrdinal(lst)) |
[[0, 0, 0], [1, 1, 1], [2, 2, 2, 2]]
Approach #4 : Alternative to use enumerate() Here, we use Python enumerate() in a similar method as in above method, but instead of another loop, we follow the approach #4 to produce the inner list.
Python3
# Python3 program to Replace element # in a list with its ordinal number def replaceOrdinal(lst): return [[index] * len (sublist) for index, sublist in enumerate (lst)] # Driver Code lst = [[ 1 , 2 , 3 ], [ 4 , 5 , 6 ], [ 7 , 8 , 9 , 10 ]] print (replaceOrdinal(lst)) |
[[0, 0, 0], [1, 1, 1], [2, 2, 2, 2]]
Approach#5: In this approach, we will iterate over each element of the list and replace it with its corresponding index value. We will use a nested for loop to traverse the list.
Algorithm
1. Traverse the list using a nested for loop.
2. Replace each element of the list with its index value.
Python3
def replace_with_ordinal(lst): for i in range ( len (lst)): for j in range ( len (lst[i])): lst[i][j] = i return lst lst = [[ 1 , 2 , 3 ], [ 4 , 5 , 6 ], [ 7 , 8 , 9 , 10 ]] print (replace_with_ordinal(lst)) |
[[0, 0, 0], [1, 1, 1], [2, 2, 2, 2]]
Time Complexity: O(n^2)
Auxiliary Space: O(1)
METHOD 6:Using def and len.
APPROACH:
This algorithm is implemented in the replace_with_ordinal function, which returns a new list with each element replaced with its corresponding index in the input list.
ALGORITHM:
1. Define a function named `replace_with_ordinal` that takes an input list as argument.
2. Create an empty list called `result`.
3. Loop through each list `lst` in the input list using a `for` loop.
4. Create an empty list called `ordinal_list`.
5. Loop through each element `elem` in `lst` using a `for` loop.
6. Append the index of `lst` to `ordinal_list` for each element `elem`.
7. Append `ordinal_list` to `result`.
8. Return `result`.
Python3
def replace_with_ordinal(input_list): return [[i for _ in lst] for i, lst in enumerate (input_list)] input_list = [[ 1 , 2 , 3 ], [ 4 , 5 , 6 ], [ 7 , 8 , 9 , 10 ]] output_list = replace_with_ordinal(input_list) print (output_list) |
[[0, 0, 0], [1, 1, 1], [2, 2, 2, 2]]
Time complexity:
The time complexity of this implementation is O(n^2), where n is the number of elements in the input list.
Space complexity:
The space complexity of this implementation is also O(n^2), where n is the number of elements in the input list.
Approach#7: In this approach, we can use the map() function along with a lambda function to replace the elements with their ordinal numbers. The lambda function takes two arguments, index and the sublist, and returns the index value for each element of the sublist.
Algorithm:
Define a lambda function that takes two arguments, index and sublist, and returns the index value for each element of the sublist.
Use the map() function to apply the lambda function to each element of the list.
Python3
def replace_with_ordinal(lst): return list ( map ( lambda x: list ( map ( lambda y: x[ 0 ], x[ 1 ])), enumerate (lst))) lst = [[ 1 , 2 , 3 ], [ 4 , 5 , 6 ], [ 7 , 8 , 9 , 10 ]] print (replace_with_ordinal(lst)) |
[[0, 0, 0], [1, 1, 1], [2, 2, 2, 2]]
Time Complexity: O(n^2) (due to the nested maps)
Auxiliary Space: O(n) (due to the creation of a new list)