Posts

Showing posts from September, 2024

4.1 Applied Python (Advanced Course) https://brilliant.org/courses/python-text-analysis/dictionaries-and-counters/?from_llp=computer-science

Image
 4.1 Applied Python  (Advnaced Course) https://brilliant.org/courses/python-text-analysis/dictionaries-and-counters/?from_llp=computer-science    Lesson 3 Counting Unique Words Instead of dictionary use Counter() function it is more handly No need to initialized any key before adding any addition: Python 1 2 3 4 5 6 7 8 9 10 11 from collections import Counter #make a counter object counter = Counter () #make a string word = 'horror' #loop over the letters in the string and add to the counter for letter in word : counter [ letter ] += 1 #print the value of key "r" print ( counter [ 'r' ] ) 1 2 3 4 from collections import Counter counter = Counter ( "horror" ) key = 's' print ( key , counter [ key ] ) s 0 , because  s  appears zero times in  horror Python 1 2 3 4 5 6 from collections import Counter counter = Counter ( 'horror' ) key = 's' print ( "Before:" , key , counter [ key ] ) counter...