Advertisement
zhongnaomi

counting occurrences of a given item in a list

Feb 7th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.60 KB | None | 0 0
  1. #counting occurrences of a given item in a list of strings
  2. from collections import Counter
  3.  
  4. colours =['white', 'white', 'green', 'purple', 'black', 'white', 'white',
  5. 'black', 'blue',     'purple', 'yellow', 'green', 'white',
  6. 'blue', 'blue', 'green', 'green', 'red', 'black', 'yellow',
  7. 'white', 'blue', 'orange', 'green', 'blue', 'green', 'white',
  8. 'red', 'yellow', 'red', 'green', 'white', 'red', 'white',
  9. 'yellow', 'orange', 'red', 'black', 'green', 'orange', 'purple',
  10. 'blue', 'red', 'red', 'blue', 'purple', 'yellow', 'yellow', 'red',
  11. 'yellow']
  12.  
  13. c = Counter(colours)
  14. print (c[input('What is the colour?  ')])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement