Bottom Page

Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Creating 2D array without Numpy
#1
I want to create a 2D array and assign one particular element.
The second way below works.
But the first way doesn't.
I am curious to know why the first way does not work.
Is there any way to create a zero 2D array without numpy and without loop?



The first way is:

n=10
Grid=[[0]*n]*n 
Grid[1][1]=1
Quote:[0, 1, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0]

The second way is:
n=10
Grid = [0] * n
for i in range(n):
    Grid[i] = [0] * n
Grid[1][1]=1
Quote:[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, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Quote
#2
The first way doesn't work because [[0] * n] creates a mutable list of zeros once. Then when the second *n copies the list, it copies references to first list, not the list itself. So you have a list of references, not a list of lists.

The second way a new [0] * n is created each time through the loop. That way there is no copying being done, so you end up with an actual list of lists. I often do the second way with a list comprehension:

[[0] * n for row in n]
landlord1984 likes this post
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote
#3
(Jan-27-2017, 09:43 AM)ichabod801 Wrote: The first way doesn't work because [[0] * n] creates a mutable list of zeros once. Then when the second *n copies the list, it copies references to first list, not the list itself. So you have a list of references, not a list of lists. The second way a new [0] * n is created each time through the loop. That way there is no copying being done, so you end up with an actual list of lists. I often do the second way with a list comprehension:
 [[0] * n for row in n] 

So there is no way to do this by one line code unless to use numpy?

L
Quote
#4
(Jan-27-2017, 05:29 PM)landlord1984 Wrote: So there is no way to do this by one line code unless to use numpy?

The list comprehension I showed you does it in one line of code.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  memory issue loading movie to numpy array djf123 1 85 Nov-07-2019, 06:19 AM
Last Post: ThomasL
  Numpy structured array Nitram 3 258 Sep-17-2019, 10:09 PM
Last Post: jefsummers
  numpy.copy / numpy.delete paul18fr 3 305 Jul-26-2019, 01:51 PM
Last Post: paul18fr
  AttributeError: module 'numpy' has no attribute 'array aapurdel 7 5,438 May-29-2019, 02:48 AM
Last Post: heiner55
  Array in numpy Truman 5 4,228 May-26-2019, 10:14 AM
Last Post: snippsat
  [split] Array in numpy aapurdel 1 308 May-25-2019, 08:05 PM
Last Post: buran
  [numpy]add a value to an array elmismo999 1 428 May-23-2019, 04:15 PM
Last Post: michalmonday
  "erlarge" a numpy-matrix to numpy-array PhysChem 2 443 Apr-09-2019, 04:54 PM
Last Post: PhysChem
  while creating a array using numpy.arrange, how to negate only the last 50 numbers? vicky53 1 454 Apr-06-2019, 11:52 AM
Last Post: scidam
  How to get the size of a numpy array? mcgrim 2 491 Mar-23-2019, 02:25 PM
Last Post: perfringo

Forum Jump:


Users browsing this thread: 1 Guest(s)