0

How would you go about extracting part of a key from a dictionary.

I have a text file that lists about 1000 birthdays Ex. 1/2/1980
I want to extract just the year portion from the dictionary and then run a count against each year.
I have no problems with any of it, except how to ignore the 1/2/.

4
Contributors
3
Replies
4
Views
6 Years
Discussion Span
Last Post by richieking
0

A couple way is split at / or use a regex.

>>> d = '1/2/2010'.split('/')[2]
>>> d
'2010'

>>> import re
>>> s = re.search(r'\d{4}', '1/2/2010')
>>> s.group()
'2010'
>>>

Edited by snippsat: n/a

0

And for counting from collections module defaultdict(int) (should be capilized but is not) or Counter for latest versions.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.