Python Forum
Creating 2D array without Numpy
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]
Reply
#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]
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#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
Reply
#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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Numpy] How to store different data type in one numpy array? water 7 288 Mar-26-2024, 02:18 PM
Last Post: snippsat
  reshaping 2D numpy array paul18fr 3 969 Jan-03-2023, 06:45 PM
Last Post: paul18fr
  Numpy returns "TypeError: unsupported operand type(s) for *: 'numpy.ufunc' and 'int'" kalle 2 2,527 Jul-19-2022, 06:31 AM
Last Post: paul18fr
  Numpy array BrianPA 13 4,837 Jan-23-2021, 09:36 AM
Last Post: Serafim
  Creating 2D Array jpinko 1 2,182 Nov-23-2020, 09:05 PM
Last Post: jefsummers
  How to fill datetime64 field in numpy structured array? AlekseyPython 0 2,234 Oct-20-2020, 08:17 AM
Last Post: AlekseyPython
  Adding data in 3D array from 2D numpy array asmasattar 0 2,168 Jul-23-2020, 10:55 AM
Last Post: asmasattar
  Creating look up table/matrix from 3d data array chai0404 3 2,824 Apr-09-2020, 04:53 AM
Last Post: buran
  converting dataframe to int numpy array glennford49 1 2,290 Apr-04-2020, 06:15 AM
Last Post: snippsat
  Replacing sub array in Numpy array ThemePark 5 4,086 Apr-01-2020, 01:16 PM
Last Post: ThemePark

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020