I want to construct a 2-dimensional array from a List but I cannot
find a simple way of changing any element. For example, construct a
3x3 array like this:-
>>x=[0,0,0]
x=[x]*3
this produces [[0,0,0],[0,0,0],[0,0,0]. So far so good.
How do I change the value of any element to produce (say)
[[99,0,0],[0,0,0],[0,0,0]] ?
gordc 4 1626
gc_ott:
How do I change the value of any element to produce (say)
[[99,0,0],[0,0,0],[0,0,0]] ?
gordc
To create a 2D list, that is a list of lists:
x = [[0] * ncols for i in nrows]
(Don't do what you were doing, because you end with many references to
the same list, and that will give you troubles.)
To set an item you do just:
x[0][0] = 99
Bye,
bearophile
On Nov 11, 7:48*pm, bearophileH...@lycos.com wrote:
gc_ott:
How do I change the value of any element to produce (say)
[[99,0,0],[0,0,0],[0,0,0]] ?
gordc
To create a 2D list, that is a list of lists:
x = [[0] * ncols for i in nrows]
(Don't do what you were doing, because you end with many references to
the same list, and that will give you troubles.)
To set an item you do just:
x[0][0] = 99
Bye,
bearophile
Many thanks, I don't think I would ever 'discovered' this.
gordc gc*******@yahoo.ca wrote:
On Nov 11, 7:48 pm, bearophileH...@lycos.com wrote:
>gc_ott:
>>How do I change the value of any element to produce (say) [[99,0,0],[0,0,0],[0,0,0]] ? gordc
To create a 2D list, that is a list of lists: x = [[0] * ncols for i in nrows] (Don't do what you were doing, because you end with many references to the same list, and that will give you troubles.)
To set an item you do just: x[0][0] = 99
Bye, bearophile
Many thanks, I don't think I would ever 'discovered' this.
If you are trying to emulate numerical arrays, you may want to use numpy, instead. http://numpy.scipy.org/
import numpy
x = numpy.zeros([3,3], dtype=int)
x[0,0] = 99
--
Robert Kern
"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
En Tue, 11 Nov 2008 23:09:25 -0200, <gc*******@yahoo.caescribió:
On Nov 11, 7:48*pm, bearophileH...@lycos.com wrote:
>To create a 2D list, that is a list of lists: x = [[0] * ncols for i in nrows]
Many thanks, I don't think I would ever 'discovered' this.
Try reading the FAQ: http://www.python.org/doc/faq/progra...mensional-list
--
Gabriel Genellina This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: myang |
last post by:
Hi,
I have thousands of points in 3D space, and I want to calcuate the distance
between each other of them. I do the calculation with the following code.
def distance(a,b):
sum = 0
for i in...
|
by: Cant Think Today |
last post by:
I have multi-dimesional arrays that can be specifed by the user, e.g
1,2,3,4,5
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,6
I think a bit of code that will iterate over these arrays to print out
the...
|
by: Soeren Sonnenburg |
last post by:
Hi all,
Just having started with python, I feel that simple array operations '*'
and '+' don't do multiplication/addition but instead extend/join an
array:
a=
>>> b=
>>> a+b
|
by: d[ - - ]b |
last post by:
Hi there,
Just wondering, if there is any way to have a dynamic / unspecified size of
an array? It obviously needs to be updated, deleted etc. The only way I
thought is by making the array...
|
by: scottyman |
last post by:
I can't make this script work properly. I've gone as far as I can with
it and the rest is out of my ability. I can do some html editing but
I'm lost in the Java world. The script at the bottom of...
|
by: Sheldon |
last post by:
Hi,
I have two arrays that are identical and contain 1s and zeros. Only the
ones are valid and I need to know where both arrays have ones in the
same position. I thought logical_and would work...
|
by: bei |
last post by:
Hi,
I am trying to write several arrays into one file, with one arrays in
one column. Each array (column) is seperated by space.
ie. a= b= c=
1 5 9
2 6 10
3 7 11
4 8 12
|
by: Erland Sommarskog |
last post by:
I am glad to announce that there is now a version of my article "Arrays and
Lists in SQL Server" for SQL 2005 available on my web site.
THe URL for the article is...
|
by: dmitrey |
last post by:
howto make Python list from numpy.array?
Thx, D.
|
by: gits |
last post by:
This little article will show you how to optimize runtime performance when you need to compare two arrays (a quite common task). Have a close look at the entire article, and you will see the...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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,...
|
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...
|
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,...
|
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...
| |