Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def divide(x, y):
- return x / y
- def multiply(x, y):
- return x * y
- def listolists2list_yo(func, *x):
- '''
- Take a list of function(s) and a list(s) of lists possibly containing dict(s), and
- return function(*list) for each list in list for each function in list of functions.
- EXAMPLE: listolists2list_yo(["multiply", "divide"], [[1, 2], [3, 4]])
- RETURNS: [[2, 12], [0.5, 0.75]]
- TODO: Allow arbitrary nesting of lists within lists.
- '''
- result = []
- for f in func:
- presult = []
- for thing in x:
- if type(thing) == list:
- for item in thing:
- if isinstance(item, list):
- eval(f"presult.append({f}(*item))") # Ye boi
- elif isinstance(item, dict):
- eval(f"presult.append({f}(item.get('x'), item.get('y')))")
- else:
- presult.append("ERROR") # Sick graceful recovery
- elif type(thing) == dict:
- eval(f"presult.append({f}(thing.get('x'), thing.get('y')))")
- else:
- presult.append("ERROR")
- result.append(presult)
- return result
- dawg = [[10, 5], [8, 4]] # Everything in dawg & dawg2:
- dawg2 = [[6, 3], [4,2]] # x / y = 2
- catdic = {'y':2, 'x':1} # "Backwards", 1 / 2 = .5
- catdic2 = {'y':4, 'x':3} # 3 / 4 = .75
- listdic = [catdic, catdic2] # List of dicts fuck ye
- uberlist = [["divide", "multiply"], dawg, dawg2, catdic, listdic, (3,2)]
- for line in listolists2list_yo(*uberlist):
- print(line)
- '''OUTPUT
- [2.0, 2.0, 2.0, 2.0, 0.5, 0.5, 0.75, 'ERROR']
- [50, 32, 18, 8, 2, 2, 12, 'ERROR']
- '''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement