Python Virtual Training For Arcesium - Module II - Day 4¶

Jun Jul 18-22, 2022 Vikrant Patil

All notes are available online at https://notes.pipal.in/2022/arcesium_finop_batch1/

Please accept the invitation that you have received in your email and login to

https://engage.pipal.in/

From there launch your jupyter lab. Create a notebook with name module2-day4.

Shutdown all older notebooks so that load on server reduces.

© Pipal Academy LLP

Problem Solving¶

Lets start with an example

  • Lets say we have a file which has numeric data in it. The file has multiple rows. Every row has some values seperated by comma. We want to find avearge of each row.

Paper work

  • What is the division of problem. Meaning, find out subproblems that we want to solve
  • Think of about data structures , how will store the data..list, dictionary, tuple,
  • combining the subproblems to get final solution
  • Think of data structure to store the results!
  1. There is file. That means we must read the file.
  2. For each row we have to do some processing
  3. Find average of each row
  4. we will use list to store a row
  5. we will use list to store the mean values
In [15]:
def average_rows_of_a_file(filepath):
    """Takes filepath as argument and returns list of average values for each row.
    """
    with open(filepath) as f:
        averages = []
        for line in f:
            row = extract_row(line)
            avg = mean(row)
            averages.append(avg)
            
        return averages
    
def extract_row(line):
    return [float(item) for item in line.strip().split(",")]# strip will make sure that \n at end is removed

def mean(row):
    return sum(row)/len(row)
In [16]:
mean([1, 2, 3, 4, 5])
Out[16]:
3.0
In [17]:
extract_row("23,45,4.6,56,34") # this testing is incomplete! it does not have \n at end
Out[17]:
[23.0, 45.0, 4.6, 56.0, 34.0]
In [18]:
extract_row("23,45,4.6,56,34\n")
Out[18]:
[23.0, 45.0, 4.6, 56.0, 34.0]
In [19]:
extract_row("23,45,4.6,56,34    \n")
Out[19]:
[23.0, 45.0, 4.6, 56.0, 34.0]
In [7]:
%%file numeric_values.txt
177.86307848009363,168.57970829672172,56.64667509485027,140.9453091498533,7.60588098031354,35.46585201959433,109.79299526934932,127.73481373344411,142.6569862535187,55.42226271294348
50.73110854833257,37.04770129180774,134.12303059207613,24.094715946706696,16.62709152318884,76.79158320138639,18.869039939685138,55.16544658992794,9.080542932129527,86.33584190856514
78.13874487140086,6.230686793261686,9.90129561402988,38.73432202944475,28.24102532900047,178.32675126964756,156.91807110256244,13.715926468541555,40.65402349504774,56.26057091162304
52.33390135982785,82.18187238357866,165.98732406823763,16.24849453464723,134.6940059960185,43.074586969351294,164.98109061612234,49.01998365034387,46.47274837268185,71.57223840684533
14.054024611303051,33.26061264583661,6.025038296009622,46.20617825321064,20.36303569589534,76.36142148522664,35.7247427244146,96.95015828855182,14.591002411820476,114.73930165632399
176.98106147487977,69.94975607322517,118.2574420888458,40.0226187414381,65.86533365890166,112.00582375498792,111.92750266225774,26.795263824303984,113.78920957808114,129.28326476993482
35.08017332225934,49.66626098662552,97.1770522711201,103.31251256854945,45.385340645578644,70.73296754777625,124.0599818449227,3.1152276402996506,189.621114798281,26.984391777865575
47.86347022018917,143.08376550789785,48.86114868739707,86.02253627471258,57.673544870935736,85.46964903214331,88.72038289528996,81.1572932565442,93.33330250009122,73.21497272029562
40.548227987191765,3.3788760416886054,45.81322292884595,18.41416684775437,0.4957688331691745,151.46049182954297,74.50089155354308,17.159975671658877,25.732237076201475,22.157154152625157
107.43073464586683,28.781131842076306,46.961173143962576,20.468161528556195,5.099354269264699,35.01542373037388,28.907504914509495,20.089203534798646,56.800621137020045,143.02788557795452
Writing numeric_values.txt
In [8]:
average_rows_of_a_file("numeric_values.txt")
Out[8]:
[102.27135619906822,
 50.8866102473806,
 60.712141788455995,
 82.65662463576545,
 45.827551606859274,
 96.48772766268561,
 74.51350234032782,
 80.54000659654967,
 39.96610129222215,
 49.25811943243832]
In [11]:
import os
os.getcwd()
Out[11]:
'/home/vikrant/trainings/2022/arcesium_finop_batch1'
In [12]:
average_rows_of_a_file('/home/vikrant/trainings/2022/arcesium_finop_batch1/numeric_values.txt')
Out[12]:
[102.27135619906822,
 50.8866102473806,
 60.712141788455995,
 82.65662463576545,
 45.827551606859274,
 96.48772766268561,
 74.51350234032782,
 80.54000659654967,
 39.96610129222215,
 49.25811943243832]
In [21]:
text = "    hello world    "
In [22]:
text.rstrip()
Out[22]:
'    hello world'
In [23]:
text.lstrip()
Out[23]:
'hello world    '

strip is used to remove trailing space

In [42]:
text.strip() # spaces which are not at end are not removed, only tailing spaces will be removed
Out[42]:
'hello world'

Working with dictionaries¶

In [26]:
stock = {"name":"IBM", "value":125, "high":127, "low":123}
In [27]:
stock
Out[27]:
{'name': 'IBM', 'value': 125, 'high': 127, 'low': 123}
In [28]:
stock['name']
Out[28]:
'IBM'
In [29]:
stock['value'] = 124
In [30]:
stock
Out[30]:
{'name': 'IBM', 'value': 124, 'high': 127, 'low': 123}
In [31]:
stock['quantity']
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
Input In [31], in <cell line: 1>()
----> 1 stock['quantity']

KeyError: 'quantity'
In [32]:
stock.get('name') # will work same as stock['name']
Out[32]:
'IBM'
In [34]:
stock.get('quantity') # this will not fail..no error 
In [35]:
print(stock.get('quantity'))
None
In [36]:
stock.get('quantity', 0)
Out[36]:
0
In [38]:
stock.get('quantity', 0) #if the key 'quantity` does not exist in stock dictionary then return the value for this key as 0
Out[38]:
0
In [39]:
stock.get('quantity') # if the second argument is not given, it will return None
In [41]:
stock.get('exchange', "NYC") # you can choose your own value
Out[41]:
'NYC'
In [43]:
stock
Out[43]:
{'name': 'IBM', 'value': 124, 'high': 127, 'low': 123}
In [44]:
stock.setdefault('quantity', 0) # it will return 0 as value but also set it
Out[44]:
0
In [45]:
stock
Out[45]:
{'name': 'IBM', 'value': 124, 'high': 127, 'low': 123, 'quantity': 0}
In [72]:
%%file stocks.csv
IBM,125,128,123
XYS,234,235,233,4
XYM,234,235,233
XYN,234,235,233,10
XYO,234,235,233,15
Overwriting stocks.csv
In [73]:
values = ["IBM",125,128,123]
keys = ["name", "value", "high", "low"]
In [74]:
# compare this with list comprehensions loop!
data = {} # it is similar except that there is dictionary here!
for key, value in zip(keys, values):
    data[key] = value
In [75]:
data
Out[75]:
{'name': 'IBM', 'value': 125, 'high': 128, 'low': 123}
In [76]:
{key:value for key,value in zip(keys, values)}
Out[76]:
{'name': 'IBM', 'value': 125, 'high': 128, 'low': 123}
In [77]:
# compare this with list comprehensions loop!
data = {} # it is similar except that there is dictionary here!
length = len(values)
for i in range(length):
    data[keys[i]] = values[i]
In [78]:
data
Out[78]:
{'name': 'IBM', 'value': 125, 'high': 128, 'low': 123}
In [82]:
def load_stocks_data(filename):
    keys = ["name", "value", "high", "low", 'quantity']
    with open(filename) as f:
        filedata = []
        for line in f:
            items = line.strip().split(",")
            values = [items[0]] + [float(v) for v in items[1:]] # 0th item is string so it is handled seperately
            data = {key:value for key,value in zip(keys, values)}
            filedata.append(data)
    return filedata
    
In [80]:
stocksdata = load_stocks_data("stocks.csv")
In [81]:
stocksdata[0]
Out[81]:
{'name': 'IBM', 'value': 125.0, 'high': 128.0, 'low': 123.0}
In [85]:
sum([stock['value'] for stock in stocksdata])     
Out[85]:
1061.0
In [86]:
sum([stock['quantity'] for stock in stocksdata])
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
Input In [86], in <cell line: 1>()
----> 1 sum([stock['quantity'] for stock in stocksdata])

Input In [86], in <listcomp>(.0)
----> 1 sum([stock['quantity'] for stock in stocksdata])

KeyError: 'quantity'
In [88]:
sum([stock.get('quantity', 0) for stock in stocksdata])
Out[88]:
29.0
In [90]:
sum([stock.get("quantity", 0)*stock['value'] for stock in stocksdata])
Out[90]:
6786.0

{key:value for zip(keys, values)}

In [91]:
dict(zip(keys, values))
Out[91]:
{'name': 'IBM', 'value': 125, 'high': 128, 'low': 123}
In [92]:
prices = [('IBM', 'Monday', 111.71436961893693),
            ('IBM', 'Tuesday', 141.21220022208635),
            ('IBM', 'Wednesday', 112.40571010053796),
            ('IBM', 'Thursday', 137.54133351926248),
            ('IBM', 'Friday', 140.25154281801224),
            ('MICROSOFT', 'Monday', 235.0403622499107),
            ('MICROSOFT', 'Tuesday', 225.0206535036475),
            ('MICROSOFT', 'Wednesday', 216.10342426936444),
            ('MICROSOFT', 'Thursday', 200.38038844494193),
            ('MICROSOFT', 'Friday', 235.80850482793264),
            ('APPLE', 'Monday', 321.49182055844256),
            ('APPLE', 'Tuesday', 340.63612771662815),
            ('APPLE', 'Wednesday', 303.9065277507285),
            ('APPLE', 'Thursday', 338.1350605764038),
            ('APPLE', 'Friday', 318.3912296144338)]
In [127]:
def weekly_average(prices, symbol):
    return mean([price for name, day, price in prices if name==symbol])
In [130]:
weekly_average(prices, "MICROSOFT")
Out[130]:
222.47066665915946
In [131]:
symbols = set([symbol for symbol,day, value in prices])
In [132]:
symbols # it is a set .. it does not have key:value pair... it is not a dictonary
Out[132]:
{'APPLE', 'IBM', 'MICROSOFT'}

in set uniqueness is gauranteed and not order. in list order is gauranteed, it is not necessariliy unique

In [133]:
symbols.add("APPLE") # uniqueness
In [134]:
symbols
Out[134]:
{'APPLE', 'IBM', 'MICROSOFT'}
In [135]:
ones = [1, 1, 1, 1]
In [136]:
ones.append(1)
In [137]:
ones
Out[137]:
[1, 1, 1, 1, 1]
In [138]:
help(symbols.pop) # it will remove arbitrary...not last
Help on built-in function pop:

pop(...) method of builtins.set instance
    Remove and return an arbitrary set element.
    Raises KeyError if the set is empty.

In [139]:
s = {1, 2, 3, 4, 5, 5}
In [140]:
s
Out[140]:
{1, 2, 3, 4, 5}
In [141]:
for i in range(len(s)):
    print(s.pop())
1
2
3
4
5
In [142]:
s
Out[142]:
set()
In [143]:
s = {1, 2, 3, 4}
In [144]:
s.remove(1)
In [145]:
s
Out[145]:
{2, 3, 4}
In [146]:
nums = [1, 2, 3, 4]
In [147]:
nums.remove(2)
In [148]:
nums
Out[148]:
[1, 3, 4]
In [149]:
symbols
Out[149]:
{'APPLE', 'IBM', 'MICROSOFT'}
In [150]:
weekly_averages = {symbol:weekly_average(prices, symbol) for symbol in symbols}
In [151]:
weekly_averages
Out[151]:
{'APPLE': 324.51215324332736,
 'MICROSOFT': 222.47066665915946,
 'IBM': 128.62503125576717}
In [154]:
stocks=  {'APPLE': 700.5,
          'IBM': 300.1,
          'AT&T': 355.7,
          'AGILENT': 600.3}
In [155]:
[i for i in range(100) if i%7==0 or i%11==0]
Out[155]:
[0,
 7,
 11,
 14,
 21,
 22,
 28,
 33,
 35,
 42,
 44,
 49,
 55,
 56,
 63,
 66,
 70,
 77,
 84,
 88,
 91,
 98,
 99]
In [156]:
stocks=  {'APPLE': 700.5,
          'IBM': 300.1,
          'AT&T': 355.7,
          'AGILENT': 600.3}
In [158]:
{k:v for k,v in stocks.items() if v > 300}
Out[158]:
{'APPLE': 700.5, 'IBM': 300.1, 'AT&T': 355.7, 'AGILENT': 600.3}
In [159]:
symbols
Out[159]:
{'APPLE', 'IBM', 'MICROSOFT'}
In [160]:
{k:v for k,v in stocks.items() if k in symbols}
Out[160]:
{'APPLE': 700.5, 'IBM': 300.1}

Problem

  • There is a file with some text data in it. We want to find frequncies of each word from the file
In [166]:
def generate_test_data(filename):
    words = ["one","two","three","four","five","six","seven","eight","nine","ten"]
    freq = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    with open(filename, "w") as file:
        for w,f in zip(words, freq):
            file.write(",".join([w]*f))
            file.write("\n")
        
In [167]:
[1,2]*2
Out[167]:
[1, 2, 1, 2]
In [168]:
["one"]*2
Out[168]:
['one', 'one']
In [169]:
generate_test_data("words.csv")
In [170]:
!python cat.py words.csv
one
two,two
three,three,three
four,four,four,four
five,five,five,five,five
six,six,six,six,six,six
seven,seven,seven,seven,seven,seven,seven
eight,eight,eight,eight,eight,eight,eight,eight
nine,nine,nine,nine,nine,nine,nine,nine,nine
ten,ten,ten,ten,ten,ten,ten,ten,ten,ten

Problem

  • There is a file with some text data in it. We want to find frequncies of each word from the file

Paperwork

  1. Read the file
  2. extract Words
  3. create a set of all words
  4. go over set one by one and count
In [171]:
ones.count(1)
Out[171]:
5
In [173]:
def get_all_words(filename):
    with open(filename) as f:
        
        return f.read().split()
    
def word_freq(filename):
    words = get_all_words(filename)
    
    unique_words = set(words)
    wordfreq = {}
    for w in unique_words:
        wordfreq[w] = words.count(w)
    return wordfreq
In [174]:
word_freq("words.csv")
Out[174]:
{'two,two': 1,
 'four,four,four,four': 1,
 'eight,eight,eight,eight,eight,eight,eight,eight': 1,
 'ten,ten,ten,ten,ten,ten,ten,ten,ten,ten': 1,
 'three,three,three': 1,
 'six,six,six,six,six,six': 1,
 'nine,nine,nine,nine,nine,nine,nine,nine,nine': 1,
 'seven,seven,seven,seven,seven,seven,seven': 1,
 'five,five,five,five,five': 1,
 'one': 1}
In [175]:
get_all_words("words.csv")
Out[175]:
['one',
 'two,two',
 'three,three,three',
 'four,four,four,four',
 'five,five,five,five,five',
 'six,six,six,six,six,six',
 'seven,seven,seven,seven,seven,seven,seven',
 'eight,eight,eight,eight,eight,eight,eight,eight',
 'nine,nine,nine,nine,nine,nine,nine,nine,nine',
 'ten,ten,ten,ten,ten,ten,ten,ten,ten,ten']
In [176]:
def get_all_words(filename):
    with open(filename) as f:
        words = []
        for line in f:
            words.extend(line.strip().split(","))
        return words
    
def word_freq(filename):
    words = get_all_words(filename)
    
    unique_words = set(words)
    wordfreq = {}
    for w in unique_words:
        wordfreq[w] = words.count(w)
    return wordfreq
In [178]:
word_freq("words.csv")
Out[178]:
{'five': 5,
 'ten': 10,
 'six': 6,
 'seven': 7,
 'two': 2,
 'four': 4,
 'three': 3,
 'nine': 9,
 'eight': 8,
 'one': 1}
In [179]:
    
def word_freq1(filename):
    words = get_all_words(filename)
    
    wordfreq = {}
    for w in words:
        if w not in wordfreq:
            wordfreq[w] = 1
        else:
            wordfreq[w] += 1
    return wordfreq
In [180]:
word_freq1("words.csv")
Out[180]:
{'one': 1,
 'two': 2,
 'three': 3,
 'four': 4,
 'five': 5,
 'six': 6,
 'seven': 7,
 'eight': 8,
 'nine': 9,
 'ten': 10}
In [181]:
# wordfreq[w]-> get/setdefault

def word_freq2(filename):
    words = get_all_words(filename)
    
    wordfreq = {}
    for w in words:
        wordfreq[w] = wordfreq.get(w, 0) + 1
    return wordfreq
In [182]:
word_freq2("words.csv")
Out[182]:
{'one': 1,
 'two': 2,
 'three': 3,
 'four': 4,
 'five': 5,
 'six': 6,
 'seven': 7,
 'eight': 8,
 'nine': 9,
 'ten': 10}

string formating¶

In [183]:
for i in range(1, 11):
    print(i, i**2, i**3)
1 1 1
2 4 8
3 9 27
4 16 64
5 25 125
6 36 216
7 49 343
8 64 512
9 81 729
10 100 1000
In [185]:
"hello {name}!".format(name="vikrant")
Out[185]:
'hello vikrant!'
In [186]:
"{x} {y} {z}".format(x=1, y=2, z=3)
Out[186]:
'1 2 3'
In [187]:
x, y,z = 1, 2, 3
f"{x}, {y}, {z}"
Out[187]:
'1, 2, 3'
In [188]:
"some string one {}, {}, {} thsese values".format(1, 2, 3)
Out[188]:
'some string one 1, 2, 3 thsese values'
In [190]:
"some string one {0}, {2}, {1} thsese values".format("ABC", "XYZ", "MNO")
Out[190]:
'some string one ABC, MNO, XYZ thsese values'
In [191]:
stocksdata
Out[191]:
[{'name': 'IBM', 'value': 125.0, 'high': 128.0, 'low': 123.0},
 {'name': 'XYS', 'value': 234.0, 'high': 235.0, 'low': 233.0, 'quantity': 4.0},
 {'name': 'XYM', 'value': 234.0, 'high': 235.0, 'low': 233.0},
 {'name': 'XYN',
  'value': 234.0,
  'high': 235.0,
  'low': 233.0,
  'quantity': 10.0},
 {'name': 'XYO',
  'value': 234.0,
  'high': 235.0,
  'low': 233.0,
  'quantity': 15.0}]
In [195]:
for d in stocksdata:
    v = list(d.values())
    print("{},{},{},{}".format(v[0], v[1], v[2], v[3]))
IBM,125.0,128.0,123.0
XYS,234.0,235.0,233.0
XYM,234.0,235.0,233.0
XYN,234.0,235.0,233.0
XYO,234.0,235.0,233.0
In [196]:
for i in range(1, 11):
    print("{} {} {}".format(i, i**2, i**3))
1 1 1
2 4 8
3 9 27
4 16 64
5 25 125
6 36 216
7 49 343
8 64 512
9 81 729
10 100 1000
In [197]:
for i in range(1, 11):
    print("{c0} {c1} {c2}".format(c0=i, c1=i**2, c2=i**3))
1 1 1
2 4 8
3 9 27
4 16 64
5 25 125
6 36 216
7 49 343
8 64 512
9 81 729
10 100 1000
In [198]:
for i in range(1, 11):
    print("{c0:2d} {c1:3d} {c2:4d}".format(c0=i, c1=i**2, c2=i**3))
 1   1    1
 2   4    8
 3   9   27
 4  16   64
 5  25  125
 6  36  216
 7  49  343
 8  64  512
 9  81  729
10 100 1000
In [199]:
word_freq("words.csv")
Out[199]:
{'five': 5,
 'ten': 10,
 'six': 6,
 'seven': 7,
 'two': 2,
 'four': 4,
 'three': 3,
 'nine': 9,
 'eight': 8,
 'one': 1}
In [200]:
freq = word_freq("words.csv")
In [201]:
freq
Out[201]:
{'five': 5,
 'ten': 10,
 'six': 6,
 'seven': 7,
 'two': 2,
 'four': 4,
 'three': 3,
 'nine': 9,
 'eight': 8,
 'one': 1}
In [204]:
def get_freq(r):
    return r[1]
for w, f in sorted(freq.items(), key=get_freq):
    print(w, f)
one 1
two 2
three 3
four 4
five 5
six 6
seven 7
eight 8
nine 9
ten 10
In [205]:
for w, f in sorted(freq.items(), key=get_freq):
    print(w.rjust(5), f)
  one 1
  two 2
three 3
 four 4
 five 5
  six 6
seven 7
eight 8
 nine 9
  ten 10
In [207]:
for w, f in sorted(freq.items(), key=get_freq):
    print(w.rjust(5), "{:2d}".format(f), "*"*f)
  one  1 *
  two  2 **
three  3 ***
 four  4 ****
 five  5 *****
  six  6 ******
seven  7 *******
eight  8 ********
 nine  9 *********
  ten 10 **********
In [208]:
for w, f in sorted(freq.items()):
    print(w.rjust(5), "{:2d}".format(f), "*"*f)
eight  8 ********
 five  5 *****
 four  4 ****
 nine  9 *********
  one  1 *
seven  7 *******
  six  6 ******
  ten 10 **********
three  3 ***
  two  2 **
In [209]:
list(freq.items())
Out[209]:
[('five', 5),
 ('ten', 10),
 ('six', 6),
 ('seven', 7),
 ('two', 2),
 ('four', 4),
 ('three', 3),
 ('nine', 9),
 ('eight', 8),
 ('one', 1)]
In [211]:
for w, f in sorted(freq.items(), key=get_freq, reverse=True):
    print(w.rjust(5), "{:2d}".format(f), "*"*f)
  ten 10 **********
 nine  9 *********
eight  8 ********
seven  7 *******
  six  6 ******
 five  5 *****
 four  4 ****
three  3 ***
  two  2 **
  one  1 *
In [212]:
freq
Out[212]:
{'five': 5,
 'ten': 10,
 'six': 6,
 'seven': 7,
 'two': 2,
 'four': 4,
 'three': 3,
 'nine': 9,
 'eight': 8,
 'one': 1}
In [213]:
d = {'five': 5,
 'ten': 10,
 'six': 6,
 'seven': 7}
In [214]:
d
Out[214]:
{'five': 5, 'ten': 10, 'six': 6, 'seven': 7}
In [215]:
freq
Out[215]:
{'five': 5,
 'ten': 10,
 'six': 6,
 'seven': 7,
 'two': 2,
 'four': 4,
 'three': 3,
 'nine': 9,
 'eight': 8,
 'one': 1}

Finding common keys from two dictionaries¶

In [217]:
d.keys() & freq.keys()
Out[217]:
{'five', 'seven', 'six', 'ten'}

COUNTIFS¶

In [238]:
import operator as op

def countifs(criterio_list, condstr):
    value = int("".join([c for c in condstr if c.isdigit()]))
    cond = "".join([c for c in condstr if not c.isdigit()])
    
    conds = [">", ">=", "<", "<=", "", "<>"]
    funcs = [op.gt, op.ge, op.lt, op.le, op.eq, op.ne]
    funcmap = dict(zip(conds, funcs))
    
    return len([item for item in criterio_list if funcmap[cond](item, value)])
    

countifs([10, 20, 10, 20, 30, 40, 50, 50, 60, 60], "<>60")
Out[238]:
8
In [226]:
countifs([10, 20, 10, 20, 30, 40, 50, 50, 60, 60], ">60")
Out[226]:
0
In [227]:
countifs([10, 20, 10, 20, 30, 40, 50, 50, 60, 60], "<60")
Out[227]:
8
In [228]:
countifs([10, 20, 10, 20, 30, 40, 50, 50, 60, 60], "<=30")
Out[228]:
5
In [218]:
condstr = "<>40"
int("".join([c for c in condstr if c.isdigit()]))
Out[218]:
40
In [220]:
"".join([c for c in condstr if not c.isdigit()])
Out[220]:
'<>'
In [222]:
op.ge
Out[222]:
<function _operator.ge(a, b, /)>
In [223]:
def greaterthan(x, y):
    return x > y
In [232]:
conds = [">", ">=", "<", "<=", "", "<>"]
funcs = [op.gt, op.ge, op.lt, op.le, op.eq, op.ne]
funcmap = dict(zip(conds, funcs))
In [233]:
funcmap
Out[233]:
{'>': <function _operator.gt(a, b, /)>,
 '>=': <function _operator.ge(a, b, /)>,
 '<': <function _operator.lt(a, b, /)>,
 '<=': <function _operator.le(a, b, /)>,
 '': <function _operator.eq(a, b, /)>,
 '<>': <function _operator.ne(a, b, /)>}
In [234]:
funcmap[">"]
Out[234]:
<function _operator.gt(a, b, /)>
In [235]:
funcmap[">"](2, 1)
Out[235]:
True
In [236]:
funcmap[">"](2, 5)
Out[236]:
False
In [ ]: