I'm going to assume that it's supposed to work like this, but could
someone tell me the reasoning behind it? I.E. why is 3 skipped?
>>alist=[1,2,3] for item in alist:
.... print item
.... if item==2:
.... alist.remove(it em)
....
1
2
>>>
Bonus Question:
Can we make this behave more intuitiviely in Python 3000?
-Greg
--
Gregory Piñero
Chief Innovation Officer
Blended Technologies
( www.blendedtechnologies.com) 3 1174
I'm going to assume that it's supposed to work like this, but could
someone tell me the reasoning behind it? I.E. why is 3 skipped?
Because:
>>alist[2]
3
You are removing the third item, not the second.
bayerj wrote:
I'm going to assume that it's supposed to work like this, but could
someone tell me the reasoning behind it? I.E. why is 3 skipped?
Because:
>alist[2]
3
You are removing the third item, not the second.
This is incorrect.
You may need to remind yourself that the arg of remove is a value to be
searched for and then removed, not an index. del alist[2] would remove
the third item.
You may have been confused by the OP's obfuscatory example alist = [1,
2, 3].
Consider this equivalent:
| >>alist = ['foo', 'bar', 'zot']
| >>for item in alist:
| ... print item
| ... if item == 'bar':
| ... alist.remove(it em)
| ...
| foo
| bar
| >>alist
| ['foo', 'zot']
| >>>
HTH,
John
On 5 Sep 2006 16:05:36 -0700, bayerj <ba****@in.tum. dewrote:
I'm going to assume that it's supposed to work like this, but could
someone tell me the reasoning behind it? I.E. why is 3 skipped?
Because:
>alist[2]
3
You are removing the third item, not the second.
Actually, he's removing 2 from the list, but then the length of the
list shrinks by 1 and iteration stops.
The example would have been better if alist = ['a','b','c'] and 'b' was removed.
L.remove(value) -- remove first occurrence of value
you were possibly thinking of alist.pop(2), which removes the item
alist[2] from alist
HTH :)
--
Tim Williams This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Steven Bethard |
last post by:
George Sakkis wrote:
> "Steven Bethard" <steven.bethard@gmail.com> wrote:
>> Dict comprehensions were recently rejected:
>> http://www.python.org/peps/pep-0274.html
>> The reason, of course, is that dict comprehensions don't gain you
>> much at all over the dict() constructor plus a generator expression,
>> e.g.:
>> dict((i, chr(65+i)) for i in range(4))
>
> Sure, but the same holds for list comprehensions: list(i*i for i in
|
by: Drew |
last post by:
I have a permission tracking app that I am working on, and I have made the
insert page for it. I am having issues on how to prevent duplicates from
getting entered.
Currently the interface for the app has a mixture of select boxes, list
boxes and checkboxes. The form submits the page to processAIMR.asp and then
does the inserting. I am using a loop to insert a new record for each
checkbox checked or listbox entry selected.
My...
|
by: Jani Yusef |
last post by:
Based on an interview question I heard of but did not know the answer
to.......
How do you find and remove a loop from a singly linked list?
In a google groups search I found the following code which will detect
the loop but I am stumped how one would remove this loop.
Any ideas?
typedef enum { FALSE, TRUE } bool;
|
by: Jeremy Owens-Boggs |
last post by:
We are trying to implement a dual list box selection where you have two list
boxes, You highlight items in the right side list box, click a button and
this moves those items over to the left hand list box. The problem is that
if there are many items selected (thousands), then removing the items from
the right side list box takes for ever because we are removing them one at a
time, which causes the listbox to re-index everything before we...
|
by: Frawls |
last post by:
Hi,
This is concerned with System.Web.UI.WebControls.DropDownList
I am having problems creating a method which will remove list items
from a preloaded dropdownlist. This dropdown is loaded when the page
loads and the items are pulled from a database.
I want to create a method which is called on the page load and will
remove certain items from the dropdown list. I want the method to loop
| |
by: RyanTaylor |
last post by:
I have a final coming up later this week in my beginning Java class and my
prof has decided to give us possible Javascript code we may have to write.
Problem is, we didn't really cover JS and what we covered was within the
last week of the class and all self taught.
Our prof gave us an example of a Java method used to remove elements from an
array:
public void searchProcess()
{ int outIt=0;
|
by: Eric_Dexter |
last post by:
def simplecsdtoorc(filename):
file = open(filename,"r")
alllines = file.read_until("</CsInstruments>")
pattern1 = re.compile("</")
orcfilename = filename + "orc"
for line in alllines:
if not pattern1
print >>orcfilename, line
I am pretty sure my code isn't close to what I want. I need to be able
|
by: gnassar |
last post by:
Essentially my problem is that .NET 2005 is removing my event
handlers. There's no real special things about my project, it just
continually removes them all.
It starts on the open of a solution. The screen flashes and
immediately I can undo something. I look at the undo list and it says
designer generated code. When I undo it, all of my event handlers come
back. This is a very annoying issue has anyone solved this problem?
-G
|
by: =?Utf-8?B?Sm9lbCBNZXJr?= |
last post by:
I have created a custom class with both value type members and reference type
members. I then have another custom class which inherits from a generic list
of my first class. This custom listneeds to support cloning:
Public Class RefClass
Public tcp As TcpClient
Public name As String
End Class
Public Class RefClassList
Inherits List(Of RefClass)
Implements ICloneable
|
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |