Python: Add occurrences of a list element and its total count to another list, avoid adding duplicates -


i have corpus of text containing sentences. wish count number of occurrences of each word , avoid adding word more once (e.g. multiple occurrences of ',' must added once return ',': 2047)

desired output:'partner': 7, 'meetings': 7, '14': 7, 'going': 7,etc. realize need use set() avoid duplicates. don't know how. currently, avoiding adding elements in list saying append if not in occurrences

this isn't working getting ',':2047 multiple times in result.

i avoiding list comprehensions in sample code increase reader's comprehension! :p

counting occurrences of words[i] in words

occurrences = [] in range(1, words.__len__() - 1):     if words[i-1] not in occurrences:         occurrences.append((words[i - 1], words.count(words[i - 1]))) print(occurrences) 

use collections.counter:

word_count = counter(words) 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -