# Conventional Hello World in Jython print "Hello World!" def flatten_with_path(obj): """ Flatten a nested structure (dicts, lists, or variables) into a list of tuples with each tuple containing the value and its path in the original structure. """ flattened = [] # Helper function to recursively flatten with path def _flatten_helper(item, path): if isinstance(item, dict): for key, value in item.items(): _flatten_helper(value, path + [key]) elif isinstance(item, list): for index, value in enumerate(item): _flatten_helper(value, path + [index]) else: flattened.append((item, path)) # Call the helper function to start the recursion _flatten_helper(obj, []) return flattened nested_data = [ { "name": "John", "age": 30, "children": [ {"name": "Alice", "age": 5}, {"name": "Bob", "age": 8} ] }, ["apple", "banana", ["orange", "grape"]], "hello" ] print flatten_with_path(nested_data)
Standard input is empty
Hello World! [(30, [0, 'age']), ('John', [0, 'name']), (5, [0, 'children', 0, 'age']), ('Alice', [0, 'children', 0, 'name']), (8, [0, 'children', 1, 'age']), ('Bob', [0, 'children', 1, 'name']), ('apple', [1, 0]), ('banana', [1, 1]), ('orange', [1, 2, 0]), ('grape', [1, 2, 1]), ('hello', [2])]