Skip to content Skip to sidebar Skip to footer

Frequency Of Letters Pairs At Position In String

I have a text file with a list of strings like: ALFLLNSYLTTL DYLLHGDDKSLN SLLADESADSHR YMVNLNDELYNN I need to write a script in python which will determine the frequency of each l

Solution 1:

You can use numpy and collections.Counter to calculate the frequency of letters by column. Use numpy to transform the file content to 2d array which can use multiple dimension slice. And use Counter to count the frequency of each unique letter.

import numpy as np

from collections import Counter

with open("text.txt", "r", encoding="utf-8") as text:
    lines = [list(line[:-1]) for line in text.readlines()]

lines = np.array(lines)

# for first question

for i in lines.shape[1]:

    counter = Counter(lines[:, i])

    for key in counter.keys():

        print key, counter[key] / lines.shape[0]

# for second question

counter = Counter(lines[:, 0:2])

print counter["AL"] / lines.shape[0]

Post a Comment for "Frequency Of Letters Pairs At Position In String"