1

Suppose I have two lists like this,

Ii = [[7,1],[7,5],[7,8],[5,8],[2,8],[3,5]]
ci = [11,5,3,5,5,4]

Now I want to make another list (I) of m x n which will look like this,

I = 
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[11, 0, 0, 0, 5, 0, 0, 3, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 5, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 4, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 5, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

I tried this code,

m,n = 8,10    
I = [[0 for j in range(n)] for i in range(m)]
    for i, j in Ii:
        I[m - i][j - 1] = 1

Which have an output looks like this,

I = 
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[1, 0, 0, 0, 1, 0, 0, 1, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 1, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 1, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

Now I have 1 where I wanted the values of ci. But I want to put the values of ci in the places of 1. Need help regarding how can I do this?

6
  • What is n and what is m? Commented Nov 5, 2020 at 16:03
  • can you explain what is the logic behind creating I form Ii and ci? Commented Nov 5, 2020 at 16:03
  • 2
    Can you explain the logic by which the two input lists lead to the 4x4 list? I thought for a second it might be "assign the values from the second list to the corresponding coordinates from the first list", but that's not what your sample output looks like. Commented Nov 5, 2020 at 16:03
  • 1
    Sorry for missing the information, m=4 and n=4 for 4x4 matrix (n is not used). The logic is creating a list which has a reverse index number in the vertical line (m-vertical) and (horizontal-1) in the horizontal index. but you can also directly use (i,j) as index no problem. I only need to know how to pass the values of ci to replace the 1s? Commented Nov 5, 2020 at 16:20
  • Henry bro it is saying list index out of range for 8x8 matrix. Commented Nov 5, 2020 at 16:31

1 Answer 1

1
m = 8 
n = 10 
Ii = [[7,1],[7,5],[7,8],[5,8],[2,8],[3,5]]
ci = [11,5,3,5,5,4]

I = [[0 for j in range(n)] for i in range(m)]

for z, *j, in enumerate(Ii):
    for i, k in j:
        I[m-i][k-1] = ci[z]

for i in I:
    print(i)
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[11, 0, 0, 0, 5, 0, 0, 3, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 5, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 4, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 5, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Sign up to request clarification or add additional context in comments.

8 Comments

bro Ii and ci will have same length but the output I can have different length.
It is not working for the following values: m = 8 n = 10 Ii = [[7,1],[7,5],[7,8],[5,8],[2,8],[3,5]] ci = [11,5,3,5,5,4]
Please check. The output is wrong. You need to put 11 in the (7,1) cell, 5 in the (7,5) cell, 3 in the (7,8) cell and so on. You are putting 11 in multiple cells.
You cannot call index number in any logic because it is not relatable. You need to search for '1' in my code and replace it with values of ci.
No bro you are wrong. m can be any values. It is not related to Ii. Please check this code: m = 8 n = 10 Ii = [[7,1],[7,5],[7,8],[5,8],[2,8],[3,5]] ci = [11,5,3,5,5,4] I = [[0 for j in range(n)] for i in range(m)] for i, j in Ii: I[m - i][j - 1] = 1 This will place 1 into all the places of Ii where I have used a reverse index method. It is never related to ci. and m is independent, you can put m=100 also this will work.
|

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.