【发布时间】:2021-09-17 09:54:26
【问题描述】:
我有一些代码可以生成如下所示的列表:
chrs = [[["a",["b","c"]],["d","e"],"f"],["g",[["h","i"],"j"]]]
现在我需要在这样的列表中找到一个特定的值。
我写了一些代码来做到这一点,但它似乎陷入了无限循环:
def locate_value(v,chrs): #
checked=[[]] # Set up the values
index_list=[] #
inside=0 #
cur=[0,0] #
#
while True: #
if chrs[cur[0]] == v: # If found, end the loop and return the index_list
return index_list #
#
if type(chrs[cur[0]]) != list: # If the current is not a list, skip this one
checked[inside].append(cur[0]) #
index_list = [] #
#
else: #
try: #
if cur[0] in gone[inside]: # If the current has been done before, skip it
cur[0] += 1 #
#
else: # And if not, move 1 space deeper
checked.append([]) #
index_list.append(cur[0]) #
cur[1] = cur[0] #
cur[0] = 0 #
inside += 1 #
#
except IndexError: # And if a sublist is fully checked, block it too
checked[inside].append(cur[1]) #
print(locate_value("e",[[["a",["b","c"]],["d","e"],"f"],["g",[["h","i"],"j"]]]))
编辑我才意识到这段代码有多糟糕
在这个例子中,函数locate_value应该返回[0, 1, 1],因为list[0][1][1]是"e"
但是当我运行它时,我陷入了无限循环 谁能告诉我出了什么问题,也许可以给我一些真正工作一次的代码。
这个列表是由用户输入的,所以我不知道它有多深。
虽然只有 python-3 的解决方案很好,但我也更喜欢支持 python-2 的解决方案。
【问题讨论】:
-
@BhagyeshDudhediya 这些似乎只适用于列表列表,不适用于任意/不规则嵌套。
-
“也许给我一些代码,实际上可以一次工作”哦,对不起,您对我们的免费编码支持不满意吗?
-
不,我的意思是我的代码
-
因为我的从来不工作