Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)

I have below two lists:

Ii = [[7,1],[7,5],[7,8],[5,8],[2,8],[3,5]]

ci = [11,5,3,5,5,4]

I want to make another list named (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

I get the output: 

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]

In the output instead of 1 I wanted to place the value of ci, how can I do this?

1 Answer

0 votes
by (36.8k points)

You can use the below approch:

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]

 Want to be a master in Data Science? Enroll in this Data Scientist

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...