Count days worked i.e count items in python list

python count number of items in list
python count frequency in list
python count items in list matching criteria
count occurrences of each element in list python
python count elements in list with condition
write a python program to get the frequency of the elements in a list
how to count duplicate values in list in python
python count occurrences in numpy array

I have a list of employees who work at different times of the day, what I would like is to count the number of days every guys has worked, like so:

FOO : 3 BAZ : 3 NOM : 1 etc....

this is how I receive the raw data:

my_list = [('NOM', datetime.date(2030, 1, 1)),
 ('BAR', datetime.date(2019, 4, 8)),
 ('HAM', datetime.date(2019, 4, 8)),
 ('FOO', datetime.date(2019, 4, 8)),
 ('BAZ', datetime.date(2019, 4, 8)),
 ('BAR', datetime.date(2019, 4, 10)),
 ('BAZ', datetime.date(2019, 4, 10)),
 ('FOO', datetime.date(2019, 4, 10)),
 ('HAM', datetime.date(2019, 4, 10)),
 ('HAM', datetime.date(2019, 4, 10)),
 ('FOO', datetime.date(2019, 4, 10)),
 ('BAR', datetime.date(2019, 4, 10)),
 ('BAZ', datetime.date(2019, 4, 10)),
 ('BAZ', datetime.date(2019, 4, 11)),
 ('HAM', datetime.date(2019, 4, 11)),
 ('BAR', datetime.date(2019, 4, 11)),
 ('FOO', datetime.date(2019, 4, 11)),
 ('BAZ', datetime.date(2019, 4, 11)),
 ('FOO', datetime.date(2019, 4, 11)),
 ('BAR', datetime.date(2019, 4, 11)),
 ('HAM', datetime.date(2019, 4, 11)),
 ('FOO', datetime.date(2019, 4, 11)),
 ('BAZ', datetime.date(2019, 4, 11)),
 ('BAR', datetime.date(2019, 4, 11)),
 ('HAM', datetime.date(2019, 4, 11)),
 ('FOO', datetime.date(2019, 4, 11)),
 ('BAZ', datetime.date(2019, 4, 11)),
 ('BAR', datetime.date(2019, 4, 11)),
 ('HAM', datetime.date(2019, 4, 11))]

I managed to strip the list to unique days for each individual like so : a = Counter(set(dictio)) which gets rid of the duplicates for one guy in a day:

Counter({('HAM', datetime.date(2019, 4, 8)): 1,
         ('HAM', datetime.date(2019, 4, 10)): 1,
         ('HAM', datetime.date(2019, 4, 11)): 1,
         ('BAR', datetime.date(2019, 4, 8)): 1,
         ('BAR', datetime.date(2019, 4, 10)): 1,
         ('BAR', datetime.date(2019, 4, 11)): 1,
         ('FOO', datetime.date(2019, 4, 8)): 1,
         ('FOO', datetime.date(2019, 4, 10)): 1,
         ('FOO', datetime.date(2019, 4, 11)): 1,
         ('BAZ', datetime.date(2019, 4, 8)): 1,
         ('BAZ', datetime.date(2019, 4, 10)): 1,
         ('BAZ', datetime.date(2019, 4, 11)): 1,
         ('NOM', datetime.date(2030, 1, 1)): 1})

This is where I'm stuck: I do I go from the above to:

HAM:3
BAR:3
FOO:3
BAZ:3
NOM:1

You can do with

import collections
collections.Counter(x for x , y in set(my_list) )
Out[251]: Counter({'BAR': 3, 'BAZ': 3, 'FOO': 3, 'HAM': 3, 'NOM': 1})

Given an item, how can I count its occurrences in a list in Python?, If you only want one item's count, use the count method: >>> [1, 2, 3, 4, 1, 4, 1]. count(1) 3. Don't use this if you want to count multiple items. Calling count in a� Count Items in List with a Given Value. To count the number of occurrences of items with a specific value in a list, you can call count() function on the list with the specific item passed as argument to the count() function. Syntax – count() Following is the syntax of count() function. n = list.count(item)

Use itertools.groupby:

from itertools import groupby
from operator import itemgetter

result = {key: len(group) for key, group in groupby(sorted(set(my_list)), key=itemgetter(0))}
print(result)

This sorts my_list by the first element (the names), partitions it into groups based on those names, and finally gets the name and length of each group as a key-value pair in a dict.

Output:

{'BAR': 3, 'BAZ': 3, 'FOO': 3, 'HAM': 3, 'NOM': 1}

Python List count() with EXAMPLES, Python count The count() is a built-in function in Python. The count() function is used to count elements on a list as well as a string. The count() method will return an integer value, i.e., the count of the given element from the given list. Following example shows the working of count() function on a list: There are many different ways to count all elements in a list with Python. You could have also different situations: Python how to count elements of a list: * Count elements in ordered list * Count elements in unordered list * Count elements with for loop * Count elements with pandas and numpy * Count elements with condition Python 3 Count elements in ordered list Below you can see how to find

Convert List to a Pandas Dataframe, Drop duplicates and Group by Name

import datetime
import pandas as pd
my_list = [('NOM', datetime.date(2030, 1, 1)),
 ('BAR', datetime.date(2019, 4, 8)),
 ('HAM', datetime.date(2019, 4, 8)),
 ('FOO', datetime.date(2019, 4, 8)),
 ('BAZ', datetime.date(2019, 4, 8)),
 ('BAR', datetime.date(2019, 4, 10)),
 ('BAZ', datetime.date(2019, 4, 10)),
 ('FOO', datetime.date(2019, 4, 10)),
 ('HAM', datetime.date(2019, 4, 10)),
 ('HAM', datetime.date(2019, 4, 10)),
 ('FOO', datetime.date(2019, 4, 10)),
 ('BAR', datetime.date(2019, 4, 10)),
 ('BAZ', datetime.date(2019, 4, 10)),
 ('BAZ', datetime.date(2019, 4, 11)),
 ('HAM', datetime.date(2019, 4, 11)),
 ('BAR', datetime.date(2019, 4, 11)),
 ('FOO', datetime.date(2019, 4, 11)),
 ('BAZ', datetime.date(2019, 4, 11)),
 ('FOO', datetime.date(2019, 4, 11)),
 ('BAR', datetime.date(2019, 4, 11)),
 ('HAM', datetime.date(2019, 4, 11)),
 ('FOO', datetime.date(2019, 4, 11)),
 ('BAZ', datetime.date(2019, 4, 11)),
 ('BAR', datetime.date(2019, 4, 11)),
 ('HAM', datetime.date(2019, 4, 11)),
 ('FOO', datetime.date(2019, 4, 11)),
 ('BAZ', datetime.date(2019, 4, 11)),
 ('BAR', datetime.date(2019, 4, 11)),
 ('HAM', datetime.date(2019, 4, 11))]

# COnvert List of Tuples to Dataframe
df = pd.DataFrame(my_list,columns=['name','date'])
#Remove Duplicates
df.drop_duplicates(inplace=True)
#Group by Name Count
df.groupby('name').count()

Counting the frequencies in a list using dictionary in Python , Given an unsorted list of some elements(may or may not be integers), Find the frequency of each distinct element in the list using a dictionary. How to count elements in lists in Python, count occurrences or test if a given value exists in a list. Learn Python functions len or count.

Python program to find number of days between two given dates , Given two dates, Write a Python program to find the total number of days between them. A Better and Simple solution is to count total number of days before dt1 from i.e., total Finally return the difference between two counts. Python program to find number of m contiguous elements of a List with a� Counting the occurrences of one item in a list. For counting the occurrences of just one list item you can use count () >>> l = ["a","b","b"] >>> l.count("a") 1 >>> l.count("b") 2. Counting the occurrences of all items in a list is also known as "tallying" a list, or creating a tally counter.

7.1. datetime — Basic date and time types — Python v3.2.6 , Naive objects are easy to understand and to work with, at the cost of ignoring some Example of counting days to an event: Example of working with date: += timedelta(days_to_go) return dt # US DST Rules # # This is a simplified (i.e., wrong for a The following is a list of all the format codes that the C standard ( 1989� count() is an inbuilt function in Python that returns count of how many times a given object occurs in list. Syntax : list_name.count(object) Parameters : Object is the things whose count is to be returned.

Pandas Groupby: Summarising, Aggregating, and Grouping data in , Aggregation and grouping of Dataframes is accomplished in Python Pandas using "groupby()" and "agg()" functions. Apply max, min, count, distinct to groups. Python List count() The count() method returns the number of times the specified element appears in the list. The syntax of the count() method is: list.count(element)

Comments
  • I like this. +1
  • Nice. I was looking for a clean way to do this using Counter. +1
  • @ChristianDean ha :-) happy coding
  • Thx dude ;) @Wen-Ben
  • Nice, clean and Pythonic, Thanks Wen-Ben ;-)