0

I was wondering how I would go about assigning a value to a specific index in a list that is inside another list. For example:

For a list parentList

for i in range(0,rows, 1):
        parentList[i] = []
        for j in range(0,cols,1):
            parentList[i[j]] = 0

Not sure if that is even the right syntax for assignment like this.

I know I can do .append, but what I need to do for my problem is create a sparse matrix, so everything should be 0 except for the values that the user specifies in the form (row) (col) (value).

So I thought using the indexes of the lists would help with assignment. If there is a better way, please let me know!

EDIT: This is an example of what is being done

input:

1 2 2

1 3 3

2 1 4

3 3 5

0 0 0


1 3 7

2 2 8

3 2 9

3 3 6

0 0 0

The first matrix in the input is:

0 2 3

4 0 0

0 0 5

The second matrix is:

0 0 7

0 8 0

0 9 6

Their product is:

0 43 18

0 0 28

0 45 30

So the output should be:

1 2 43

1 3 18

2 3 28

3 2 45

3 3 30

0 0 0

I have to use a 1D array of linked lists to represent this in my code and all input and output is done through the Python Shell.

WGS
  • 13,969
  • 4
  • 48
  • 51
Udent
  • 7
  • 4
  • Use a `dict` with a `tuple` (row, col) is probably the simplest way to emulate a sparse matrix using only Python builtins – Jon Clements Oct 24 '14 at 17:53
  • I don't follow. Could you show an example input and the corresponding desired output? – Karl Knechtel Oct 24 '14 at 17:54
  • `range(0,rows,1)` is equivalent to `range(rows)`. Starting at 0 and incrementing by 1 are the defaults, so you don't need them. – User Oct 24 '14 at 17:54
  • Not sure why, but the instructor specified this: "Your program should represent the sparse matrix as a one-dimensional array of linked lists. " – Udent Oct 24 '14 at 17:55
  • @Udent It'll be a good idea to [edit](http://stackoverflow.com/questions/26553392/interrelated-resources) your post with that and any other requirements/guidance you've been given so others can help you better... (and not suggest ways that you're not allowed/expected to do it etc...) – Jon Clements Oct 24 '14 at 17:56
  • One good way to manage a sparse matrix in Python is by using Scipy library: http://docs.scipy.org/doc/scipy-0.14.0/reference/sparse.html – mvillaress Oct 24 '14 at 17:57
  • Please specify in your post what the output should be. Is it supposed to be a m*m matrix (list of lists)? – User Oct 24 '14 at 17:57
  • Ok, put most of the stuff in that he was asking for, sorry just learning how to use this website, will be more thorough from the getgo in the future. – Udent Oct 24 '14 at 18:01

2 Answers2

0

It is easy to work with numpy arrays:

import numpy as np
a=np.empty((3,3))
for i in range(a.shape[0]):
    for j in range(a.shape[1]):
        a[i][j]=0
>>> a
array([[ 0.,  0.,  0.],
       [ 0.,  0.,  0.],
       [ 0.,  0.,  0.]])
Irshad Bhat
  • 8,479
  • 1
  • 26
  • 36
  • for this, are the values automatically converted to floating points? – Udent Oct 24 '14 at 18:09
  • You can set datatype of your choice as: **a=np.empty((3,3),dtype=int)** – Irshad Bhat Oct 24 '14 at 18:11
  • This might seem like a dumb question, but when you specify what position of shape, that is just like which one of the 3's specified earlier correct? So for: a = np.empty((x,y)) x = a.shape[0] y = a.shape[1]? – Udent Oct 24 '14 at 18:20
0

Python lists aren't sparse - but there are other packages like numpy and scipy that provide them. If your dataset isn't that big and you are okay just prefilling a list, this will do it:

rows = 100
cols = 13
l = [[0]*cols for _ in rows]

The [0]*cols creates an inner list filled with zeros, and [[0]*cols for _ in rows] repeats that operation for the number of rows you want.

After that, you address individual cells like

l[2][3] = 111
tdelaney
  • 73,364
  • 6
  • 83
  • 116