Finding the most common elements in an iterable in Python

How to find the most common elements in an iterable in Python or Django

collections.Counter lets you find the most common

# collections.Counter lets you find the most common
# elements in an iterable:

>>> import collections
>>> c = collections.Counter('helloworld')

>>> c
Counter({'l': 3, 'o': 2, 'e': 1, 'd': 1, 'h': 1, 'r': 1, 'w': 1})

>>> c.most_common(3)
[('l', 3), ('o', 2), ('e', 1)]

 

Please follow and like us:
0

Date difference in minutes in Python

Date difference in minutes in Python or Django?

How do I calculate the difference in days for the following datetime in Python?

2018-01-01 15:00:00
2018-01-05 15:00:00

from datetime import datetime

datetime_fmt = '%Y-%m-%d %H:%M:%S'
date1 = datetime.strptime('2010-01-01 17:31:22', datetime_fmt )
date2 = datetime.strptime('2010-01-03 17:31:22', datetime_fmt )

days_diff = (d2-d1).days
print (days_diff)
> 4

 

source:
https://docs.python.org/3/library/datetime.html#

Please follow and like us:
0