In Python, how do you change values ​​in a dictionary based on the frequency of its key?

I create a program that reads through the .txt file of names (lastname, firstname), one per line and creates a dictionary that shows the number of repetitions of a particular name.

I have received the following code so far, but I can’t accurately count the number of repetitions of the name. I think the problem is that my variable β€œvalue” does not match the actual value in the key value pair. How can i fix this?

file = open('names.txt') dict = {} value = 1 for line in file: listOfNames = line.split(",") firstName = listOfNames[1] if dict.has_key(firstName): value += 1 else: dict[firstName] = value file.close() 
+4
source share
6 answers

You might be interested in collections.Counter is a special dictionary for this kind of task.

+6
source

It looks like you want something like:

 if dict.has_key(firstName): dict[firstName] += 1 else: dict[firstName] = 1 

In addition, I strongly recommend that you choose a name other than dict , such as names . The reason is that dict is the name of the standard Python dictionary type (just like you usually don't want to create Python variables called str , int or list ).

There are other solutions, such as collections.defaultdict , that will be more concise.

+2
source

You can replace the if block with:

 dict[firstname] = dict.get(firstname, 0) + 1 

Alternatively, you can use collections.Counter instead of dict. This simplifies code counting only:

 c[firstname] += 1 

where c is the instance of the instance.

+2
source

Use defaultdict as follows:

 from collections import defaultdict d = defaultdict(int) for name in open('names.txt'): _, first_name = name.split(",") d[first_name] += 1 

You can normalize your names by separating spaces and capital letters.

+2
source

As @Aurora notes, Counter is great for this.

 >>> names = ['foo bar', 'foo baz', 'foo car', 'doo bar', 'doo baz', 'boo paz'] >>> from collections import Counter >>> Counter(name.split()[1] for name in names) Counter({'baz': 2, 'bar': 2, 'paz': 1, 'car': 1}) 
+2
source
 with open('names.txt') as f: firstNames = [line.split(',')[0] for line in f] print collections.Counter(firstNames) 
+2
source

Source: https://habr.com/ru/post/1399107/


All Articles