问题
I'm running a recursive function on sublist to search the element check_value in the list once it finds it, it verifies whether other_value is the first item of the corresponding list and finally return the index.But current code is returning None.can anyone please support as I'm not having much understanding on recursive functions functioning on sublists.
def check_with_list(dd, check_value, other_value=None):
global new_index
for index, h in enumerate(dd):
if isinstance(h, list):
result = check_with_list(h, check_value)
if result is not None:
if other_value:
new = (index,) + result
if len(new) == 2:
if not dd[new[0]][0] == other_value:
result = None
else:
return (index,) + result
elif h == check_value:
return (index,)
# value not found
return None
dd = [
"gcc",
"fcc",
["scc", "jhh", "rrr"],
["www", "rrr", "rrr"],
"mmm",
["qwe", ["ree", "rer", "rrr"], "ere"]
]
dd = check_with_list(dd, "rrr", "ree")
print(dd)
回答1:
def check_with_list(dd, check_value, other_value=None):
global new_index
for index, h in enumerate(dd):
if isinstance(h, list):
result = check_with_list(h, check_value)
if result is not None:
if other_value:
new = (index,) + result
if len(new) == 2:
if dd[new[0]][0] == other_value:
result = None
else:
return (index,) + result
elif h == check_value:
return (index,)
# value not found
return None
dd = [
"gcc",
"fcc",
["scc", "jhh", "rrr"],
["www", "rrr", "rrr"],
"mmm",
["qwe", ["ree", "rrr", "rrr"], "ere"]
]
dd = check_with_list(dd, "rrr", "ree")
I have removed the not from the line below:
if not dd[new[0]][0] == other_value:
Everything else seems to be perfect. The code works and returns the index of the 1st occurrence of check_value in dd.
回答2:
I made this code that is quite similar to yours: Instead od using lists, I used dictionary to recursively mark where you can find value, then I made same with list + tuples.
import pprint
def check_with_list(dd, check_value):
my_indexes = {}
for index, h in enumerate(dd):
if isinstance(h, list):
result = check_with_list(h, check_value)
if result is not None:
my_indexes[index] = result
elif h == check_value:
my_indexes[index] = True
return my_indexes
def check_with_list_2(dd, check_value):
my_indexes = []
for index, h in enumerate(dd):
if isinstance(h, list):
result = check_with_list_2(h, check_value)
if result is not None:
my_indexes.append((index, result))
elif h == check_value:
my_indexes.append(index)
return my_indexes
dd = [
"aaa",
"bbb",
["bbb", "ccc", "bbb"],
["bbb", ["ccc", "aaa", "bbb"], "aaa"]
]
rv = check_with_list(dd, "bbb") # (1,2(0,2),3(0,1(2)))
pprint.pprint(rv)
rv = check_with_list_2(dd, "bbb") # (1,2(0,2),3(0,1(2)))
pprint.pprint(rv)
Returned values
{1: True, 2: {0: True, 2: True}, 3: {0: True, 1: {2: True}}}
[1, (2, [0, 2]), (3, [0, (1, [2])])]
回答3:
I believe you structured your logic incorrectly, looking for other_value after the opportunity has passed. Here's an alternate way to structure this:
def check_with_list(structure, check_value, other_value=None):
for index, item in enumerate(structure):
path = (index,)
if isinstance(item, list):
sub_path = check_with_list(item, check_value, other_value)
if sub_path is not None:
path += sub_path
if other_value and check_value in item:
if item[0] == other_value:
return path
else:
return path
elif item == check_value:
return path
return None # value not found
dd = [
"gcc",
"fcc",
["scc", "jhh", "rrr"],
["www", "rrr", "rrr"],
"mmm",
["qwe", ["ree", "rer", "rrr"], "ere"]
]
print(check_with_list(dd, "rrr", "ree"))
OUTPUT
> python3 test.py
(5, 1, 2)
>
来源:https://stackoverflow.com/questions/51946790/recursive-function-on-sublist-returning-none