Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python counter nested dictionary

import collections
a = collections.defaultdict(collections.Counter)
inf = [('fruit','apple'),('car','truck'),('fruit','banana'),('fruit','banana')]
for category,item in inf:
    a[category][item] = a[category][item] + 1   
#print(a)
#{'fruit': Counter({'banana': 2, 'apple': 1}), 'car': Counter({'truck': 1})})
Comment

Nested dictionary Python

IDs = ['emp1','emp2','emp3']

EmpInfo = [{'name': 'Bob', 'job': 'Mgr'},
           {'name': 'Kim', 'job': 'Dev'},
           {'name': 'Sam', 'job': 'Dev'}]

D = dict(zip(IDs, EmpInfo))

print(D)
# Prints {'emp1': {'name': 'Bob', 'job': 'Mgr'},
#         'emp2': {'name': 'Kim', 'job': 'Dev'},
#         'emp3': {'name': 'Sam', 'job': 'Dev'}}
Comment

Nested dictionary Python

D = {'emp1': {'name': 'Bob', 'job': 'Mgr'},
     'emp2': {'name': 'Kim', 'job': 'Dev'},
     'emp3': {'name': 'Sam', 'job': 'Dev'}}
Comment

PREVIOUS NEXT
Code Example
Python :: how to rename columns in pandas dataframe 
Python :: how to get the realpath with python 
Python :: timeout socket python 
Python :: string in list python 
Python :: how to print last element in a list python 
Python :: make a new environment conda 
Python :: how to hide ticks in python 
Python :: task timed out after 3.00 seconds aws lambda python 
Python :: hugging face change directory model 
Python :: jointplot title 
Python :: read pickle file 
Python :: use proxy to connect smtp python 
Python :: conda enviroment python version 
Python :: pyspark add_months 
Python :: python 2d dictionary 
Python :: how to take a column from dataset in python 
Python :: python convert string to list of dictionaries 
Python :: csv download django 
Python :: random chars generator python 
Python :: numpy multiply element wise 
Python :: numpy scale array 
Python :: Modify a Python interpreter 
Python :: console-based animation-simple 
Python :: delimiter pandas 
Python :: how to print in double quotes in python 
Python :: save seaborn lmplot 
Python :: switch between frames in tkinter 
Python :: scree plot sklearn 
Python :: python concatenation 
Python :: How do I stop Selenium from closing my browser 
ADD CONTENT
Topic
Content
Source link
Name
5+3 =