How to combine two dictionaries using python - python

import csv
def partytoyear():
party_in_power = {}
with open("presidents.txt") as f:
reader = csv.reader(f)
for row in reader:
party = row[1]
for year in row[2:]:
party_in_power[year] = party
print(party_in_power)
return party_in_power
partytoyear()
def statistics():
with open("BLS_private.csv") as f:
statistics = {}
reader = csv.DictReader(f)
for row in reader:
statistics = row
print(statistics)
return statistics
statistics()
These two functions return two dictionaries.
Here is a sample of the first dictionary:
'Democrat', '1981': 'Republican', '1982': 'Republican', '1983'
Sample of the second dictionary:
'2012', '110470', '110724', '110871', '110956', '111072', '111135', '111298', '111432', '111560', '111744'
The first dictionary associates a year and the political party. The next dictionary associates the year with job statistics.
I need to combine these two dictionaries, so I can have the party inside the dictionary with the job statistics.
I would like the dictioary to look like this:
'Democrat, '2012','110470', '110724', '110871', '110956', '111072', '111135', '111298', '111432', '111560', '111744'
How would I go about doing this? I've looked at the syntax for update() but that didn't work for my program

You can’t have a dictionary in that manor in python it’s syntactically wrong but you can have each value be a collection such as a list. Here’s a comprehension that does just that using dict lookups:
first_dict = {'Democrat': '1981': 'Republican': '1982': 'Republican': '1983', ...}
second_dict = {'2012': ['110470', '110724', '110871', '110956', '111072', '111135', '111298', '111432', '111560', '111744'], ...}
result = {party: [year, *second_dict[year] for party, year in first_dict.items()}
Pseudo result dict structure:
{'Party Name': [year, stats, ...], ...}

Related

Nested Dictionary Creation - Python

I have a dictionary setup like this:
company = {'Honda': {}
,'Toyota':{}
,'Ford':{}
}
I have a list containing data like this:
years = ['Year 1', 'Year 2', 'Year 3']
Finally, I also have a list of lists containing data like this:
sales = [[55,9,90],[44,22,67],[83,13,91]]
I am trying to achieve a final result that looks like this:
{'Honda': {'Year 1':55,'Year 2':9,'Year 3':90}
,'Toyota':{'Year 1':44,'Year 2':22,'Year 3':67}
,'Ford':{'Year 1':83,'Year 2':13,'Year 3':91}
}
I can access the sub-list if sales like this:
for i in sales:
for j in i:
#this would give me a flat list of all sales
I can't seem to wrap my head around constructing the final dictionary that would tie everything together.
Any help is appreciated!
You can use a dict comprehension with zip.
res = {k : dict(zip(years, sale)) for k, sale in zip(company, sales)}
You can use zip to pair corresponding information together. First, zip the brand names with the values in sales. Next, zip years with a particular brand's sales numbers.
company = {brand: dict(zip(years, sales_nums))
for brand, sales_nums in zip(["Honda", "Toyota", "Ford"], sales)}
You can use zip and a double for-loop to zip all 3 lists. Here you are:
final_dict = {}
for i, (name, sub_dict) in enumerate(company.items()):
for year, sale in zip(years, sales[i]):
sub_dict[year] = sale
final_dict[name] = sub_dict

Create a nested dict containing list from a file

For example, for the txt file of
Math, Calculus, 5
Math, Vector, 3
Language, English, 4
Language, Spanish, 4
into the dictionary of:
data={'Math':{'name':[Calculus, Vector], 'score':[5,3]}, 'Language':{'name':[English, Spanish], 'score':[4,4]}}
I am having trouble with appending value to create list inside the smaller dict. I'm very new to this and I would not understand importing command. Thank you so much for all your help!
For each line, find the 3 values, then add them to a dict structure
from pathlib import Path
result = {}
for row in Path("test.txt").read_text().splitlines():
subject_type, subject, score = row.split(", ")
if subject_type not in result:
result[subject_type] = {'name': [], 'score': []}
result[subject_type]['name'].append(subject)
result[subject_type]['score'].append(int(score))
You can simplify it with the use of a defaultdict that creates the mapping if the key isn't already present
result = defaultdict(lambda: {'name': [], 'score': []}) # from collections import defaultdict
for row in Path("test.txt").read_text().splitlines():
subject_type, subject, score = row.split(", ")
result[subject_type]['name'].append(subject)
result[subject_type]['score'].append(int(score))
With pandas.DataFrame you can directly the formatted data and output the format you want
import pandas as pd
df = pd.read_csv("test.txt", sep=", ", engine="python", names=['key', 'name', 'score'])
df = df.groupby('key').agg(list)
result = df.to_dict(orient='index')
From your data:
data={'Math':{'name':['Calculus', 'Vector'], 'score':[5,3]},
'Language':{'name':['English', 'Spanish'], 'score':[4,4]}}
If you want to append to the list inside your dictionary, you can do:
data['Math']['name'].append('Algebra')
data['Math']['score'].append(4)
If you want to add a new dictionary, you can do:
data['Science'] = {'name':['Chemisty', 'Biology'], 'score':[2,3]}
I am not sure if that is what you wanted but I hope it helps!

Is it possible to store data within a key, within another key in a dictionary?

I have an dictionary of keys that are the initials of employees' names. I need to store data for each month of the year, for each employee (key). Is it possible to store each month as a key, within each employee key, and then store data for each month?
This is my code if it helps:
#Creates dictionary, each employee as a key
employee_dict = dict((z[0],list(z[1:])) for z in zip(employee_list))
for key, value in employee_dict.items():
for i in data_to_store_for_each_employee_array:
if i[0] == key:
employee_dict[key].append(i[1])
My programs reads the employee initials off of a txt file using configparser. It then stores the employee initials into a list. The initials are prone to change so I am unable to hard-code them into my program
My dictionary looks like this right now:
dict_items([('TS', []), ('IR', []), ('RD', []), ('SP', []), ('RA', []), ('WN', []), ('KT', [])])
I was hoping to get it to look something like:
dict_items([('TS': January[], February[], March[]....) {and so on, for each pair of initials}
You could try creating a dictionary with a each having a dictionary value (a nested dictionary). This is what it would look like:
info_dict = {
'employee_1': {'January': #data,
'February':#data}
'employee_2': {'January': #data,
'February':#data}
'employee_3': {'January': #data,
'February':#data}
}
If you have a .txt file of employee names than you would turn those into keys and add the value to a particular employee key:
employees = []
dictionary = {}
nested_dictionary = {}
filename = '#file_name'
with open(filename, 'r') as f_obj:
lines = f_obj.readlines()
for line in lines:
name_lst = line.split(' ')
for name in name_lst:
employees.append(name)
for employee in employees:
dictionary[employee] = None #Because I don't know where you're data is coming from...
print(dictionary)

How to create a list in Python with the unique values of a CSV file?

I have CSV file that looks like the following,
1994, Category1, Something Happened 1
1994, Category2, Something Happened 2
1995, Category1, Something Happened 3
1996, Category3, Something Happened 4
1998, Category2, Something Happened 5
I want to create two lists,
Category = [Category1, Category2, Category3]
and
Year = [1994, 1995, 1996, 1998]
I want to omit the duplicates in the column. I am reading the file as following,
DataCaptured = csv.reader(DataFile, delimiter=',')
DataCaptured.next()
and Looping through,
for Column in DataCaptured:
You can do:
DataCaptured = csv.reader(DataFile, delimiter=',', skipinitialspace=True)
Category, Year = [], []
for row in DataCaptured:
if row[0] not in Year:
Year.append(row[0])
if row[1] not in Category:
Category.append(row[1])
print Category, Year
# ['Category1', 'Category2', 'Category3'] ['1994', '1995', '1996', '1998']
As stated in the comments, if order does not matter, using a set would be easier and faster:
Category, Year = set(), set()
for row in DataCaptured:
Year.add(row[0])
Category.add(row[1])
A very concise way to do this is to use pandas, the benefits are: it has a faster CSV pharser; and it works in columns (so it only requires one df.apply(set) to get you there) :
In [244]:
#Suppose the CSV is named temp.csv
df=pd.read_csv('temp.csv',header=None)
df.apply(set)
Out[244]:
0 set([1994, 1995, 1996, 1998])
1 set([ Category2, Category3, Category1])
2 set([ Something Happened 4, Something Happene...
dtype: object
The downside is that it returns a pandas.Series, and to get access each list, you need to do something like list(df.apply(set)[0]).
Edit
If the order has to be preserved, it can be also done very easily, for example:
for i, item in df.iteritems():
print item.unique()
item.unique() will return numpy.arrays, instead of lists.
dawg pointed out one of the greatest tricks in Python: using set() to remove duplicates from a list. dawg shows how to build the unique list from scratch by adding each item to a set, which is perfect. But here's another equivalent way to do it, generating a list with duplicates and a list without duplicates using a list(set()) approach:
import csv
in_str = [
'year, category, event',
'1994, Category1, Something Happened 1',
'1994, Category2, Something Happened 2',
'1995, Category1, Something Happened 3',
'1996, Category3, Something Happened 4',
'1998, Category2, Something Happened 5'
]
cdr = csv.DictReader(in_str, skipinitialspace=True)
col = []
for i in cdr:
col.append(i['category'])
# all items in the column...
print(col)
# only unique items in the column...
print(list(set(col)))

Read two column CSV as dict with 1st column as key

I have a CSV with two columns, column one is the team dedicated to a particular building in our project.
The second column is the actual building number.
What I am looking for is a dictionary with the first column as the key and the buildings that belong to that team in the list.
I have tried various forms of csv.reader and csv.DictReader along with different for loops to rewrite the data to another dictionary, but I cannot get the structure I want.
CSV:
team,bldg,
3,204,
3,250,
3,1437,
2,1440,
1,1450,
The structure of the dictionary would be as follows:
dict["1"] = ["1450"]
dict["2"] = ["1440"]
dict["3"] = ["204", "250", "1437"]
This works:
import csv
result={}
with open('/tmp/test.csv','r') as f:
red=csv.DictReader(f)
for d in red:
result.setdefault(d['team'],[]).append(d['bldg'])
#results={'1': ['1450'], '3': ['204', '250', '1437'], '2': ['1440']}
The useful collections.defaultdict in the standard library makes short work of this task:
import csv
import collections as co
dd = co.defaultdict(list)
with open('/path/to/your.csv'),'rb') as fin:
dr = csv.DictReader(fin)
for line in dr:
dd[line['team']].append(line['bldg'])
# defaultdict(<type 'list'>, {'1': ['1450'], '3': ['204', '250', '1437'], '2': ['1440']})
http://docs.python.org/2/library/collections.html#collections.defaultdict
The first argument provides the initial value for the default_factory
attribute; it defaults to None.

Categories

Resources