counter

python: Dictionary counter items. How to optimize performance

python: Dictionary counter items. How to optimize performance Question: I need to keep track of occurrences of items in parsed files in a structure as follows: data= {‘tomate’: {‘John’: 2, ‘mike’: 2}, ‘pasta’: {‘mike’: 1}, ‘wine’: {‘mike’: 1, ‘alex’: 2}} the dictionary starts as empty one. data = {} and its is being populated with …

Total answers: 4

Code not looping through the rest of my file or resetting the counter

Code not looping through the rest of my file or resetting the counter Question: Currently trying to loop through a file with addresses in python, but for some reason instead of looping through the entire file – it stops at the first line. I tried resetting the counter and making it go back to the …

Total answers: 1

Python: Weird result while indexing the dictionary from collections.Counter

Python: Weird result while indexing the dictionary from collections.Counter Question: Let’s say I have a list of numbers numbers = [‘3’, ‘3’, ‘4’, ‘4’] I want to count the number of occurrences of the elements in the list, so I use collections.Counter from collections import Counter result = Counter(numbers) result Counter({‘3’: 2, ‘4’: 2}) Here’s …

Total answers: 1

Using a counter in a python function

Using a counter in a python function Question: I am trying to count the iterations in a function, but I can’t get it right. What is wrong with the code? Every time I call the function, the counter give me zero as a result. I want to add a pause between iteration an print the …

Total answers: 2

Pandas Series of lists of strings how to count and append to df per row

Pandas Series of lists of strings how to count and append to df per row Question: Summarize the problem I have a Dataframe with a Series of lists of strings. I can use a Counter from ‘collections’ to iterate over each row of the series and count the times each string appeared easy enough: for …

Total answers: 1

Python adding Counter objects with negative numbers fails

Python adding Counter objects with negative numbers fails Question: why do adding an empty counter to another with a negative value results in 0 from collections import Counter a=Counter({‘a’:-1}) b=Counter() print(a+b) Result Counter() but if the counter added to the empty one has a positive value it works. How do I preserve those negative values? …

Total answers: 2

Function to see if i can generate phrase from characters string

Function to see if i can generate phrase from characters string Question: I am trying to write a function that checks if I can generate the required word/phrase using the characters provided. The phrase created can contain any characters including special characters, capital letters, numbers, and spaces. I can generate the phrase if the frequency …

Total answers: 2

How to find the most common name in 2 related lists

How to find the most common name in 2 related lists Question: I would like to seek help from the community.. I have 2 related lists here: names = [‘alan_grant’, ‘alan_grant’, ‘alan_grant’, ‘alan_grant’, ‘alan_grant’, ‘claire_dearing’, ‘claire_dearing’, ‘claire_dearing’, ‘claire_dearing’, ‘claire_dearing’, ‘ellie_sattler’, ‘ellie_sattler’, ‘ellie_sattler’, ‘ellie_sattler’, ‘ellie_sattler’, ‘ian_malcolm’, ‘ian_malcolm’, ‘ian_malcolm’, ‘ian_malcolm’, ‘ian_malcolm’, ‘john_hammond’, ‘john_hammond’, ‘john_hammond’, ‘john_hammond’, ‘john_hammond’, ‘owen_grady’, …

Total answers: 2

Why is my counter not updated by "count == count + 1"?

Why is my counter not updated by "count == count + 1"? Question: answers = [] for syn in all_words : count = 0 result = [syn, count] for Document in Corpus: for Word in Document: if syn == Word : count == count + 1 answers.append(tuple(result)) i’m trying to count the number of occurrences …

Total answers: 2