Re: Moving Places, Subtracting from slices/lists

2005-06-03 Thread Mark Sargent

>what does [hotcat[1]] do.?
>
>  
>
ah, quite simple really, adds roof to the end of hotcat...thanx..

Mark Sargent.

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


Re: Moving Places, Subtracting from slices/lists

2005-06-02 Thread Mark Sargent
Fredrik Lundh wrote:

>Elliot Temple wrote:
>
>  
>
>>btw hotcat[:] is a *copy* of hotcat, so just leave out "[:]"
>>
>>
>
>when you want to modify the thing you're looping over, you need
>to be careful.  looping over a copy is often a good idea (see the
>Python tutorial and the FAQ for more on this).
>
>  
>
Yes, I saw that, and that's why I was using the copy form in the 
loop...thanx for verifying this.

>>enumerate is a function that adds indexes to a list.  observe:
>>
>>
>
>  
>
hotcat = ['roof', 'roof', 'roof']
for index, word in enumerate(hotcat):
   if word == 'roof': del hotcat[index]
print hotcat


>['roof']
>
>if I understand the OP correctly, he wants to *move* the "roof" to the
>end of the string.
>  
>
correct...

>try:
>hotcat.remove("root")
>hotcat.append("root")
>except ValueError:
>pass
>
>is most likely the fastest way to do that.
>
> 
>
>  
>
will give it a blast...thanx

>
>  
>
Cheers.

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


Re: Moving Places, Subtracting from slices/lists

2005-06-02 Thread Mark Sargent
Dennis Lee Bieber wrote:

>On Thu, 02 Jun 2005 16:12:44 +0900, Mark Sargent
><[EMAIL PROTECTED]> declaimed the following in
>comp.lang.python:
>
>  
>
>>How do I get that x to be an integer b4 it is entered into the indice.? 
>>Cheers.
>>
>>
>>
>   If you really want the index, ask for the index...
>
>  
>
hc


>['Cat', 'roof', 'on', 'a', 'hot', 'tin']
>  
>
try:


>...i = hc.index("roof")
>...new_hc = hc[:i] + hc[i+1:] + [hc[i]]
>... except ValueError:
>...pass
>... 
>  
>
new_hc


>['Cat', 'on', 'a', 'hot', 'tin', 'roof']
>
>  
>
Hi All,

ok, got the following for that.

 >>> hotcat = ['Cat', 'roof', 'on', 'a', 'hot', 'tin']
 >>> i = hotcat.index("roof")
 >>> hotcat = hotcat[:1] + hotcat[i+1:] + [hotcat[1]]
 >>> except ValueError:
  File "", line 1
except ValueError:
 ^
SyntaxError: invalid syntax

Cheers.

P.S. If it's not too much, could you explain this line.?
hotcat[:1] + hotcat[i+1:] + [hotcat[1]]
hotcat[:1] = Cat?
hotcat[i+1:] = all after roof?
what does [hotcat[1]] do.?

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


Re: Moving Places, Subtracting from slices/lists

2005-06-02 Thread Fredrik Lundh
Elliot Temple wrote:

> btw hotcat[:] is a *copy* of hotcat, so just leave out "[:]"

when you want to modify the thing you're looping over, you need
to be careful.  looping over a copy is often a good idea (see the
Python tutorial and the FAQ for more on this).

> enumerate is a function that adds indexes to a list.  observe:

>>> hotcat = ['roof', 'roof', 'roof']
>>> for index, word in enumerate(hotcat):
>>>if word == 'roof': del hotcat[index]
>>> print hotcat
['roof']

if I understand the OP correctly, he wants to *move* the "roof" to the
end of the string.

try:
hotcat.remove("root")
hotcat.append("root")
except ValueError:
pass

is most likely the fastest way to do that.

 



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


Re: Moving Places, Subtracting from slices/lists

2005-06-02 Thread Elliot Temple

On Jun 2, 2005, at 12:12 AM, Mark Sargent wrote:

> Hi All,
>
> getting closer, me thinks.
>
>
 hotcat = ['Cat', 'roof', 'on', 'a', 'hot', 'tin']
 for x in hotcat[:]:

> ... if x == 'roof': hotcat.insert(6,x)
> ... del hotcat[x]
> ...
> Traceback (most recent call last):
>   File "", line 3, in ?
> TypeError: list indices must be integers
>
> How do I get that x to be an integer b4 it is entered into the  
> indice.?
> Cheers.

if you add "print x" to the loop you will see that X is the various  
words.

to get an integer, you could search the list for the index of x.  but  
that's lame.

btw hotcat[:] is a *copy* of hotcat, so just leave out "[:]"

enumerate is a function that adds indexes to a list.  observe:

hotcat = ['Cat', 'roof', 'on', 'a', 'hot', 'tin']
for index, word in enumerate(hotcat):
 if word == 'roof': del hotcat[index]

you could also use a list comprehension

hotcat = ['Cat', 'roof', 'on', 'a', 'hot', 'tin']
hotcat = [x for x in hotcat if x != 'roof']

-- Elliot Temple
http://www.curi.us/


---
[This E-mail scanned for viruses by Declude Virus]

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


Re: Moving Places, Subtracting from slices/lists

2005-06-02 Thread Mark Sargent
Hi All,

getting closer, me thinks.

 >>> hotcat = ['Cat', 'roof', 'on', 'a', 'hot', 'tin']
 >>> for x in hotcat[:]:
... if x == 'roof': hotcat.insert(6,x)
... del hotcat[x]
...
Traceback (most recent call last):
  File "", line 3, in ?
TypeError: list indices must be integers

How do I get that x to be an integer b4 it is entered into the indice.? 
Cheers.

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


Moving Places, Subtracting from slices/lists

2005-06-01 Thread Mark Sargent
Hi All,

playing around with the tut now. How can I get this code to remove the 
original instance of 'roof'.?
 >>> hotcat = ['Cat', 'roof', 'on', 'a', 'hot', 'tin']
 >>> for x in hotcat[:]:
... if x == 'roof': hotcat.insert(6,x)
...
 >>> hotcat
['Cat', 'roof', 'on', 'a', 'hot', 'tin', 'roof']

Perhaps a replace or something after the 2nd line of the for function.?
 >>> hotcat = ['Cat', 'roof', 'on', 'a', 'hot', 'tin']
 >>> for x in hotcat[:]:
... if x == 'roof': hotcat.insert(6,x)
... hotcat[x:len(x)] = []
...
Traceback (most recent call last):
  File "", line 3, in ?
TypeError: slice indices must be integers

 I feel I'm close to it.

Cheers.

Mark Sargent.

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