Python 从嵌套列表中提取索引

Python 从嵌套列表中提取索引,python,list,recursion,Python,List,Recursion,我希望能够获得嵌套列表中每个数字的索引,例如,我有以下列表: T=['苹果',[10,5,['橙色]],3] 我想提取如下值: 编号清单:[10,5,3] 数字地址:[[1,0],[1,1],[2]] 例如,数字“3”是列表索引2中的第三个值,其中数字“5”是第二个列表中的第二个值,因此它将是[1,1] 我编写了以下递归函数,其中包含for循环: indices = [] List_numbers = [] Address_numbers = [] def Numbers(T): fo

我希望能够获得嵌套列表中每个数字的索引,例如,我有以下列表:

T=['苹果',[10,5,['橙色]],3]

我想提取如下值:

编号清单:[10,5,3] 数字地址:[[1,0],[1,1],[2]]

例如,数字“3”是列表索引2中的第三个值,其中数字“5”是第二个列表中的第二个值,因此它将是[1,1]

我编写了以下递归函数,其中包含for循环:

indices = []
List_numbers = []
Address_numbers = []

def Numbers(T):
    for index, elem in enumerate(T):
        if isinstance(elem, int):
            List_numbers.append(elem)
            Address_numbers.append(indices + [index])

        if isinstance(elem, list):
            indices.append(index)
            Fruits(elem)

    print('List of numbers: ' + str(List_numbers))
    print('Address of numbers: ' + str(Address_numbers))
此代码的问题在于,当它达到最后一个值并需要退出列表时,它会保留相同的索引,因此地址为:

List of numbers: [10, 5, 3]
Address of numbers: [[1, 0], [1, 1], [1, 2, 2]]

我已经尝试了所有的方法来修复它,但老实说,我不确定如何修复它,有人能帮忙吗?

您可以参考这里的代码

T = ['apple', [10, 5, ['orange']], 3]
U=[10,5,3]
new_list=[]
for j,i in enumerate(T):
    if isinstance(i,list): #=== If the value is list
        for k,v in enumerate(i):
            if v in U:
              
                new_list.append([j,k])
                print([j,k])
    else:
        if i in U:
            new_list.append([j])
            print([j])
print(new_list)
输出:

[[1, 0], [1, 1], [2]]
更好的方法是:

T = ['apple', [10, 5, ['orange',5]], 3]
U=[10,5,3,5]
new_list=[]
index_list=[]
new_dic={}
def new_lists(lists,index):
    for i,j in enumerate(lists):
        if isinstance(j,int):
            #new_list.append(j)
            new_dic.update({j:[index,i]})
            new_list.append(j)
            index_list.append([index,i])
        elif isinstance(lists,list):
            new_lists(j,index+1)
    

new_lists(T,0)
print("Original List :--",T)
print("List Element :--",new_list)
print("List Element Index :--",index_list)
print("updated dictionary :--",new_dic)
使用递归函数从嵌套列表中提取索引。 使用递归,您可以将索引从嵌套列表提取到任意深度:

T = ['apple', [10, 5, ['orange']], 3]
total_numbers = []
indexes = []


def find_index_of_numbers(lisst, level):
    for index, value in enumerate(lisst):
        if isinstance(value, int):
            total_numbers.append(value)
            indexes.append([level, index])
        elif isinstance(value, list):
            find_index_of_numbers(value, level + 1)


find_index_of_numbers(T, 0)
print("My list--->", T)
print("Numbers--->", total_numbers)
print("Indexes--->", indexes)
输出

My list---> ['apple', [10, 5, ['orange']], 3]
Numbers---> [10, 5, 3]
Indexes---> [[1, 0], [1, 1], [0, 2]]

嘿,我对你的解决方案有点困惑,因为如果T被改成其他任何东西,那么它就不起作用了。我需要一个解决方案,可以在任何嵌套列表中找到数字,并返回数字列表和每个数字的索引-这个解决方案非常特定于特定的T,T的值是多少?我用这个测试:['apple',de',3,4,[2,3,4,5,6]],我得到:[[2],[3],[4,0],[4,1],[4,2],[4,3],[4,4]]你在哪里,U=[1,2,3,4,5,6]@Sujay,我认为显式定义U不是一个好方法,而且如果它有一个更嵌套的列表,它将不起作用。谢谢你的解决方案,它帮助了我很多,但是有没有任何方法可以在不显示初始0的情况下做到这一点?那么它是[2]而不是[0,2]?