Professional Documents
Culture Documents
Python For Oil and Gas: Code Text
Python For Oil and Gas: Code Text
44, 45, 46, 47, 48, 49, 50, 51, 52 - Dictionaries - Colaboratory
Website - https://petroleumfromscratchin.wordpress.com/
LinkedIn - https://www.linkedin.com/company/petroleum-from-scratch
YouTube - https://www.youtube.com/channel/UC_lT10npISN5V32HDLAklsw
Code Text
Can store naything inside dictionaries --> numbers, lists, strings, dictioanries etc
# 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
# a, b, c
print(phi_list)
print(phi_dict)
/
2/2/2021 Python for O&G Lecture 43, 44, 45, 46, 47, 48, 49, 50, 51, 52 - Dictionaries - Colaboratory
print(phi_dict_new)
# Syntax: Tuples inside a list and 2 items in tuple i.e. one key and second is value
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}
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']
excer['fav_movies'][1]
'ZNMD'
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}
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']
b = {}
print(b)
{}
b['name'] = 'Divyansh'
print(b)
{'name': 'Divyansh'}
print(b)
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'}
print(b)
length function
len(b)
in keyword
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
if 1000 in drilling_data:
print('present')
else:
print('Not present')
Not present
# .values() method
drilling_data.values()
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
for i in drilling_data:
print(i)
drill_bit
KOP
measurements
formations
PDC
1000
{'TVD': 1500, 'MD': 2100}
['shale', 'limestone', 'sandstone']
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)
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
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
print(drilling_data)
drilling_data.pop()
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-75-c4bc7bbfb1b2> in <module>()
----> 1 drilling_data.pop()
# here in dictionary .pop() needs the name of key you want to delete, unlike lists where .pop() will remove the last element
print(drilling_data)
# i have some more drilling data in form of a dictionary which contains field name and state name where drilling has been done
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)
drilling_data.update(more_drill_data)
print(drilling_data)
{'drill_bit': 'PDC', 'KOP': 1000, 'measurements': {'TVD': 1500, 'MD': 2100}, 'field': 'Rageshwari', 'state': 'Rajasthan'}
drilling_data.update({})
print(drilling_data)
{'drill_bit': 'PDC', 'KOP': 1000, 'measurements': {'TVD': 1500, 'MD': 2100}, 'field': 'Rageshwari', 'state': 'Rajasthan'}
fromkeys method
# 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)
# fromkeys method
print(mohit_new)
{'fav_book': 'no idea', 'fav_movie': 'no idea', 'fav_sports': 'no idea', 'fav_tv': 'no idea', 'fav_dish': 'no idea'}
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'}
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
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'
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
clear method
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]
# 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_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
prod_data_3 = {}
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
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']
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]
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}
form_counter = {}
for i in lith:
form_counter[i] = lith.count(i)
print(form_counter)
# alternate method
def counter(a):
count_result = {}
for i in a:
count_result[i] = a.count(i)
return count_result
counter(lith)
counter(phi)
/
2/2/2021 Python for O&G Lecture 43, 44, 45, 46, 47, 48, 49, 50, 51, 52 - Dictionaries - Colaboratory