473,409 Members | 1,979 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,409 software developers and data experts.

Define a 2d Array?

Hi.

How do I define a 2d list?

For instance, to define a 4 by 5 list, I wanted to do this:
n=4
m=5
world = [n][m]
However, it gives me an invalid syntax error saying the index is out
of range.
I know this is a real newbie question, but if you could help me out,
I'd really appreciate it!

Thanks,
Jill
Oct 12 '08 #1
6 5908

JillHow do I define a 2d list?

Python doesn't truly have 2d lists in the way you might think of 2d arrays
in C or Fortran. It has 1d lists which can contain any Python object,
including other lists. If you wanted to create a 4x5 list you'd do
something like this:

N = 4
M = 5
mylist = []
for i in range(N):
mylist.append([0.0] * M)

If you are looking to do numeric work with such multidimensional lists you
should consider the builtin array object or the numpy package:

http://docs.python.org/dev/library/a...l#module-array
http://numpy.scipy.org/

Skip
Oct 12 '08 #2
On Oct 11, 9:30*pm, s...@pobox.com wrote:
* * JillHow do I define a 2d list?

Python doesn't truly have 2d lists in the way you might think of 2d arrays
in C or Fortran. *It has 1d lists which can contain any Python object,
including other lists. *If you wanted to create a 4x5 list you'd do
something like this:

* * N = 4
* * M = 5
* * mylist = []
* * for i in range(N):
* * * * mylist.append([0.0] * M)

If you are looking to do numeric work with such multidimensional lists you
should consider the builtin array object or the numpy package:

* *http://docs.python.org/dev/library/a...l#module-array
* *http://numpy.scipy.org/

Skip
I think you can do

mylist = [[]] or somesuch...

if you are looking on google for examples you will comonly find them
in spreadsheets.. I have one in the editor part of dex tracker
(available on source forge) The array will start at zero and ie x[0]
and will keep growing as long as you .append it.. You don't define
the size in advance like you would with other languages.. You need to
have values befour you try to
use a location in the array.
Oct 12 '08 #3
On Oct 12, 1:30*pm, s...@pobox.com wrote:
* * JillHow do I define a 2d list?
>
If you are looking to do numeric work with such multidimensional lists you
should consider the builtin array object or the numpy package:

* *http://docs.python.org/dev/library/a...l#module-array
The built-in array module does *NOT* support multidimensional arrays.

The referenced docs say (first two sentences) "This module defines an
object type which can compactly represent an array of basic values:
characters, integers, floating point numbers. Arrays are sequence
types and behave very much like lists, except that the type of objects
stored in them is constrained."

"behave very much like lists" is in no way to be construed as
supporting multiple dimensions, and that type constraint means that
you can't even have an array of arrays.

However you can have a list of arrays; this can be a memory-efficient
solution for some 2D applications. Note that arrays are not
recommended for CPU efficiency, as (in general) each time you access
an array element, a new Python object must be created. Escape clause:
CPython and single-byte arrays (type 'c' and some values of types 'b'
and 'B').

Cheers,
John
Oct 12 '08 #4

EricI think you can do

Ericmylist = [[]] or somesuch...

That won't do what you want. You've defined a list with a single element
(another list). You might have been thinking of something like this:
>>N = 4
M = 5
mylist = [[0.0] * M] * N
While to the casual glance it looks like what you want:
>>print mylist
[[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]]

assigning to an element of this structure demonstrates its shortcoming:

>>mylist[1][1] = 42.7
print mylist
[[0.0, 42.700000000000003, 0.0, 0.0, 0.0], [0.0, 42.700000000000003, 0.0, 0.0, 0.0], [0.0, 42.700000000000003, 0.0, 0.0, 0.0], [0.0, 42.700000000000003, 0.0, 0.0, 0.0]]

There is just one copy of [0.0] * M referenced N times.

Skip
Oct 12 '08 #5
On Oct 11, 9:19*pm, Jillian Calderon <jillian.calde...@gmail.com>
wrote:
How do I define a 2d list?

For instance, to define a 4 by 5 list, I wanted to do this:
n=4
m=5
world = [n][m]
However, it gives me an invalid syntax error saying the index is out
of range.
Here are some examples of how you can use list comprehensions to do
this:

In [1]: n=4

In [2]: m=5

In [3]: world = [[[] for ni in range(n)] for mi in range(m)]

In [4]: world
Out[4]:
[[[], [], [], []],
[[], [], [], []],
[[], [], [], []],
[[], [], [], []],
[[], [], [], []]]

In [5]: world[0][0]
Out[5]: []

In [6]: len(world)
Out[6]: 5

In [7]: len(world[0])
Out[7]: 4

In [8]: world = [[[ni+mi*n] for ni in range(n)] for mi in range(m)]

In [9]: world
Out[9]:
[[[0], [1], [2], [3]],
[[4], [5], [6], [7]],
[[8], [9], [10], [11]],
[[12], [13], [14], [15]],
[[16], [17], [18], [19]]]

Best,
Tom
Oct 13 '08 #6
Thanks, everyone. All of your help solved at least my data storage
problem!
Oct 13 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Roman Mashak | last post by:
Hello, All! I wonder is it possible to define an array containing strings, not single characters? What I want is array 'table' that will have N elements, and every element is a strings tailoring...
5
by: Stuart Norris | last post by:
Dear Readers, I am attempting to define an array of IPAddress-es in C#. I wish to have a array of address so I can try in order to connect to them in a loop to handle unavailable hosts. ...
29
by: Ancient_Hacker | last post by:
It sure would be nice if I could have a macro that add a level of indirection to its argument. So if I write: AddIndirection( X ) The macro AddIndirection will do: #define X (*X) ...
2
by: marceliogp | last post by:
I have a file.h that have all my definitions and I need to define a array to use in my software, but I don´t know how can I do it. My define´s that cause error: .... #define ERROR_SAMPLE1 ...
10
by: Zhou Yan | last post by:
I want to define a pointer to a multiple-subscripted array. For instance, I have an array defined as below( I have reduced the size of the array as well as the dimension, but I think it OK to ask...
23
by: anon.asdf | last post by:
Hello! In the following code-snippet, is it possible to initialize each element of arr, with STRUCT_INIT? struct mystruct { int a; char b; };
5
by: suresh | last post by:
Hi, How to define a two dimensional array where each row is of type vector<map<string,int>>? My idea is, if "x" is such a variable, x is a vector where each cell of the vector is a...
8
by: Susan Bricker | last post by:
Hi. I would like to create an array and use the data to populate some new records when the user clicks on a button (that's the short description). If I were to code the array in C (I know ......
5
by: maker.rain1 | last post by:
Hello All, I have come across a problem as explained below in a sample. Please help me if anyone has any ideas to solve this. I have a #define as defined below. #define MAX 200 int...
1
by: lumumba401 | last post by:
Hello everybody, i am asking about how to define a bidimensional dynamic array as a global variable to use as incoming variable in a function Let us see , for example in a part of a programm...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.