중첩된 여러 자료구조의 Element에 Func mapping하는법

minsing-jin·2025년 5월 4일
0

nest_map을 만드세유

def nested_map(struct, map_fn):
	if isinstance(struct, tuple):
		return tuple(nested_map(x, map_fn) for x in struct)
	if isinstance(struct, list):
		return [nested_map(x, map_fn) for x in struct]
	if isinstance(struct, dict):
		return {k: nested_map(v, map_fn) for k, v in struct.items()}
	return map_fn(struct)
profile
why not? 정신으로 맨땅에 헤딩하고 있는 코린이

0개의 댓글