Counting the Number of keywords in a dictionary in python

前端 未结 5 1547
离开以前
离开以前 2020-12-04 08:11

I have a list of words in a dictionary with the value = the repetition of the keyword but I only want a list of distinct words so I wanted to count the number of keywords. I

5条回答
  •  爱一瞬间的悲伤
    2020-12-04 08:35

    The number of distinct words (i.e. count of entries in the dictionary) can be found using the len() function.

    > a = {'foo':42, 'bar':69}
    > len(a)
    2
    

    To get all the distinct words (i.e. the keys), use the .keys() method.

    > list(a.keys())
    ['foo', 'bar']
    

提交回复
热议问题