I am making a 'cutest pet bracket' for my company while on quarantine, and I am confused as how to update the competitors of subsequent rounds after one round is played (somebody wins).
Say my winner is 3 and the round after is:
[[3, 14], [6, 11]]
and the round after is:
[[2, [7, 10]], [[3, 14], [6, 11]]]
I want to replace the children nodes with the winner in them to reflect the fact that now just the winner remains:
[[3, 14], [6, 11]] --> [3, [6, 11]]
[[2, [7, 10]], [[3, 14], [6, 11]]] --> [[2, [7, 10]], [3, [6, 11]]]
I came up with some code but it doesn't work properly
[[3, 14], [6, 11]] --> [[3], [6, 11]] (close, but no cigar)
[[2, [7, 10]], [[3, 14], [6, 11]]] --> [[2, [7, 10]], [[3, 14], [6, 11]]] (no change)
def traverse(traversed_element_of_list_of_lists, winner):
rebuild = []
if isinstance(traversed_element_of_list_of_lists, list):
for value in traversed_element_of_list_of_lists:
if isinstance(value, int):
continue#rebuild.append(value)
if winner in value:
rebuild.append([winner])
else:
rebuild.append(value)
traverse(value, winner)
return rebuild
There are plenty of answers on SO about replacing item in nested list of defined shape, and there are answers about how to traverse and flatten a nested list of arbitrary depth, but I cannot seem to find anything about how to replace an item in a list of arbitrary depth.
Thank you for help!