Given a list of lists, the task is to find the frequency of a character at every position of sub-list in list of lists.
Input : lst = [['X', 'Y', 'X'], ['Z', 'Y', 'X'], ['Y', 'Y', 'Y'], ['Z', 'Z', 'X'], ['Y', 'Z', 'X']], character = 'X' Output: [0.2, 0.0, 0.8]
Explanation: We have 3 elements in each sublist, we have to find position of ‘X’ at position 0, 1 and 2. For Position 0 in all sublist we have – ‘x’ in first sub list at zero position, ‘z’ in second sub list at zero position, ‘y’ in third sub list at zero position, ‘z’ in fourth sub list at zero position and ‘y’ in fifth sub list at zero position. So, we have 1 occurrence of ‘x’ at position 1 in all sub list so, Occurrence = 1/5 = .2 For Position 1 we don’t have any occurrence of ‘x’ in sub list so, Occurrence = 0/5 = 0. For Position 2 we have 4 occurrence of ‘x’ in sub list so, Occurrence = 4/5 = 0.8 Let’s discuss certain ways in which this can be performed.
Method #1 : Using Iteration
Python3
# Python code to find frequency of a character # at every position of list in list of lists. # Input list initialization Input = [[ 'X' , 'Y' , 'X' ], [ 'Z' , 'Y' , 'X' ], [ 'Y' , 'Y' , 'Y' ], [ 'Z' , 'Z' , 'X' ], [ 'Y' , 'Z' , 'X' ]] Output = [] # Character Initialization character = 'X' # Output list initialization for elem in range ( len ( Input [ 0 ])): Output.append( 0 ) # Using iteration for elem in Input : for x, y in enumerate (elem): if y = = character: Output[x] + = 1 for x, y in enumerate (Output): Output[x] = y / len ( Input ) # Printing print ( "Initial list of list is :" , Input ) print ( "Occurrence of 'X' in list is" , Output) |
Initial list of list is : [['X', 'Y', 'X'], ['Z', 'Y', 'X'], ['Y', 'Y', 'Y'], ['Z', 'Z', 'X'], ['Y', 'Z', 'X']] Occurrence of 'X' in list is [0.2, 0.0, 0.8]
Time complexity: O(N*M), where N is the number of lists in the Input list and M is the maximum length of any list in the Input list.
Auxiliary space: O(M), where M is the maximum length of any list in the Input list.
Method #2 : Using zip
Python3
# Python code to find frequency of a character # at every position of list in list of lists. # Input list initialization Input = [[ 'X' , 'Y' , 'X' ], [ 'Z' , 'Y' , 'X' ], [ 'Y' , 'Y' , 'Y' ], [ 'Z' , 'Z' , 'X' ], [ 'Y' , 'Z' , 'X' ]] Output = [] # Character initialization character = 'X' # Using zip Output = [elem.count(character) / len (elem) for elem in zip ( * Input )] # Printing print ( "Initial list of list is :" , Input ) print ( "Occurrence of 'X' in list is" , Output) |
Initial list of list is : [['X', 'Y', 'X'], ['Z', 'Y', 'X'], ['Y', 'Y', 'Y'], ['Z', 'Z', 'X'], ['Y', 'Z', 'X']] Occurrence of 'X' in list is [0.2, 0.0, 0.8]
The time complexity of this code is O(m*n), where m is the number of sublists in the input list and n is the length of the longest sublist.
The space complexity is O(n), where n is the length of the longest sublist.
Method #3: Using Pandas
Python3
# Python code to find frequency of a character # at every position of list in list of lists. import pandas as pd # Input list initialization Input = [[ 'X' , 'Y' , 'X' ], [ 'Z' , 'Y' , 'X' ], [ 'Y' , 'Y' , 'Y' ], [ 'Z' , 'Z' , 'X' ], [ 'Y' , 'Z' , 'X' ]] # Defining character character = 'X' # using pandas Output = pd.DataFrame( Input ) Output = Output.where(Output = = character, 0 ).where(Output ! = character, 1 ) # Printing print ( "Initial list of list is :" , Input ) print ( "Occurrence of 'X' in list is\n" , Output.mean()) |
Initial list of list is : [[‘X’, ‘Y’, ‘X’], [‘Z’, ‘Y’, ‘X’], [‘Y’, ‘Y’, ‘Y’], [‘Z’, ‘Z’, ‘X’], [‘Y’, ‘Z’, ‘X’]] Occurrence of ‘X’ in list is 0 0.2 1 0.0 2 0.8 dtype: float64
Method #4 : Using operator.countOf()
Python3
# Python code to find frequency of a character # at every position of list in list of lists. import operator as op # Input list initialization Input = [[ 'X' , 'Y' , 'X' ], [ 'Z' , 'Y' , 'X' ], [ 'Y' , 'Y' , 'Y' ], [ 'Z' , 'Z' , 'X' ], [ 'Y' , 'Z' , 'X' ]] Output = [] # Character initialization character = 'X' # Using zip Output = [op.countOf(elem,character) / len (elem) for elem in zip ( * Input )] # Printing print ( "Initial list of list is :" , Input ) print ( "Occurrence of 'X' in list is" , Output) |
Initial list of list is : [['X', 'Y', 'X'], ['Z', 'Y', 'X'], ['Y', 'Y', 'Y'], ['Z', 'Z', 'X'], ['Y', 'Z', 'X']] Occurrence of 'X' in list is [0.2, 0.0, 0.8]
Time Complexity: O(N*N)
Auxiliary Space: O(N*N)
Method #5: Using List Comprehension
By using list comprehension we can iterate through the rows of the input list and count the occurrences of the character ‘X’ in each row. Then, we can divide this count by the length of the row to get the frequency of the character at each position.
Python3
# Python code to find frequency of a character # at every position of list in list of lists. # Input list initialization Input = [[ 'X' , 'Y' , 'X' ], [ 'Z' , 'Y' , 'X' ], [ 'Y' , 'Y' , 'Y' ], [ 'Z' , 'Z' , 'X' ], [ 'Y' , 'Z' , 'X' ]] # Character initialization character = 'X' # Using List Comprehension Output = [ sum (row[i] = = character for row in Input ) / len ( Input ) for i in range ( len ( Input [ 0 ]))] # Printing print ( "Initial list of list is :" , Input ) print ( "Occurrence of 'X' in list is" , Output) |
Initial list of list is : [['X', 'Y', 'X'], ['Z', 'Y', 'X'], ['Y', 'Y', 'Y'], ['Z', 'Z', 'X'], ['Y', 'Z', 'X']] Occurrence of 'X' in list is [0.2, 0.0, 0.8]
The time complexity of this code is O(nm), where n is the number of rows in the input list and m is the length of each row.
The space complexity of this code is O(m), where m is the length of each row in the input list.