my code so far:
import statistics
def nested_median(nest, templist=[]):
if not nest:
pass
elif isinstance (nest[0], list):
nested_median(nest[0]), nested_median(nest[1:])
elif isinstance (nest[0], int):
templist.append(nest[0])
nested_median(nest[1:])
return statistics.median(templist)
else:
nested_median(nest[1:])
I am trying to write a program that takes a arbitrarily nested lists as input and returns the median of all the integers in the list and/or sublists while ignoring all other elements. So for instance:
nested_median([3,2,"Hello",[1,5],("Hello", "world"),5,9.3]) == 3
I have come up with a solution above using a global variable, but this only works for one function call since templist dosen't get cleared.I have two questions:
How would i go about clearing my global variable between function calls?
How would i implement this without using a global variable?