Created
June 17, 2017 13:27
-
-
Save Pk13055/d6e316ed463088b7a5c0b04ad55b344e to your computer and use it in GitHub Desktop.
List manipulator scripts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# this function recursively performs an operation on nested lists element-wise | |
import operator | |
def list_recur(l1, l2, op = operator.add): | |
if not l1: | |
return type(l1)([]) | |
elif isinstance(l1[0], type(l1)): | |
return type(l1)([list_recur(l1[0], l2[0], op)]) + list_recur(l1[1:],l2[1:], op) | |
else: | |
return type(l1)([op(l1[0], l2[0])]) + list_recur(l1[1:], l2[1:], op) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment