python遍历多维序列搜索(栈)
``
def listfiand(w,obj):
li =w
k1 = []
bate = {}#外字典,记录所有的元素
li2=str(li)
h2 = li2.find("%s"%obj)#用字符串搜索obj的位置
print("h2=",h2)
print("h3=",li2.count("[",0,h2))
print("h4=",li2.count("]",0,h2))
h=1+abs(li2.count("[",0,h2)-li2.count("]",0,h2))#用"["和"]"之差锁定在几维列表中,元组可以用"()",如果混合可迭代对象(序列),可以尽可能的把h写大
print(h)
for x, i in enumerate(li) :
bate["[%d]" % x] = i#将列表加入字典进行遍历,字典的key值就是obj在多维列表的下标
while h:
h-=1
dis = {}#循环字典
for k, v in bate.items():
if type(v)==list :
for x, i in enumerate(v) :
dis[k+"[%d]" % x] = i#进行坐标拼接
bate.update(dis)#加入外部字典
list1=[]
for k , v in bate.items():#遍历外部字典,同时输出key和values
if v == obj:#判断字典中的中的值是否符合
k=k.strip()[1:-1].split("][")#处理得到的坐标,
list2=[]
for i in k :
k1.append(int(i))
k1.append(list2)#可以得到所有在序列中的obj的下标
print(bate)
return k1
li = ["hello",'seven',[[[[[[[0,[0]]]]]]]],["mon",["h","kelly"],'all'],123,446]
print(listfiand(li,0))
#输出:[[2, 0, 0, 0, 0, 0, 0, 0], [2, 0, 0, 0, 0, 0, 0, 1, 0]]分别表示两个零的坐标
``