0% found this document useful (0 votes)
11 views4 pages

Collections Datetime

The document shows examples of using various Python collections and datetime modules. It demonstrates initializing and using a Counter to count elements in a list, initializing a defaultdict with default values, ordering a dictionary with OrderedDict, and manipulating datetime objects to extract attributes like year, month, day, and format datetime strings. It also shows adding a timedelta to a datetime and working with timezones using pytz.

Uploaded by

mgm9211
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views4 pages

Collections Datetime

The document shows examples of using various Python collections and datetime modules. It demonstrates initializing and using a Counter to count elements in a list, initializing a defaultdict with default values, ordering a dictionary with OrderedDict, and manipulating datetime objects to extract attributes like year, month, day, and format datetime strings. It also shows adding a timedelta to a datetime and working with timezones using pytz.

Uploaded by

mgm9211
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

24/8/2019 Untitled

In [1]:

from collections import Counter

In [2]:

l = [1, 2, 1, 3, 4, 1, 2, 5]

In [3]:

Counter(l)

Out[3]:

Counter({1: 3, 2: 2, 3: 1, 4: 1, 5: 1})

In [4]:

animales = "gato perro canario gato oveja jirafa perro canario jirafa jirafa ovejita

In [5]:

Counter(animales)

Out[5]:

Counter({'g': 2,
'a': 14,
't': 3,
'o': 8,
' ': 10,
'p': 2,
'e': 4,
'r': 9,
'c': 2,
'n': 2,
'i': 6,
'v': 2,
'j': 5,
'f': 3})

In [6]:

l1 = Counter(l)
l1.most_common(1)

Out[6]:

[(1, 3)]

https://hub.gke.mybinder.org/user/ipython-ipython-in-depth-sf3vkic3/notebooks/binder/Untitled.ipynb?kernel_name=python3 1/4
24/8/2019 Untitled

In [14]:

from collections import defaultdict

d = defaultdict(int)
d[1] = 1
d[2] = 4
d[3] = 3
d[0] = 1
d

Out[14]:

defaultdict(int, {1: 1, 2: 4, 3: 3, 0: 1})

In [16]:

from collections import OrderedDict

d = OrderedDict(d)
d

Out[16]:

OrderedDict([(1, 1), (2, 4), (3, 3), (0, 1)])

In [27]:

import datetime

dt = datetime.datetime.now()
dt

Out[27]:

datetime.datetime(2019, 8, 24, 9, 51, 30, 221605)

In [21]:

dt.year

Out[21]:

2019

In [22]:

dt.month

Out[22]:

In [23]:

dt.day

Out[23]:

24

https://hub.gke.mybinder.org/user/ipython-ipython-in-depth-sf3vkic3/notebooks/binder/Untitled.ipynb?kernel_name=python3 2/4
24/8/2019 Untitled

In [24]:

dt.hour

Out[24]:

In [28]:

dt.tzinfo

In [29]:

dt.strftime("%A %d %B %Y %I:%M")

Out[29]:

'Saturday 24 August 2019 09:51'

In [43]:

import locale
locale.setlocale(locale.LC_ALL, 'es_ES.UTF-8')

----------------------------------------------------------------------
-----
Error Traceback (most recent call
last)
<ipython-input-43-54b233d80e90> in <module>
1 import locale
----> 2 locale.setlocale(locale.LC_ALL, 'es_ES.UTF-8')

/srv/conda/lib/python3.6/locale.py in setlocale(category, locale)


596 # convert to string
597 locale = normalize(_build_localename(locale))
--> 598 return _setlocale(category, locale)
599
600 def resetlocale(category=LC_ALL):

Error: unsupported locale setting

In [35]:

dt.strftime("%A %d %B %Y %I:%M")

Out[35]:

'Saturday 24 August 2019 09:51'

In [45]:

t = datetime.timedelta(days=15, hours=3, minutes=43)


dentro_de_dos_semanas = dt + t
dentro_de_dos_semanas

Out[45]:

datetime.datetime(2019, 9, 8, 13, 34, 30, 221605)

https://hub.gke.mybinder.org/user/ipython-ipython-in-depth-sf3vkic3/notebooks/binder/Untitled.ipynb?kernel_name=python3 3/4
24/8/2019 Untitled

In [46]:

import pytz

In [47]:

pytz.all_timezones

Out[47]:

['Africa/Abidjan',
'Africa/Accra',
'Africa/Addis_Ababa',
'Africa/Algiers',
'Africa/Asmara',
'Africa/Asmera',
'Africa/Bamako',
'Africa/Bangui',
'Africa/Banjul',
'Africa/Bissau',
'Africa/Blantyre',
'Africa/Brazzaville',
'Africa/Bujumbura',
'Africa/Cairo',
'Africa/Casablanca',
'Africa/Ceuta',
'Africa/Conakry',
'Africa/Dakar',

In [48]:

dt = datetime.datetime.now(pytz.timezone('Europe/Madrid'))

In [49]:

dt

Out[49]:

datetime.datetime(2019, 8, 24, 12, 28, 56, 12480, tzinfo=<DstTzInfo 'E


urope/Madrid' CEST+2:00:00 DST>)

In [ ]:

https://hub.gke.mybinder.org/user/ipython-ipython-in-depth-sf3vkic3/notebooks/binder/Untitled.ipynb?kernel_name=python3 4/4

You might also like