You are on page 1of 9

2/2/2021 Python for O&G Lecture 53, 54, 55: Sets - Colaboratory

Python for Oil & Gas

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

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

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

Lecture 53: Sets - Part 1

Sets

Stores unordered collection of unique items


Unordered means, there is no indexing in sets as well (like dictionaries)
1 item can be stored only once. No repetition. Hence ever item is unique
Syntax: {} -> just input the item. No key value pairs

# {key: value} - Dictionary /


2/2/2021 Python for O&G Lecture 53, 54, 55: Sets - Colaboratory

# {el1,el2} - set

alpha = {'a', 'b', 'c', 'd', 'a', 'b'}

# print(alpha)

type(alpha)

set

# unique items

# let's try to do indexing

alpha = {'a', 'b', 'c', 'd', 'a', 'b'}

alpha[1]

/
2/2/2021 Python for O&G Lecture 53, 54, 55: Sets - Colaboratory

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-6-da6fbe6453e6> in <module>()
7
8
----> 9 alpha[1]
10
11

TypeError: 'set' object does not support indexing

SEARCH STACK OVERFLOW

# let's say we have list of all the lithology types encountered in an area where various wells have been drilled

lith = ['Sandstone', 'Shale', 'Sandstone', 'Limestone', 'Shale', 'Limestone', 'Coal Bed Seam', 'Sandstone', 'Coal Bed Seam', 'Limestone', 'Dolomite', 'Basement

#I want to create list of unique lithologies

lith_new = set(lith)

print(lith_new)

type(lith_new)

{'Basement', 'Coal Bed Seam', 'Sandstone', 'Dolomite', 'Limestone', 'Shale'}


set

lith_2 = list(lith_new)

print(lith_2)
/
2/2/2021 Python for O&G Lecture 53, 54, 55: Sets - Colaboratory

type(lith_2)

['Basement', 'Coal Bed Seam', 'Sandstone', 'Dolomite', 'Limestone', 'Shale']


list

# we can store all the data types

abc = {24, 41, 20.6, 'python', 5+8j, True}


type(abc)

set

We CANNOT add list, sets, dictionary inside a set.

We CAN add tuple inside a set

xyz = {14, 56, 'python', (1, 2, 3)}


type(xyz)

set

Lecture 54: Sets - Part 2

Methods in sets

# add items to a set

stages_well = {'Exploration', 'Appraisal'} /


2/2/2021 Python for O&G Lecture 53, 54, 55: Sets - Colaboratory

add method

# syntax - set_name.add(whatever you want to add)

stages_well.add('Development')

print(stages_well)

{'Development', 'Appraisal', 'Exploration'}

# try to add 2 items using add method

a = {1, 2, 5}

a.add(3, 4)

print(a)

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-38-4264641dce06> in <module>()
4 a = {1, 2, 5}
5
----> 6 a.add(3, 4)
7
8 print(a)

TypeError: add() takes exactly one argument (2 given)

SEARCH STACK OVERFLOW

/
2/2/2021 Python for O&G Lecture 53, 54, 55: Sets - Colaboratory

update method

print(a)

{1, 2, 5}

a.update([3, 4])
print(a)

{1, 2, 3, 4, 5}

a.update((6, 7, 8, 9))

print(a)

{1, 2, 3, 4, 5, 6, 7, 8, 9}

a.update('abc')

print(a)

a
b
c

Removing items from set

# remove method

phi = {0.15, 0.25, 0.3, 0.4, 0.3, 'b'}

phi.remove('b')
/
2/2/2021 Python for O&G Lecture 53, 54, 55: Sets - Colaboratory

print(phi)

{0.15, 0.25, 0.3, 0.4}

phi.add('c')

print(phi)

{0.15, 0.25, 0.3, 0.4, 'c'}

# discard method

phi.discard('c')

print(phi)

{0.15, 0.25, 0.3, 0.4}

phi.discard(555)

clear method

phi.clear()

print(phi)

set()

copy method

/
2/2/2021 Python for O&G Lecture 53, 54, 55: Sets - Colaboratory

Lecture 55: Sets - Part 3

Set Theory (union and intersection)

l = {1, 2, 3, 4, 5}

m = {456, 1254, 56, 2, 5}

# union

n = l | m

print(n)

{1, 2, 3, 4, 5, 1254, 456, 56}

# intersection

k = l & m

print(k)

{2, 5}

Assignment 16

# I have drilled two wells in an area

# In well 1 I encountered formations shale, sandstone, limestone


/
2/2/2021 Python for O&G Lecture 53, 54, 55: Sets - Colaboratory
# In well 2 I encounterd formations sandstone and salt dome

# I want two sets as result where the 1st set contains all the formations I have encountered in this area, 2nd set contains formations which are common to both

You might also like