Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Defining Two Functions at Once?

As I learn Python, I’m wondering if possible to define two functions in a single function?

I’ve made an attempt with the following code/MWE to simulate cleaning a dataset of dates:

#COMBINED FUNCTION

def combined_function(entry):
    for char in bad_chars:
        date = entry.replace(char,"")
    if "-" in date:
        split_date = date.split("-")
        date_one = int(split_date[0])
        date_two = int(split_date[1])
        date = (date_one+date_two)/2
        date = round(date)
    else:
        date = int(date)
    return(date)

Unfortunately, when I attempt to run the new function on test_data it does not appear to be yielding the correct results,

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

test_data = ["1912", "1929", "1913-1923",
             "(1951)", "1994", "1934",
             "c. 1915", "1995", "c. 1912",
             "(1988)", "2002", "1957-1959",
             "c. 1955.", "c. 1970's", 
             "C. 1990-1999"]
bad_chars = ["(",")","c","C",".","s","'", " "]

revised_date = []

for x in test_data:
    date = combined_function(x)
    revised_date.append(date)
    
print(revised_date)

Instead I receive the following traceback error,

ValueError: invalid literal for int() with base 10: '(1951)'

Looks like an issue in removing the bad_chars?

Any thoughts/suggestions on where I went wrong? Thanks!

>Solution :

Just change your function parameter to date, and replace the .replace statement with date = date.replace(char,"").

Function should look like this:

def combined_function(date):
    for char in bad_chars:
        date = date.replace(char,"")
    if "-" in date:
        split_date = date.split("-")
        date_one = int(split_date[0])
        date_two = int(split_date[1])
        date = (date_one+date_two)/2
        date = round(date)
    else:
        date = int(date)
    return(date)

Why is this happening??

Simply because date is being replaced with entry in every iteration which remains unchanged by .replace statement.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading