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 6 5941
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 multidimensiona l 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
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 multidimensiona l 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.
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 multidimensiona l 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 multidimensiona l 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
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.700000000000 003, 0.0, 0.0, 0.0], [0.0, 42.700000000000 003, 0.0, 0.0, 0.0], [0.0, 42.700000000000 003, 0.0, 0.0, 0.0], [0.0, 42.700000000000 003, 0.0, 0.0, 0.0]]
There is just one copy of [0.0] * M referenced N times.
Skip
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
Thanks, everyone. All of your help solved at least my data storage
problem! This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 with '\0', like that:
table = "string0\0"
table = "string1\0"
...
|
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.
Todate since I do not know how to define an array of IPAddress-es. I
have been defining variable like:
|
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)
so after that point whenever you use X it gets replaced by (*X)
|
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 123
#define ERROR_SAMPLE2 124
....
#define FUNCTIONS_EMP { 20, 15, 22, 53, 34)
|
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
this simple case.)
/----
int array = { 1, 2, 3, 4 }
\-----
| |
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;
};
|
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 map<string,int>.
Similarly x is a vector where each cell of it is a map<string,int>.
|
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 ... not this forum, but it's
what I know) I would do it like this ...
/* Declare the structure template */
struct recdata {
long classID;
|
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 _tmain(int argc, _TCHAR* argv)
|
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 my problem:
#include<iostream>
...
using namespace std;
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |