Understanding the Insertion of Numbers into a 3D Array and Conditional Branching in a For Loop

Asked 2 years ago, Updated 2 years ago, 26 views

I'm having trouble inserting numbers into a 3D array and conditional branching during a for loop.

If li1[i][j][k] and li2[l][1] match the number li2[l][0] and li2[l][2] would like to be inserted before and after li2[l][1] in li1.

 li1 = [[0,1,10], [0,2,10]], 
 [[0, 1, 3, 5, 10], [0, 1, 3, 6, 10], [0, 1, 4, 5, 10], [0, 1, 4, 6, 10], [0, 2, 3, 5, 10], [0, 2, 3, 6, 10], [0, 2, 4, 5, 10], [0, 2, 4, 6, 10]]]

li2 = [[7,1,8], [9,2,11], [12,3,13], [14,4,15], [16,5,17], [18,6,19]]

for i in range (len(li1)):
    for jin range (len(li1[i])):
        for kin range (1, len(li1[i][j])-1):
            for lin range (len(li2)):#
                if li1[i][j][k]==li2[l][1]:
                    li1[i][j].insert(k, li2[l][0])
                    li1[i][j].insert (k+2, li2[l][2])


print(li1)


>> 
[[[0, 7, 1, 8, 10], [0, 9, 2, 11, 10]], 
 [[0, 7, 7, 7, 1, 8, 8, 8, 3, 5, 10], [0, 7, 7, 7, 1, 8, 8, 8, 3, 6, 10], [0, 7, 7, 7, 1, 8, 8, 8, 4, 5, 10], [0, 7, 7, 7, 1, 8, 8, 8, 4, 6, 10], [0, 9, 9, 9, 2, 11, 11, 11, 3, 5, 10], [0, 9, 9, 9, 2, 11, 11, 11, 3, 6, 10], [0, 9, 9, 9, 2, 11, 11, 11, 4, 5, 10], [0, 9, 9, 9, 2, 11, 11, 11, 4, 6, 10]]]

As a final result, we would like to derive the result of the 3D array.
According to the above results, 7 and 8 have been repeated three times in a row, and the results are completely different from the results below.

result=[[0,7,1,8,10],[0,9,2,11,10]],
    [[0,7,1,8,12,3,13,18,6,19,10],[0,7,1,8,12,3,13,18,6,19,10],[0,7,1,8,14,4,15,16,5,17,10], [0,7,1,8,14,4,15,18,6,19,10], [0, 2, 3, 5,10], [0,9,2,11,12,3,13,18,6,19,10], [0,9,2,11,14,4,15,16,5,17,10], [0,9,2,11,14,4,15,18,6,19,10]]]

There is no problem with writing in a different way.Thank you for your cooperation.

python

2022-09-30 21:11

2 Answers

insert() changes the length of li1[i][j], so using k as an index would be strange.Change to use another variable (m) as an index.

for i in range(len(li1)):
  for jin range (len(li1[i])):
    m = 0
    for kin range (1, len(li1[i][j])-1):
      m + = 1 
      for lin range (len(li2)):
        if li1[i][j][m]==li2[l][1]:
          li1[i][j].insert(m, li2[l][0])
          li1[i][j].insert(m+2, li2[l][2])
          m+=2


2022-09-30 21:11

If you fiddle with the list of looped objects in the middle of a loop, the index changes and it becomes more and more troublesome.
If you want to build a list, it's easy to create a new list instead of changing the existing one.

First, consider the code to copy li1 to the new list.
The process is to add the elements of li1 according to the conditions.

Below is the method of creating a new list with the code of the question as much as possible.
There are many ways to organize things, such as cutting out the parts to be processed into different functions.

The output does not match the result shown in the question.
There must be a mistake between them.

#!/usr/bin/python3

li1 = [[0,1,10], [0,2,10]], 
 [[0, 1, 3, 5, 10], [0, 1, 3, 6, 10], [0, 1, 4, 5, 10], [0, 1, 4, 6, 10], [0, 2, 3, 5, 10], [0, 2, 3, 6, 10], [0, 2, 4, 5, 10], [0, 2, 4, 6, 10]]]

li2 = [[7,1,8], [9,2,11], [12,3,13], [14,4,15], [16,5,17], [18,6,19]]

result = [ ]

for i in range (len(li1)):
    result.append([])
    for jin range (len(li1[i])):
        result[-1].append([])
        fork in range (len(li1[i][j]))):
            ifk==0 or k==len(li1[i][j])-1:
                result[-1][-1].append(li1[i][j][k])
            else:
                add = [li1[i][j][k]] 
                for lin range (len(li2)):
                    if li1[i][j][k]==li2[l][1]:
                        add=li2[l]
                        break
                result[-1][-1].extend(add)


from print import print
print(result)

Results

[[0,7,1,8,10],[0,9,2,11,10]],
 [[0, 7, 1, 8, 12, 3, 13, 16, 5, 17, 10],
  [0, 7, 1, 8, 12, 3, 13, 18, 6, 19, 10],
  [0, 7, 1, 8, 14, 4, 15, 16, 5, 17, 10],
  [0, 7, 1, 8, 14, 4, 15, 18, 6, 19, 10],
  [0, 9, 2, 11, 12, 3, 13, 16, 5, 17, 10],
  [0, 9, 2, 11, 12, 3, 13, 18, 6, 19, 10],
  [0, 9, 2, 11, 14, 4, 15, 16, 5, 17, 10],
  [0, 9, 2, 11, 14, 4, 15, 18, 6, 19, 10]]]


2022-09-30 21:11

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.