mardi 12 janvier 2021

How to count word dictionary in python?

I have a question about dictionary handling in python.

I wonder how to compare the dictionary with the list of words.

Here are example about my question.

text = ['fall', 'big', 'pocket', 'layer', 'park',...]
mylist = ['spring', 'summer', 'fall', 'winter', 'school', ...]

[make dictionary code]

lst_dic = {mylist : 0 for mylist in mylist }
lst_dic

I want to know how many words matching in mylist from text.

[fail code]

 lst_dic = {}
 for w in mylist:
     if w in text_1_1:
         lst_dic[w] += 1
     else:
        lst_dic[w] = 0

But, It doesn't work. As a result, I want to know dictionary key and value. Like this,

{ 'spring':0,
  'summer':3,
  'fall':1,
  'winter':0,
     ...
}

Then, I extract more than 10 count value of attributes. Please take a look at this issue.

Aucun commentaire:

Enregistrer un commentaire