1

I'm trying to insert a list of number into a list of list. I've simplified the code just as an example because the actual thing is fairly long. Basically i have two list and the end result i want is as follows:

C = [
[1,1,x],
[2,1,x],
[3,1,x],
[1,2,x],
[2,2,x],
[3,2,x],
[1,3,x],
[2,3,x],
[3,3,x],
]

You can think of this as a matrix of nxn dimensions, but the list has to be like this. Basically my problem is that i can't figure out how to insert the first list into index 0 and 1 like in the matrix above. The second list is just the x's at index 3 which i can work my way around.

Here is what i have so far, any help would be appreciated. Thanks a lot!!

set_I = [1,2,3]


set_J = [x,x,x,x,x,x,x,x,x]


C = []
for i in range(len(set_I)*len(set_I)):
    C.append([])

for liste in C:
    liste.insert(0,0)
    liste.insert(1,0)
    liste.insert(2,0)
3
  • 4
    Your text does not make it clear what your expected outcome is. Your first example does not seem to fit the second example, at least not that I can see. What do the first two rows mean? Are those all the combinations of set_I? Commented Apr 12, 2021 at 17:12
  • What results are you getting? Commented Apr 12, 2021 at 17:13
  • The first for loop I'm initializing the list of list of the right size so 3x3. and the second for loop is me inserting zeros so maybe i could replace them after. this was just and idea but I'm not sure I'm on the right path Commented Apr 12, 2021 at 17:20

3 Answers 3

1

Super wild guess:

set_I = [1,2,3]

set_J = [20,21,22,23,24,25,26,27,28]

C = []
for i in range(len(set_I)):
    for j in range(len(set_I)):
        C.append([set_I[j], set_I[i], set_J[3*i+j]])

print(C)

Result:

[
   [1, 1, 20],
   [2, 1, 21],
   [3, 1, 22],
   [1, 2, 23],
   [2, 2, 24],
   [3, 2, 25],
   [1, 3, 26],
   [2, 3, 27],
   [3, 3, 28]
]
Sign up to request clarification or add additional context in comments.

Comments

0

I am confused. If you want the product, then use it.product(), like:

set_I = [1,2,3]
set_J = [0,0,0,0,0,0,0,0,0] #Just putting zeros instead of x's. 

import itertools as it

[[a,b,c] for (a,b),c in zip(it.product(set_I,repeat=2),set_J)]

If you want to initialize an empty list of list, then simply multiply.

[[0]*3]*9

Comments

0

I guess if you are looking for that problem, then I guess you must use this code set_I = [1,2,3]

   set_J = ['x','x','x','x','x','x','x','x','x']
   
   
   C = []
   for i in range(len(set_I)*len(set_I)):
       C.append([])
   
   for i in range(len(set_I)):
       for j in range(len(set_I)):
           C[3*i+j].append(j+1)
           C[3*i+j].append(i+1)
           C[3*i+j].append(set_J[3*i+j])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.