简体   繁体   中英

recursively extract each individual list from nested list python

not a python expert. I have a nested list like

result=[["a","b","c"],["d","e","f"],["g","h","i"], ...]

I would like to obtain separated lists for each sub list:

sublist1=["a","b","c"]
sublist2=["d","e","f"]
sublist3=["g","h","i"]
...

I know the "manual" procedure slicing using indexes, problem is that I have 20000 sublists and I am looking for a way to do that recursively and store the result in 20000 different list that are created automatically.

My current guess was to use a for loop like

for d in range(0, len(result)):

in order to have all the 20000 indexes, and than use slicing with theses indexes. Problem is that I have no idea of how to generate 20000 empty lists where to store each result in a way that is not manual. There are probably other alterntaives, but I could not think of anything that excluded rewriting the same command 20000 times.

result=[["a","b","c"],["d","e","f"],["g","h","i"]]
result_dict ={}
serial = 1
for inner_list in result:
    sub_list_name ="sublist"+str(serial) 
    serial = serial + 1
    result_dict[sub_list_name] = inner_list
print(result_dict)     

I have used an unorderedDict incase order matters you can use orderedDict.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM