Skip to content

Instantly share code, notes, and snippets.

@acrymble
Created July 5, 2011 19:25
Show Gist options
  • Save acrymble/1065647 to your computer and use it in GitHub Desktop.
Save acrymble/1065647 to your computer and use it in GitHub Desktop.
Python count items in a list 2
# count-list-items-1.py
wordstring = 'it was the best of times it was the worst of times '
wordstring += 'it was the age of wisdom it was the age of foolishness'
wordlist = wordstring.split()
wordfreq = [wordlist.count(w) for w in wordlist] # a list comprehension
print "String\n" + wordstring +"\n"
print "List\n" + str(wordlist) + "\n"
print "Frequencies\n" + str(wordfreq) + "\n"
print "Pairs\n" + str(zip(wordlist, wordfreq))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment