473,378 Members | 1,544 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,378 software developers and data experts.

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 1583
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
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...
11
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...
17
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...
4
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...
2
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...
17
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...
3
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...
0
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...
0
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
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...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
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...

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.