python - Multi-mapping a value while saving intermediate values -
i'd map value through dictionary multiple times, , record intermediate values. since list.append()
doesn't return value, below best i've been able come with. there better way in python, perhaps using list comprehension or recursion?
def append(v, seq): seq.append(v) return v def multimap(value, f, count): result = [] _ in range(count): value = append(f[value], result) return result print( multimap('a', {'a': 'b', 'b': 'c', 'c': 'd', 'd': 'a'}, 4) )
output:
['b', 'c', 'd', 'a']
instead of having deal lists, can use generator:
def multimap(value, f, count): _ in range(count): value = f[value] yield value print(list(multimap('a', {'a': 'b', 'b': 'c', 'c': 'd', 'd': 'a'}, 4)))