473,549 Members | 2,408 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problems with duplicating and slicing an array

Hi python gurus,
I have some questions when I'm using python numeric:

1. When I do v = u[:, :], it seems u and v still point to the same
memory. e.g. When I do v[1,1]=0, u[1,1] will be zero out as well.
What's the right way to duplicate an array? Now I have to do v =
dot(u, identity(N)), which is kind of silly.

2. Is there a way to do Matlab style slicing? e.g. if I have
i = array([0, 2])
x = array([1.1, 2.2, 3.3, 4.4])
I wish y = x(i) would give me [1.1, 3.3]
Now I'm using map, but it gets a little annoying when there are two
dimensions.
Any ideas would be deeply appreciated!

Thanks!!!

-Y
Jul 18 '05 #1
2 1595
Yun Mao wrote:
Hi python gurus,
I have some questions when I'm using python numeric:

1. When I do v = u[:, :], it seems u and v still point to the same
memory. e.g. When I do v[1,1]=0, u[1,1] will be zero out as well.
What's the right way to duplicate an array? Now I have to do v =
dot(u, identity(N)), which is kind of silly.
Hmm... I don't know Numeric, but in numarray (Numeric's replacement),
you just call the copy method:

py> u = na.arange(6, shape=(2, 3))
py> v = u.copy()
py> v[0,0] = 100
py> u
array([[0, 1, 2],
[3, 4, 5]])
py> v
array([[100, 1, 2],
[ 3, 4, 5]])
2. Is there a way to do Matlab style slicing? e.g. if I have
i = array([0, 2])
x = array([1.1, 2.2, 3.3, 4.4])
I wish y = x(i) would give me [1.1, 3.3]


This also works exactly as you'd hope in numarray.

py> i = na.array([0, 2])
py> x = na.array([1.1, 2.2, 3.3, 4.4])
py> x[i]
array([ 1.1, 3.3])

Do you have any reason that you have to use Numeric? Or can you upgrade
to numarray?

Steve
Jul 18 '05 #2
Yun Mao wrote:
I have some questions when I'm using python numeric: 1. When I do v = u[:, :], it seems u and v still point to the same
memory. e.g. When I do v[1,1]=0, u[1,1] will be zero out as well.
What's the right way to duplicate an array? Now I have to do v =
dot(u, identity(N)), which is kind of silly.
You can use v = 1*u or v = u.copy() to get the type of copy you want.
2. Is there a way to do Matlab style slicing?


Use the take function.

The output of the following code

from Numeric import array,take
a = array([1,2])
b = a
c = 1*a
d = a.copy()
a[0] = 3
print b
print c
print d
print take(a,[1,0,1])

is

[3 2]
[1 2]
[1 2]
[2 3 2]

Jul 18 '05 #3

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

Similar topics

54
3932
by: seberino | last post by:
Many people I know ask why Python does slicing the way it does..... Can anyone /please/ give me a good defense/justification??? I'm referring to why mystring gives me elements 0, 1, 2 and 3 but *NOT* mystring (5th element). Many people don't like idea that 5th element is not invited. (BTW, yes I'm aware of the explanation where slicing
11
2031
by: jbperez808 | last post by:
>>> rs='AUGCUAGACGUGGAGUAG' >>> rs='GAG' Traceback (most recent call last): File "<pyshell#119>", line 1, in ? rs='GAG' TypeError: object doesn't support slice assignment You can't assign to a section of a sliced string in Python 2.3 and there doesn't seem to be mention of this as a Python 2.4 feature (don't have time to actually try
17
15996
by: Jon Slaughter | last post by:
I'm having a little trouble understanding what the slicing problem is. In B.S.'s C++ PL3rdEd he says "Becayse the Employee copy functions do not know anything about Managers, only the Employee part of Manager is copied. ".... and gives the code above as .....
4
2896
by: jeg | last post by:
dear all, i'm an astronomer working with 2d images -- 2d numarrays. i have a script which basically does some operations on some images, and one of the first steps is to find a galaxy on an image (at, say, a known x,y coord), and create a sub-image by slicing out part of the larger array to create a more managable smaller one (e.g. 50x50...
2
2832
by: Pete Davis | last post by:
I have a Windows Forms datagrid with the datasource set to an ArrayList. The ArrayList is initially empty. I have DataGridColumn styles defined for 4 columns with widths, header titles, and mapping names set. The problems: 1: Now, I realize ArrayList doesn't implement IBindingList, so the
17
3547
by: baibaichen | last post by:
i have written some code to verify how to disable slicing copy according C++ Gotchas item 30 the follow is my class hierarchy, and note that B is abstract class!! class B { public: explicit B(INT32 i =0):i_(i){} virtual ~B(){}
3
1454
by: Adrian | last post by:
In the following code example I am trying to create a generic interface for a bunch of objects. Each concrete type is stored in its own container and the a container of pointer's to base is used so I can access all at once. I created a specialization of the template for Fruit *'s so that it returns a reference and the code looks the same as...
0
920
by: Jonathan Wood | last post by:
When a GridView performs a row edit, edit controls appear without needing to display a different page, or, apparently, needing to refresh the existing page. Does anyone have tips on duplicating this functionality. I'm guessing it involves a bunch of javascript, which my abilities are limited with. I need to edit some data that will exist...
0
1830
by: GavReynolds | last post by:
Hi All I am writing a program to load in an xml file from the server and create an animation to represent this data (for use as a 'front end' for the data). I have loaded in the XML data into the script no worries. I have also managed to duplicate 1 set of movieclips relating to this data. In this case there are 10 childnodes in the xml...
0
7542
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, 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...
0
7467
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7982
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6066
agi2029
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...
1
5385
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5110
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...
0
3494
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1961
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
0
783
bsmnconsultancy
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...

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.