Change language

# Python | Find the frequency of a given character at each position in a list of lists

|

For a given list of lists, the task is to find the frequency of the character in each position of the sublist in the 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 items in each sublist, we need to find the position & # 39; X & # 39; at positions 0, 1 and 2. For position 0 in all sublists we have:
"x" in the first sublist at position zero,
& # 39; z & # 39; in the second sublist at position zero,
& # 39; y & # 39; in the third sublist at the zero position,
& # 39; z & # 39; in the fourth sublist at the zero position and
& # 39; y & # 39; in the fifth sublist at position zero.

So, we have 1 occurrence of & # 39; x & # 39; at position 1 in the whole sublist, therefore Occurrence = 1/5 = .2
For position 1 in the sublist there is no occurrence & # 39; x & # 39;, therefore Occurrence = 0/5 = 0.
For Position 2 we have 4 occurrences & # 39; x & # 39; in a sublist, so Occurrence = 4/5 = 0.8

Let’s discuss some of the ways this can be done.

Method # 1: Using Iteration

 ` # Python code to find the frequency of a character ` ` # at each list position in the list of lists. `   ` # Initialize the insertion list ` ` Input ` ` = ` ` [[` ` ’X ’` `, ` `’ Y’ ` `, ` ` ’X’ ` `], [` `’ Z’ ` `, ` ` ’Y’ ` `, ` `’ X’ ` `], ` ` ` ` [` ` ’Y’ ` `, ` `’ Y’ ` `, ` ` ’Y’ ` `], [` `’ Z’ ` `, ` ` ’Z’ ` `, ` `’ X’ ` `], ` ` [` ` ’Y’ ` `, ` `’ Z’ ` `, ` ` ’X’ ` `]] ` ` Output ` ` = ` ` [] `   ` # Character initialization ` ` character ` ` = ` ` ’X’ ` ` `  ` # Initialize the output list ` ` 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 ` `) `    ` # Print ` ` print ` ` (` ` "Initial list of list is:" ` `, ` ` Input ` `) ` ` print ` ` (` ` "Occurrence of’ X’ in list is "` `, Output) `

Exit:

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]

Method # 2: Using Zip

 ` # Python code to find character frequency ` ` # on each sleep position ska in the list of lists. `   ` # Initialize the input list ` ` 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 ` `)] `   ` # Print ` ` print ` ` (` ` "Initial list of list is: "` `, ` ` Input ` `) ` ` print ` ` (` ` "Occurrence of’ X’ in list is "` `, Output) `

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]

Method # 3: Using Pandas

 ` # Python code to find the frequency of a character ` ` # at each list position in the list of lists. `   ` import ` ` pandas as pd `   ` # Initialize the input list ` ` Input ` ` = ` ` [[` ` ’X’ ` `, ` ` ’Y’ ` `, ` `’ X’ ` `] , ` ` [` ` ’Z’ ` `, ` `’ Y’ ` `, ` ` ’X’ ` `], ` ` [` ` ’Y’ ` `, ` ` ’Y’ ` `, ` `’ Y’ ` `], ` ` [` ` ’Z’ ` `, ` `’ Z’ ` `, ` ` ’X’ ` `], ` ` ` ` [` `’ Y’ ` `, ` ` ’Z’ ` `, ` `’ X’ ` ` ]] `   ` # Character definition ` ` character ` ` = ` `’ X’ `   ` # using pandas ` ` Output ` ` = ` ` pd.DataFrame (` ` Input ` `) ` ` Output ` ` = ` ` Output.where (Output ` ` = ` ` = ` ` character, ` ` 0 ` `). where (Output! ` ` = ` ` character, ` ` 1 ` `) `   ` # Print ` ` print ` ` (` `" Initial list of list is: "` , ` Input ` `) ` ` print ` ` (` `" Occurrence of ’X’ in list is" ` `, 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

## Shop

Learn programming in R: courses

\$

Best Python online courses for 2022

\$

Best laptop for Fortnite

\$

Best laptop for Excel

\$

Best laptop for Solidworks

\$

Best laptop for Roblox

\$

Best computer for crypto mining

\$

Best laptop for Sims 4

\$

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

NUMPYNUMPY

How to convert Nonetype to int or string?

NUMPYNUMPY

How to specify multiple return types using type-hints

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

## Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method