-3

I have a csv document in which each row contains a string of words. For each row, I want to know how many times each words appears in that row, how would I go about this?

Thanks

wasp
  • 1
  • 1

2 Answers2

0

Move the row to a list, make it unique and count every object in list,

For converting to list Check this

if result is more than one, print it.

Considered this as list ['a','b','a','c']

z=['a','b','a','c']
u=set(z)
for i in u:
    if z.count(i)>1: print i,z.count(i)

Output:

`a 2` 
Community
  • 1
  • 1
Siva Shanmugam
  • 662
  • 9
  • 19
0

Use collections.Counter:

from collections import Counter
z = ['a', 'b', 'c', 'd', 'a']
u = Counter(z)
print(u['a'])  # 2
print(u['b'])  # 1
Philip Tzou
  • 5,926
  • 2
  • 18
  • 27