counting

count the number of occurrences of a certain value in a dictionary in python?

count the number of occurrences of a certain value in a dictionary in python? Question: If I have got something like this: D = {‘a’: 97, ‘c’: 0 , ‘b’:0,’e’: 94, ‘r’: 97 , ‘g’:0} If I want for example to count the number of occurrences for the “0” as a value without having to …

Total answers: 5

Length of hexadecimal number

Length of hexadecimal number Question: How can we get the length of a hexadecimal number in the Python language? I tried using this code but even this is showing some error. i = 0 def hex_len(a): if a > 0x0: # i = 0 i = i + 1 a = a/16 return i b …

Total answers: 3

Counting the amount of occurrences in a list of tuples

Counting the amount of occurrences in a list of tuples Question: I am fairly new to python, but I haven’t been able to find a solution to my problem anywhere. I want to count the occurrences of a string inside a list of tuples. Here is the list of tuples: list1 = [ (‘12392’, ‘some …

Total answers: 3

Counting the number of True Booleans in a Python List

Counting the number of True Booleans in a Python List Question: I have a list of Booleans: [True, True, False, False, False, True] and I am looking for a way to count the number of True in the list (so in the example above, I want the return to be 3.) I have found examples …

Total answers: 8

Fast way of counting non-zero bits in positive integer

Fast way of counting non-zero bits in positive integer Question: I need a fast way to count the number of bits in an integer in python. My current solution is bin(n).count("1") but I am wondering if there is any faster way of doing this? Asked By: zidarsk8 || Source Answers: It turns out your starting …

Total answers: 13

Find the item with maximum occurrences in a list

Find the item with maximum occurrences in a list Question: In Python, I have a list: L = [1, 2, 45, 55, 5, 4, 4, 4, 4, 4, 4, 5456, 56, 6, 7, 67] I want to identify the item that occurred the highest number of times. I am able to solve it but I …

Total answers: 14

python histogram one-liner

python histogram one-liner Question: There are many ways to write a Python program that computes a histogram. By histogram, I mean a function that counts the occurrence of objects in an iterable and outputs the counts in a dictionary. For example: >>> L = ‘abracadabra’ >>> histogram(L) {‘a’: 5, ‘b’: 2, ‘c’: 1, ‘d’: 1, …

Total answers: 9

How to count the frequency of the elements in an unordered list?

How to count the frequency of the elements in an unordered list? Question: Given an unordered list of values like a = [5, 1, 2, 2, 4, 3, 1, 2, 3, 1, 1, 5, 2] How can I get the frequency of each value that appears in the list, like so? # `a` has 4 …

Total answers: 34

Item frequency count in Python

Item frequency count in Python Question: Assume I have a list of words, and I want to find the number of times each word appears in that list. An obvious way to do this is: words = “apple banana apple strawberry banana lemon” uniques = set(words.split()) freqs = [(item, words.split().count(item)) for item in uniques] print(freqs) …

Total answers: 14