[英]Given nested list element, find the one level back list value
Say i have List=[[[['a','b'],['c','d'],['e','f']],[['1','2'],['3','4'],['5','6']]],[[['a','b'],['c','d'],['e','f']],[['1','2'],['3','4'],['5','6']]]]
i know that if i call List[0][0]
i will get [['a', 'b'], ['c', 'd'], ['e', 'f']]
, and so on. 假设我有
List=[[[['a','b'],['c','d'],['e','f']],[['1','2'],['3','4'],['5','6']]],[[['a','b'],['c','d'],['e','f']],[['1','2'],['3','4'],['5','6']]]]
我知道如果我调用List[0][0]
我将得到[['a', 'b'], ['c', 'd'], ['e', 'f']]
,等等。
Is there any built-in or external python function(suppose its func(a)
) or way to get the nested list element a
one level back? 是否有任何内置或外置Python函数(假设其
func(a)
或方式来获得嵌套列表元素a
一个级别了?
So if i call func(List[0][1])
those function will return List[0]
or when i call func(List[1][0][1])
those function will return List[1][0]
but if i call func(List)
it will return List
since it's already at the root. 因此,如果我调用
func(List[0][1])
这些函数将返回List[0]
或当我调用func(List[1][0][1])
这些函数将返回List[1][0]
但是如果我调用func(List)
它将返回List
因为它已经在根。 I've been searching for this kind of problem for hours but still couldn't find the solution. 我一直在寻找这种问题几个小时,但仍然找不到解决方案。
You can use the following recursive function: 您可以使用以下递归函数:
def get_parent_list(the_elem, the_list):
if (the_elem == the_list):
return (True, the_elem)
elif the_elem in the_list:
return (True, the_list)
else:
for e in the_list:
if (type(e) is list):
(is_found, the_parent) = get_parent_list(the_elem, e)
if (is_found):
return (True, the_parent)
return (False, None)
Testing it out: 测试出来:
my_list=[[[['a','b'],['c','d'],['e','f']],[['1','2'],['3','4'],['5','6']]],
[[['a','b'],['c','d'],['e','f']],[['1','2'],['3','4'],['5','6']]]]
Test Case 1: 测试案例1:
the_child = my_list[0][1][1]
the_flag, the_parent = get_parent_list(the_child, my_list)
print (the_flag)
print (the_child)
print (the_parent)
Result: 结果:
True
['3', '4']
[['1', '2'], ['3', '4'], ['5', '6']]
Test Case 2: 测试案例2:
the_child = my_list[0][1]
the_flag, the_parent = get_parent_list(the_child, my_list)
print (the_flag)
print (the_child)
print (the_parent)
Result: 结果:
True
[['1', '2'], ['3', '4'], ['5', '6']]
[[['a', 'b'], ['c', 'd'], ['e', 'f']], [['1', '2'], ['3', '4'], ['5', '6']]]
Test Case 3: 测试案例3:
the_child = my_list[:]
the_flag, the_parent = get_parent_list(the_child, my_list)
print (the_flag)
print (the_child)
print (the_parent)
Result: 结果:
True
[[[['a', 'b'], ['c', 'd'], ['e', 'f']], [['1', '2'], ['3', '4'], ['5', '6']]], [[['a', 'b'], ['c', 'd'], ['e', 'f']], [['1', '2'], ['3', '4'], ['5', '6']]]]
[[[['a', 'b'], ['c', 'd'], ['e', 'f']], [['1', '2'], ['3', '4'], ['5', '6']]], [[['a', 'b'], ['c', 'd'], ['e', 'f']], [['1', '2'], ['3', '4'], ['5', '6']]]]
Test Case 4: 测试案例4:
the_child = my_list[0][1] + ['Non-existent value']
the_flag, the_parent = get_parent_list(the_child, my_list)
print (the_flag)
print (the_child)
print (the_parent)
Result: 结果:
False
[['1', '2'], ['3', '4'], ['5', '6'], 'Non-existent value']
None
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.