You are on page 1of 21

2/2/2021 Python for O&G Lecture 43, 44, 45, 46, 47, 48, 49, 50, 51, 52 - Dictionaries

44, 45, 46, 47, 48, 49, 50, 51, 52 - Dictionaries - Colaboratory

Python for Oil and Gas

Website - https://petroleumfromscratchin.wordpress.com/

LinkedIn - https://www.linkedin.com/company/petroleum-from-scratch

YouTube - https://www.youtube.com/channel/UC_lT10npISN5V32HDLAklsw
Code Text

Lecture 43: Dictionaries - Part 1

Dictionaries are unordered collection of data in key:value pair

Can store naything inside dictionaries --> numbers, lists, strings, dictioanries etc

It's Easier to access data in Dictionaires (you'll understand why soon)

# Syntax {}

# {key1:value1, key2:value2}
/
2/2/2021 Python for O&G Lecture 43, 44, 45, 46, 47, 48, 49, 50, 51, 52 - Dictionaries - Colaboratory

# If we wanted to store the porosities of different reservoirs in lists

# a, b, c

phi_list = [0.14, 0.29, 0.40]

print(phi_list)

[0.14, 0.29, 0.4]

# make a dictionary now for same porosities

phi_dict = {'res_a' : 0.14, 'res_b': 0.29, 'res_c' : 0.40}

print(phi_dict)

# {'key1': value1, 'key2': value2}

{'res_a': 0.14, 'res_b': 0.29, 'res_c': 0.4}

/
2/2/2021 Python for O&G Lecture 43, 44, 45, 46, 47, 48, 49, 50, 51, 52 - Dictionaries - Colaboratory

Method 2 to create dictionary

# another way to create a dictionary

# variable = dict(key1 = value1, key2 = value2)

# dict(key1 = value1, key2 = value2, ---)

phi_dict_new = dict(res_a = 0.14, res_b = 0.29, res_c = 0.40)

print(phi_dict_new)

{'res_a': 0.14, 'res_b': 0.29, 'res_c': 0.4}

Method 3 to create dictionary

# one more way to create a dictionary

# Syntax: Tuples inside a list and 2 items in tuple i.e. one key and second is value

xyz = [('res_a', 0.14), ('res_b', 0.29), ('res_c', 0.4)]

phi_dict_2 = dict(xyz)

print(phi_dict_2)

/
2/2/2021 Python for O&G Lecture 43, 44, 45, 46, 47, 48, 49, 50, 51, 52 - Dictionaries - Colaboratory
{'res_a': 0.14, 'res_b': 0.29, 'res_c': 0.4}

Lectre 44: Dictionaries - Part 2

Accessing the data in dictionaries

# There's no index in dictionaries

# We access the data using keys

# instead of index you use key

# say if, i want the porosity of res_b

phi_dict = {'res_a' : 0.14, 'res_b': 0.29, 'res_c' : 0.40}

phi_dict['res_c']

0.4

Excercise

# make a dictionary conatining keys of 'name', 'college', 'fav_movies' list, 'hobbies' list
/
2/2/2021 Python for O&G Lecture 43, 44, 45, 46, 47, 48, 49, 50, 51, 52 - Dictionaries - Colaboratory

# and then print the name of 2nd movie from your fav_movies list

excer = {
'name' : 'Divyansh Sethi',
'college': 'MIT Pune/IIT Madras',
'fav_movies': ['Rockstar', 'ZNMD', 'Tamasha'],
'hobbies': ['meme making', 'cricket']
}

print(excer)

{'name': 'Divyansh Sethi', 'college': 'MIT Pune/IIT Madras', 'fav_movies': ['Rockstar', 'ZNMD', 'Tamasha'], 'hobbies': ['meme making', 'cricket']}

excer['fav_movies']

['Rockstar', 'ZNMD', 'Tamasha']

excer['fav_movies'][1]

'ZNMD'

# Dictionaries inside dictionaries

res_1 = {'Reservoir_name': 'A', 'State': 'Rajasthan', 'avg_phi': 0.34, 'avg_perm': 35, 'pay': 40}
res_2 = {'Reservoir_name': 'B', 'State': 'Maharsahtra', 'avg_phi': 0.25, 'avg_perm': 28, 'pay': 15}
res_3 = {'Reservoir_name': 'C', 'State': 'Assam', 'avg_phi': 0.29, 'avg_perm': 50, 'pay':25}

reservoir_india = { /
' ' {' i ' ' ' 'St t ' ' j th ' ' hi' 0 3 ' ' 3 ' ' 0}
2/2/2021 Python for O&G Lecture 43, 44, 45, 46, 47, 48, 49, 50, 51, 52 - Dictionaries - Colaboratory
'res_1' : {'Reservoir_name': 'A', 'State': 'Rajasthan', 'avg_phi': 0.34, 'avg_perm': 35, 'pay': 40},
'res_2' : {'Reservoir_name': 'B', 'State': 'Maharsahtra', 'avg_phi': 0.25, 'avg_perm': 28, 'pay': 15},
'res_3' : {'Reservoir_name': 'C', 'State': 'Assam', 'avg_phi': 0.29, 'avg_perm': 50, 'pay':25}
}

print(reservoir_india)

{'res_1': {'Reservoir_name': 'A', 'State': 'Rajasthan', 'avg_phi': 0.34, 'avg_perm': 35, 'pay': 40}, 'res_2': {'Reservoir_name': 'B', 'State': 'Maharsaht

reservoir_india['res_2']

{'Reservoir_name': 'B',
'State': 'Maharsahtra',
'avg_perm': 28,
'avg_phi': 0.25,
'pay': 15}

Lecture 45: Dictionaries - Part 3

Add data to dictionary

# create an empty list. Add data to it

a = ['hey', 'there']
print(a)

['hey', 'there']

a[1] = 'welcome'

print(a) /
2/2/2021 Python for O&G Lecture 43, 44, 45, 46, 47, 48, 49, 50, 51, 52 - Dictionaries - Colaboratory

['hey', 'welcome']

# create an empty dictionary. Add data to it

b = {}
print(b)

{}

b['name'] = 'Divyansh'

print(b)

{'name': 'Divyansh'}

b['college'] = 'IIT Madras/MIT Pune'

print(b)

{'name': 'Divyansh', 'college': 'IIT Madras/MIT Pune'}

# let's try to update the existing data in existing dictionary

b['name'] = 'Mohit'

print(b)

/
2/2/2021 Python for O&G Lecture 43, 44, 45, 46, 47, 48, 49, 50, 51, 52 - Dictionaries - Colaboratory
{'name': 'Mohit', 'college': 'IIT Madras/MIT Pune'}

b['hobby'] = 'meme making'

print(b)

{'name': 'Mohit', 'college': 'IIT Madras/MIT Pune', 'hobby': 'meme making'}

length function

len(b)

Lectre 46: Dictionaries - Part 4 - in keyword and .values() method

in keyword

# let's make a dictionary

drilling_data = {
'drill_bit' : 'PDC',
'KOP' : 1000,
'measurements': {'TVD' : 1500, 'MD': 2100},
'formations' : ['shale', 'limestone', 'sandstone']
}

print(drilling_data)

/
2/2/2021 Python for O&G Lecture 43, 44, 45, 46, 47, 48, 49, 50, 51, 52 - Dictionaries - Colaboratory

{'drill_bit': 'PDC', 'KOP': 1000, 'measurements': {'TVD': 1500, 'MD': 2100}, 'formations': ['shale', 'limestone', 'sandstone']}

if 'formations' in drilling_data:
print('present')
else:
print('Not present')

present

# now let's see if in keywords search for values ?

if 1000 in drilling_data:
print('present')
else:
print('Not present')

# in keyword only checks the key and not the value

Not present

# we have another way for this search

# .values() method

drilling_data.values()

dict_values(['PDC', 1000, {'TVD': 1500, 'MD': 2100}, ['shale', 'limestone', 'sandstone']])

if 'shale' in drilling_data.values():
print('present')
else:
print('Not present')

/
2/2/2021 Python for O&G Lecture 43, 44, 45, 46, 47, 48, 49, 50, 51, 52 - Dictionaries - Colaboratory
Not present

# if we want to get values printed without using the .values() method

# simply use loop

for i in drilling_data:
print(i)

drill_bit
KOP
measurements
formations

for i in drilling_data: # drill_bit, KOP, measurements, formations


print(drilling_data[i]) # drilling_data[drill_bit], drilling_data[KOP]

PDC
1000
{'TVD': 1500, 'MD': 2100}
['shale', 'limestone', 'sandstone']

Lecture 47: Dictionaries - Part 5

items method

# items method

drilling_data = {
'drill_bit' : 'PDC', /
2/2/2021 Python for O&G Lecture 43, 44, 45, 46, 47, 48, 49, 50, 51, 52 - Dictionaries - Colaboratory
'KOP' : 1000,
'measurements': {'TVD' : 1500, 'MD': 2100},
'formations' : ['shale', 'limestone', 'sandstone']
}

drilling_data.items()

dict_items([('drill_bit', 'PDC'), ('KOP', 1000), ('measurements', {'TVD': 1500, 'MD': 2100}), ('formations', ['shale', 'limestone', 'sandstone'])])

type(drilling_data.items())

dict_items

# [(), (), (), ()]

# () - (key, value)

# accessing both keys and values at once using drilling_data.items()

for i, j in drilling_data.items():
print(f'{i}: {j}')

drill_bit: PDC
KOP: 1000
measurements: {'TVD': 1500, 'MD': 2100}
formations: ['shale', 'limestone', 'sandstone']

pop method

# .pop(name of the key you want to remove)

print(drilling_data)

{'drill_bit': 'PDC', 'KOP': 1000, 'measurements': {'TVD': 1500, 'MD': 2100}, 'formations': ['shale', 'limestone', 'sandstone']}

drilling_data.pop('formations') /
2/2/2021 Python for O&G Lecture 43, 44, 45, 46, 47, 48, 49, 50, 51, 52 - Dictionaries - Colaboratory

['shale', 'limestone', 'sandstone']

print(drilling_data)

{'drill_bit': 'PDC', 'KOP': 1000, 'measurements': {'TVD': 1500, 'MD': 2100}}

drilling_data.pop()

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-75-c4bc7bbfb1b2> in <module>()
----> 1 drilling_data.pop()

TypeError: pop expected at least 1 arguments, got 0

SEARCH STACK OVERFLOW

# here in dictionary .pop() needs the name of key you want to delete, unlike lists where .pop() will remove the last element

updating the dictionary

print(drilling_data)

{'drill_bit': 'PDC', 'KOP': 1000, 'measurements': {'TVD': 1500, 'MD': 2100}}

# let's update our dictionary

# i have some more drilling data in form of a dictionary which contains field name and state name where drilling has been done

# Syntax: existing_dictionary.update(name of dictionary you want to add)

more_drill_data = {
'field' : 'Rageshwari',
'state' : 'Rajasthan'
}
/
2/2/2021 Python for O&G Lecture 43, 44, 45, 46, 47, 48, 49, 50, 51, 52 - Dictionaries - Colaboratory

print(drilling_data)

{'drill_bit': 'PDC', 'KOP': 1000, 'measurements': {'TVD': 1500, 'MD': 2100}}

drilling_data.update(more_drill_data)

print(drilling_data)

{'drill_bit': 'PDC', 'KOP': 1000, 'measurements': {'TVD': 1500, 'MD': 2100}, 'field': 'Rageshwari', 'state': 'Rajasthan'}

# let's update existing dictionary with an empty dictionary

drilling_data.update({})

print(drilling_data)

{'drill_bit': 'PDC', 'KOP': 1000, 'measurements': {'TVD': 1500, 'MD': 2100}, 'field': 'Rageshwari', 'state': 'Rajasthan'}

Lecture 48: Dictionaries - Part 6

fromkeys method

# to create dictionary where all the keys have same values

# traditional method

mohit = {
'fav_book' : 'no idea',
'fav_movie': 'no idea',
'fav_sports': 'no idea' /
}
2/2/2021 Python for O&G Lecture 43, 44, 45, 46, 47, 48, 49, 50, 51, 52 - Dictionaries - Colaboratory
}

print(mohit)

{'fav_book': 'no idea', 'fav_movie': 'no idea', 'fav_sports': 'no idea'}

# fromkeys method

# dict.fromkeys([all the keys], value) or # dict.fromkeys((all the keys), value)

mohit_new = dict.fromkeys(('fav_book', 'fav_movie', 'fav_sports', 'fav_tv', 'fav_dish'), 'no idea')

print(mohit_new)

{'fav_book': 'no idea', 'fav_movie': 'no idea', 'fav_sports': 'no idea', 'fav_tv': 'no idea', 'fav_dish': 'no idea'}

# dict.fromkeys((all the keys), value)

# another example: 1 to 4 all are natural numbers

nat = {
1: 'natural number',
2: 'natural number',
3: 'natural number',
4: 'natural number'
}

print(nat)
/
2/2/2021 Python for O&G Lecture 43, 44, 45, 46, 47, 48, 49, 50, 51, 52 - Dictionaries - Colaboratory
{1: 'natural number', 2: 'natural number', 3: 'natural number', 4: 'natural number'}

nat_new = dict.fromkeys(range(1, 5), 'natural number')


print(nat_new)

{1: 'natural number', 2: 'natural number', 3: 'natural number', 4: 'natural number'}

# also values can be a list or dictioanary or list as well

mohit_new = dict.fromkeys(('fav_book', 'fav_movie', 'fav_sports', 'fav_tv', 'fav_dish'), ['no idea', 'I dont care'])
print(mohit_new)

{'fav_book': ['no idea', 'I dont care'], 'fav_movie': ['no idea', 'I dont care'], 'fav_sports': ['no idea', 'I dont care'], 'fav_tv': ['no idea', 'I dont

get method

# a good way to access keys

mohit['fav_book']

'no idea'

mohit['fav_netflix']

---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-92-4a86dc8d3937> in <module>()
----> 1 mohit['fav_netflix']

KeyError: 'fav_netflix'

SEARCH STACK OVERFLOW

mohit.get('fav_book')

'no idea'
/
2/2/2021 Python for O&G Lecture 43, 44, 45, 46, 47, 48, 49, 50, 51, 52 - Dictionaries - Colaboratory

print(mohit.get('fav_netflix', 'This key is not present'))

This key is not present

drilling_data.get('production', 'Production data is not present')

'Production data is not present'

clear method

# it will empty our dictionary

mohit.clear()

print(mohit)

{}

Lecture 49 - Assignment 14

Problem Statement

# create an dictionary named prod_data_1 where only water cut is shown as 60. Use dict method to create this

# make another dictionary prod_dict_2 with all other details suh as a API_oil is 30, completion type is cased hole, zones = [zone 1, zone 2, zone 3]

# merge both the dictionaries using update method

# I need to get results printed in following format:

# water_cut: 60
/
# API oil : 30
2/2/2021 Python for O&G Lecture 43, 44, 45, 46, 47, 48, 49, 50, 51, 52 - Dictionaries - Colaboratory
# API_oil : 30
# completion_type : cased_hole
# zones = [zone 1, zone 2, zone 3]

prod_dict_1 = dict(water_cut = 60)


# print(prod_dict_1)

prod_dict_2 = {
'API_oil': 30,
'completion type': 'cased hole',
'zones' : ['zone 1', 'zone 2', 'zone 3']
}

prod_dict_2.update(prod_dict_1)

for i, j in prod_dict_2.items():
print(f'{i}: {j}')

API_oil: 30
completion type: cased hole
zones: ['zone 1', 'zone 2', 'zone 3']
water_cut: 60

Assignment 15

Assignment 15

# same problem again but this time ask computer to get input from users
/
# first create an empty dictionary
2/2/2021 Python for O&G Lecture 43, 44, 45, 46, 47, 48, 49, 50, 51, 52 - Dictionaries - Colaboratory
# first create an empty dictionary

# then ask all the inputs from users

# then add these inputs in the empty dictionary

# then print the results in same format you printed in Assignment 14

prod_data_3 = {}

water_cut = input('Enter the water cut in percentage: ')


API_gravity = input('What is the API of produced crude oil?: ')
completion_type = input('Is it a open hole or cased hole completion?: ')
zones_names = input('Name all the producing zones separated by , : ').split(" , ")

prod_data_3['water_cut'] = water_cut
prod_data_3['API_oil'] = API_gravity
prod_data_3['completion_type'] = completion_type
prod_data_3['zones'] = zones_names

print(prod_data_3)
/
2/2/2021 Python for O&G Lecture 43, 44, 45, 46, 47, 48, 49, 50, 51, 52 - Dictionaries - Colaboratory

Enter the water cut in percentage: 60


What is the API of produced crude oil?: 30
Is it a open hole or cased hole completion?: Cased hole
Name all the producing zones separated by , : Zone A, Zone B, Zone C
{'water_cut': '60', 'API_oil': '30', 'completion_type': 'Cased hole', 'zones': ['Zone A, Zone B, Zone C']}

for i, j in prod_data_3.items():
print(f'{i}:{j}')

water_cut:60
API_oil:30
completion_type:Cased hole
zones:['Zone A, Zone B, Zone C']

Lecture 52: Excersise problems

phi = [0.15, 0.2, 0.16, 0.15, 0.3, 0.45, 0.19, 0.3, 0.3, 0.2, 0.45, 0.39, 0.21]

# {0.15: 2, 0.2: 2, 0.16: 1......}

por_counter = {}

for i in phi:
por_counter[i] = phi.count(i) # {0.15: 2, 0.2: 2......}

print(por_counter)

/
2/2/2021 Python for O&G Lecture 43, 44, 45, 46, 47, 48, 49, 50, 51, 52 - Dictionaries - Colaboratory
{0.15: 2, 0.2: 2, 0.16: 1, 0.3: 3, 0.45: 2, 0.19: 1, 0.39: 1, 0.21: 1}

lith = ['sandstone', 'shale', 'sandstone', 'limestone', 'shale', 'sandstone', 'basement']

form_counter = {}

for i in lith:
form_counter[i] = lith.count(i)

print(form_counter)

{'sandstone': 3, 'shale': 2, 'limestone': 1, 'basement': 1}

# alternate method

def counter(a):
count_result = {}
for i in a:
count_result[i] = a.count(i)
return count_result

counter(lith)

{'basement': 1, 'limestone': 1, 'sandstone': 3, 'shale': 2}

counter(phi)

{0.15: 2, 0.16: 1, 0.19: 1, 0.2: 2, 0.21: 1, 0.3: 3, 0.39: 1, 0.45: 2}

/
2/2/2021 Python for O&G Lecture 43, 44, 45, 46, 47, 48, 49, 50, 51, 52 - Dictionaries - Colaboratory

You might also like