dictionary - Add column in dict with partial key in python -


setting dict:

rr = range(1,11) ft =[('sd:jan:'+ str(x), 'news') x in rr]     fd = dict(ft)   fd     {'sd:jan:1': 'news',      'sd:jan:10': 'news',      'sd:jan:2': 'news',      'sd:jan:3': 'news',      'sd:jan:4': 'news',      'sd:jan:5': 'news',      'sd:jan:6': 'news',      'sd:jan:7': 'news',      'sd:jan:8': 'news',      'sd:jan:9': 'news'}  fd.keys()  ['sd:jan:10',  'sd:jan:2',  'sd:jan:3',  'sd:jan:1',  'sd:jan:6',  'sd:jan:7',  'sd:jan:4',  'sd:jan:5',  'sd:jan:8',  'sd:jan:9'] 

how add values of 'jan' in key?

edit: adding values (1+2+3+4+5+6+...+10) partial key of "jan."

how using generator expression:

sum(int(i.split(':')[-1]) in fd.keys()) 

gives:

55 

splits each entry :, grabs last field, converts int , sums them up.

in case you'd needed examine numbers, or wanted them reason later collect them in list using list comprehension:

[int(i.split(':')[-1]) in fd.keys()] 

Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -