Skip to content Skip to sidebar Skip to footer

Counting The Number Of Times The First Word In A Line Appears When Read From File With Exceptions

Using a dummy file (streamt.txt) with the following contents: andrew I hate mondays. fred Python is cool. fred Ko Ko Bop Ko Ko Bop Ko Ko Bop for ever andrew @fred no it isn't, what

Solution 1:

Use Counter :

from collections import Counter

withopen(filename, "r") as f:
    for line in f:
        if'DM'notin line and'RT'notin line:
            words = line.split()
            lst.append(words[0])

for k, v in Counter(lst).items():
    print(v, k)

# 2 andrew# 2 fred                                                     # 1 judy                                                  

Solution 2:

Count as follows:

#!/usr/bin/env python3.6from collections import Counter, defaultdict
from pathlib import Path

def main():
    n = input('Enter n: ')
    try:
        n = int(n)
    except:
        print('Invalid input.')
        return
    ss = Path('streamt.txt').read_text().strip().split('\n')
    c = Counter([
        i.strip().split(' ', 1)[0] for i in ss
        if i.strip().split(' ', 2)[1] not in ('RT',)
    ])
    d = defaultdict(list)
    for k, v in c.most_common():
        d[v].append(k)
    print('\n'.join([f'{k} {" ".join(v)}' for k, v in list(d.items())[:n]]))

if __name__ == '__main__':
    main()

Output:

Enter n:103andrew2fred1judyjohn

Solution 3:

Using collections Module.

Demo:

import collections
d = collections.defaultdict(int)
withopen(filename, "r") as infile:
    for line in infile:
        if'RT'notin line and'DM'notin line:
            d[line.strip().split()[0]] += 1

d = sorted(d.items(), key=lambda x: x[1], reverse=True)
for k,v in d:
    print(v, k)

Output:

2 andrew
2 fred
1 judy

Solution 4:

Here is a complete solution using only defaultdict as imported class. Note that it takes into account the fact that several users may have the same number of messages:

from collections import defaultdict

n = int(input("Enter n: "))

# Build dictionary with key = name / value = number of messages
d = defaultdict(int)
withopen('file.txt') as file:
    for line in file:
        words = line.split()
        if words[1] notin ["RT"]:
            d[words[0]] += 1# Build dictionary with key = number of messages / value = list of names
d_new = defaultdict(list)
for k,v in d.items():
    d_new[v].append(k)

# Keep only the top n items in dictionary sorted by number of messages
listOfNbAndNames = sorted(d_new.items(), reverse = True)[:n]
for nb,names in listOfNbAndNames:
    print(nb, " ".join(names))

Solution 5:

This can be done efficiently by recovering username of an author with str.split and keeping count with collections.Counter.

from collections import Counter

with open('streamt.txt', 'r') as file:
    count = Counter(line.split()[0] for line in file)

print(count) # Counter({'andrew': 4, 'fred': 2, 'judy': 2, 'george': 1, 'john': 1})

If you want the users sorted by number of message, your can then use Counter.most_common. You optionally pass as argument the number of items you want returned.

print(count.most_common())
# prints:  [('andrew', 4), ('fred', 2), ('judy', 2), ('george', 1), ('john', 1)]

print(count.most_common(2))
# prints:  [('andrew', 4), ('fred', 2)]

Post a Comment for "Counting The Number Of Times The First Word In A Line Appears When Read From File With Exceptions"