python - Iterating through list of lists of lists and list of tuples -
i'm trying iterate on list of lists of lists , list of tuples @ same time, replacing values within list of lists of lists values list of tuples.
here code far:
tups = [(1,2,3),(4,5,6),(9,8,7)] lsts = [[[1, 0, 1], [2, 3, 1, 0], [1, 1, 1, 1, 10, 0]], \ [[1, 0, 1], [2, 3, 1, 0], [1, 1, 1, 1, 10, 0]], \ [[1, 0, 1], [2, 3, 1, 0], [1, 1, 1, 1, 10, 0]]] index1, aitem in enumerate(tups): index2, in enumerate(aitem): index3, mega_item in enumerate(lsts): index4, bitem in enumerate(mega_item): index5, b in enumerate(item): if == 0: lsts[index3][index4][index5] = break else: continue break else: continue break else: continue break
i want solution produce lsts 0s replaced values in tups in sequential order this:
lsts = [[[1, 1, 1], [2, 3, 1, 2], [1, 1, 1, 1, 10, 3]], \ [[1, 4, 1], [2, 3, 1, 5], [1, 1, 1, 1, 10, 6]], \ [[1, 9, 1], [2, 3, 1, 8], [1, 1, 1, 1, 10, 7]]]
however, result @ moment is:
lsts = [[[1, 1, 1], [2, 3, 1, 2], [1, 1, 1, 1, 10, 3]], \ [[1, 1, 1], [2, 3, 1, 2], [1, 1, 1, 1, 10, 3]], \ [[1, 1, 1], [2, 3, 1, 2], [1, 1, 1, 1, 10, 3]]]
i believe loop may iterating on first item in list of tuples.
how can solve problem?
this should work:
for inner_lst,inner_tup in zip(lsts,tups): ind,depth in enumerate(inner_lst): depth[depth.index(0)]= inner_tup[ind] print lsts
output:
[[[1, 1, 1], [2, 3, 1, 2], [1, 1, 1, 1, 10, 3]], [[1, 4, 1], [2, 3, 1, 5], [1, 1, 1, 1, 10, 6]], [[1, 9, 1], [2, 3, 1, 8], [1, 1, 1, 1, 10, 7]]]
Comments
Post a Comment