Python | Find frequency of given character at every position in list of lists
Last Updated :
13 Mar, 2023
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)
OutputInitial 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)
OutputInitial 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())
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 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)
OutputInitial 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)
OutputInitial 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.
Similar Reads
Python | Find sum of frequency of given elements in the list
Given two lists containing integers, the task is to find the sum of the frequency of elements of the first list in the second list. Example: Input: list1 = [1, 2, 3] list2 = [2, 1, 2, 1, 3, 5, 2, 3] Output: 7 Explanation: No of time 1 occurring in list2 is :2 No of time 2 occurring in list2 is :3 No
4 min read
Python - Find the frequency of numbers greater than each element in a list
Given a list, a new list is constructed that has frequency of elements greater than or equal to it, corresponding to each element of the list. Input : test_list = [6, 3, 7, 1, 2, 4] Output : [2, 4, 1, 6, 5, 3] Explanation : 6, 7 are greater or equal to 6 in list, hence 2. Input : test_list = [6, 3,
8 min read
Python | Find most frequent element in a list
Given a list, find the most frequent element in it. If multiple elements appear a maximum number of times, print any one of them using Python.ExampleMake a set of the list so that the duplicate elements are deleted. Then find the highest count of occurrences of each element in the set and thus, we f
2 min read
Python - Find frequency of given Datatype in tuple
Sometimes, while working with Python records, we can have a problem in which we need to extract count of any data type occurred in tuple. This can have application in various domains such as day-day programming and web development. Let's discuss certain ways in which this task can be performed. Inpu
6 min read
Python - Convert list of strings and characters to list of characters
Sometimes we come forward to the problem in which we receive a list that consists of strings and characters mixed and the task we need to perform is converting that mixed list to a list consisting entirely of characters. Using itertools.chain()itertools.chain() combines multiple lists or iterables i
3 min read
Python program to find the character position of Kth word from a list of strings
Given a list of strings. The task is to find the index of the character position for the word, which lies at the Kth index in the list of strings. Examples: Input : test_list = ["geekforgeeks", "is", "best", "for", "geeks"], K = 21 Output : 0Explanation : 21st index occurs in "geeks" and point to "g
3 min read
Specific Characters Frequency in String List-Python
The task of counting the frequency of specific characters in a string list in Python involves determining how often certain characters appear across all strings in a given list.For example, given a string list ["geeksforgeeks"] and a target list ['e', 'g'], the output would count how many times 'e'
4 min read
Python - Count frequency of Sublist in given list
Given a List and a sublist, count occurrence of sublist in list. Input : test_list = [4, 5, 3, 5, 7, 8, 3, 5, 7, 2, 3, 5, 7], sublist = [3, 5, 7] Output : 3 Explanation : 3, 5, 7 occurs 3 times. Input : test_list = [4, 5, 3, 5, 8, 8, 3, 2, 7, 2, 3, 6, 7], sublist = [3, 5, 7] Output : 0 Explanation :
3 min read
Python - Fractional Frequency of elements in List
Given a List, get fractional frequency of each element at each position. Input : test_list = [4, 5, 4, 6, 7, 5, 4, 5, 4]Â Output : ['1/4', '1/3', '2/4', '1/1', '1/1', '2/3', '3/4', '3/3', '4/4']Â Explanation : 4 occurs 1/4th of total occurrences till 1st index, and so on.Input : test_list = [4, 5, 4,
5 min read
Python - Sort String list by K character frequency
Given String list, perform sort operation on basis of frequency of particular character. Input : test_list = ["geekforgeekss", "is", "bessst", "for", "geeks"], K = 's' Output : ['bessst', 'geekforgeekss', 'geeks', 'is', 'for'] Explanation : bessst has 3 occurrence, geeksforgeekss has 3, and so on. I
4 min read