A.Elnaggar
A.Elnaggar

Reputation: 240

Initializing matrix in Python?

I'm using this Code to create a python matrix (5 rows, 2 columns) :

[[0 for x in xrange(2)] for x in xrange(5)]

Can some one explain that part 0 for x in xrange(2) and why it didn't worked when i tried to do it as follows :

[[0 for x in xrange(2)] 0 for x in xrange(5)]

Upvotes: 3

Views: 10653

Answers (5)

Sanjay Rai
Sanjay Rai

Reputation: 27

m=int(input("Enter row"))

n=int(input("Enter column"))

a=[] #Blank Matrix

for i in range(m):#Number of Row Input

    b=[]#Blank List

    for j in range(n):#Number of col Input

        j=int(input("Enter Number in Pocket ["+str(i)+"]["+str(j)+"]")) #Show Row an col Number 

        b.append(j)#add Row Element

    a.append(b)#Add list In Matrix

print(a)#Print Matrix

Upvotes: -1

6502
6502

Reputation: 114579

The code in the question is a nested list comprehension. In Python for example

[x*x for x in range(10)]

returns

[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

That syntax means "collect the value of x*x where x is each elements contained in range(10)"

You can of course write also things like

[42 for x in range(10)]

that will give

[42, 42, 42, 42, 42, 42, 42, 42, 42, 42]

(this is exactly the same as above, simply the value x is not used in the expression that you want to collect)

That "list comprehension" is however an expression itself so it can be used in another outer list comprehension... for example

[[x*y for x in range(3)] for y in range(4)]

will return

[[0, 0, 0],
 [0, 1, 2],
 [0, 2, 4],
 [0, 3, 6]]

In the special case of a numeric constant (or any other immutable) you can use a simpler form to build a matrix:

[[0] * 3 for x in range(4)]

that returns

[[0, 0, 0],
 [0, 0, 0],
 [0, 0, 0],
 [0, 0, 0]]

because multiplying a list by an integer n you get a list composed of n copies of the original list concatenated (and so [0]*5 returns [0, 0, 0, 0, 0]).

Note that however the apparently similar

[[0] * 3] * 4

doesn't do the same thing. The reason is that while still displayed as

[[0, 0, 0],
 [0, 0, 0],
 [0, 0, 0],
 [0, 0, 0]]

in this case the outer list will be composed of 4 references to the same row, not 4 independent rows. This means that after the code

x = [[0] * 3] * 4
x[0][0] = 9

x contains

[[9, 0, 0],
 [9, 0, 0],
 [9, 0, 0],
 [9, 0, 0]]

and not

[[9, 0, 0],
 [0, 0, 0],
 [0, 0, 0],
 [0, 0, 0]]

as it would happen with a real matrix composed of independent rows.

Upvotes: 4

KurinchiMalar
KurinchiMalar

Reputation: 608

Ar = []
Ar.append([1,2,3,4])
Ar.append([2,5,6,7])
Ar.append([3,8,10,11])
Ar.append([4,9,12,13])

By creating list of list using list apprehension, I am able to index like

print Ar[2][2]

Output

10

Upvotes: 1

Ludo
Ludo

Reputation: 842

You have here a nested list comprehension. The first bit

[0 for x in xrange(2)]

creates a list of length 2, with each entry set to 0. This list is set as value for the second list comprehension. The following would yield the same result:

zeros2 = [0 for x in xrange(2)]
# create 5 copies of zeros2
zeros2x5 = [zeros2[:] for x in xrange(5)] 

Upvotes: 4

EarlGrey
EarlGrey

Reputation: 531

[0 for x in xrange(2)]

means "Write a list with a "0" for every element in (0, 1)" so a list with 2 entries, both "0" will be created.
So you can see

[[0 for x in xrange(2)] for x in xrange(5)]

creates a list with 6 entries. Each entry will be the result of

[0 for x in xrange(2)]

At the end you have a list, containing 5 lists, containing 2 zeroes.

In your code, the second "0" made no sense.

Upvotes: 2

Related Questions