python references

2007-02-05 Thread dustin . getz
>>> from Numeric import zeros
>>> p=zeros(3)
>>> p
array([0,0,0])
>>> p[0]
0
>>> x=p[0]
>>> x=10
>>> p
array([0,0,0]) #actual behavior
#array([10,0,0]) #desired behavior

I want x to be a C++-esque reference to p[0] for convenience in a
vector3 class.  i dont want accessor methods.  i know python can do
this, but it's been a long time since I used it and am unsuccessful in
my googling and docreading.  a little help please?

-- 
http://mail.python.org/mailman/listinfo/python-list


modifying a list while iterating through

2007-02-25 Thread dustin . getz
consider the following working loop where Packet is a subclass of
list, with Packet.insert(index, iterable) inserting each item in
iterable into Packet at consecutive indexes starting at index.

i=0
while(ihttp://mail.python.org/mailman/listinfo/python-list


Re: modifying a list while iterating through

2007-02-26 Thread dustin . getz
On Feb 25, 9:15 pm, [EMAIL PROTECTED] wrote:
> On Feb 25, 5:12 pm, [EMAIL PROTECTED] wrote:
>
> > consider the following working loop where Packet is a subclass of
> > list, with Packet.insert(index, iterable) inserting each item in
> > iterable into Packet at consecutive indexes starting at index.
>
> > i=0
> > while(i > if packet[i:i+5]==Packet("01110"):
> > packet.insert(i, "0")
> > i+=10 #skip the 5 bits inserted, and skip the 5 bits just
> > checked bc overlap should not trigger insertion
> > else: i+=1
>
> > is there a way to do this more elegantly?  seems like a big kludge.
>
> If Packet consists of '0's and '1's, then it may be
> easier to convert to, or base the class on str (strings):
>
> packet = "101010011100111010001"
> print "BEFORE ", packet
> li = packet.split("01110")
> packet = "011100".join(li)
> print "AFTER  ", packet
>
> --
> Hope this helps,
> Steven



Steven, George,

Thanks for your responses.  Yea, that would work.

My original question still stands, though, in situations where a
simple string replacement might not be sufficient.  Is there a way to
insert into a list whilst iterating through it?

for example, consider a list of binary values.

for index in range(len(iterable)):
  item=iterable[index]
  if item==1:
 iterable.insert(index,0)

obv this wouldn't work because now all future indexes will be off by
the number of previously inserted items.

using a while loop to fix this ugly and counterintuitive.

-- 
http://mail.python.org/mailman/listinfo/python-list