1

I'm trying to iterate over a list of lists of lists and list of tuples at the same time, replacing certain values within the list of lists of lists with values from the list of tuples.

Here is my code so 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]]]


for index1, aitem in enumerate(tups):
    for index2, a in enumerate(aitem):
        for index3, mega_item in enumerate(lsts):
            for index4, bitem in enumerate(mega_item):
                for index5, b in enumerate(item):
                    if i == 0:
                        lsts[index3][index4][index5] = a
                        break
                    else:
                        continue
                    break
                else:
                    continue
                break
            else:
                continue
            break

I want my solution to produce lsts with the 0s replaced by the values in tups in sequential order like 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, the my result at the 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 my loop may only be iterating over the first item in my list of tuples.

How can I solve this problem?

jcdrbm
  • 99
  • 1
  • 7

2 Answers2

3

This should work:

for inner_lst,inner_tup in zip(lsts,tups):
   for 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]]]
Ahsanul Haque
  • 10,676
  • 4
  • 41
  • 57
  • This works but if I want to create lsts from an original list of lists (lsts = [lst]*3), it produces this error: File "", line 3, in depth[depth.index(0)]= inner_tup[ind] ValueError: 0 is not in list. How can I solve this problem? – jcdrbm Jan 28 '16 at 13:57
  • http://stackoverflow.com/questions/240178/python-list-of-lists-changes-reflected-across-sublists-unexpectedly This will help you find the problem in your approach – Ahsanul Haque Jan 28 '16 at 14:01
  • This still throws the same error when I use list comprehension (lsts = [lst for n in range(3)])... Why does this not work? – jcdrbm Jan 28 '16 at 14:11
  • I've fixed the issue by doing this: lsts = [[[1, 0, 1], [2, 3, 1, 0], [1, 1, 1, 1, 10, 0]] for n in range(3)]. Now, the error does not happen. – jcdrbm Jan 28 '16 at 16:24
1

Simply nested for loops are working well.

for i in xrange(len(tups)):
 for j in xrange(len(tups[i])):
  index = lsts[i][j].index(0)
  lsts[i][j][index] = tups[i][j]

The output for:

for s in lsts:
 print s

is:

[[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]]