Hi,
I have a list like ['0024', 'haha', '0024']
and as output I want ['haha']
If I
myList.remove(' 0024')
then only the first instance of '0024' is removed.
It seems like regular expressions is the rescue, but I couldn't find
the right tool.
Thanks!
bahoo 37 19204
It depends on your application, but a 'set' might really be what you
want, as opposed to a list.
>>s = set(["0024","haha"," 0024"]) s
set(["0024","hah a"])
>>s.remove("002 4") s
set(["haha"])
On Apr 3, 1:31 pm, "Matimus" <mccre...@gmail .comwrote:
It depends on your application, but a 'set' might really be what you
want, as opposed to a list.
>s = set(["0024","haha"," 0024"]) s
set(["0024","hah a"])>>s.remove("00 24")
>s
set(["haha"])
If you want, you can also loop over the list, like so:
counter = 0
your_list = ["0024","haha"," 0024"]
for i in your_list:
if i == '0024':
your_list.pop(c ounter)
counter += 1
Mike
bahoo wrote:
Hi,
I have a list like ['0024', 'haha', '0024']
and as output I want ['haha']
If I
myList.remove(' 0024')
then only the first instance of '0024' is removed.
It seems like regular expressions is the rescue, but I couldn't find
the right tool.
Thanks!
bahoo
It's hard to imagine using regular expressions here. Here's a simple
attempt:
def removeall(mylis t,obj):
while obj in mylist:
mylist.remove(o bj)
Or, if you don't need the changes in place:
[x for x in mylist if x!= obj]
Tom
On Apr 3, 1:49 pm, kyoso...@gmail. com wrote:
On Apr 3, 1:31 pm, "Matimus" <mccre...@gmail .comwrote:
It depends on your application, but a 'set' might really be what you
want, as opposed to a list.
>>s = set(["0024","haha"," 0024"])
>>s
set(["0024","hah a"])>>s.remove("00 24")
>>s
set(["haha"])
If you want, you can also loop over the list, like so:
counter = 0
your_list = ["0024","haha"," 0024"]
for i in your_list:
if i == '0024':
your_list.pop(c ounter)
counter += 1
Mike
This method doesn't work.
>>li = list('hello larry') count = 0 for i in li:
.... if i=='l':
.... li.pop(count)
.... coun += 1
....
'l'
'l'
>>l
['h', 'e', 'l', 'o', ' ', 'a', 'r', 'r', 'y']
because the indexes change as you pop, it gets some of them but not
all.
On Apr 3, 1:49 pm, kyoso...@gmail. com wrote:
On Apr 3, 1:31 pm, "Matimus" <mccre...@gmail .comwrote:
It depends on your application, but a 'set' might really be what you
want, as opposed to a list.
>>s = set(["0024","haha"," 0024"])
>>s
set(["0024","hah a"])>>s.remove("00 24")
>>s
set(["haha"])
If you want, you can also loop over the list, like so:
counter = 0
your_list = ["0024","haha"," 0024"]
for i in your_list:
if i == '0024':
your_list.pop(c ounter)
counter += 1
Mike
If you want to get really fancy, you could do a list comprehension
too:
your_list = ["0024","haha"," 0024"]
new_list = [i for i in your_list if i != '0024']
Mike
On Apr 3, 12:20 pm, "bahoo" <b83503...@yaho o.comwrote:
Hi,
I have a list like ['0024', 'haha', '0024']
and as output I want ['haha']
If I
myList.remove(' 0024')
then only the first instance of '0024' is removed.
It seems like regular expressions is the rescue, but I couldn't find
the right tool.
Thanks!
bahoo
Here are a couple of ways:
target = "0024"
l = ["0024", "haha", "0024"]
------------------------------------
while(True):
try:
l.remove(target )
except ValueError:
break
print l
-------------------------------------
for index, val in enumerate(l):
if val==target:
del l[index]
print l
On Apr 3, 2:31 pm, "Matimus" <mccre...@gmail .comwrote:
It depends on your application, but a 'set' might really be what you
want, as opposed to a list.
>s = set(["0024","haha"," 0024"]) s
set(["0024","hah a"])>>s.remove("00 24")
>s
set(["haha"])
This sounds cool.
But is there a command I can convert the "set" back to a "list"?
On Apr 3, 12:13 pm, "bahoo" <b83503...@yaho o.comwrote:
On Apr 3, 2:31 pm, "Matimus" <mccre...@gmail .comwrote:
It depends on your application, but a 'set' might really be what you
want, as opposed to a list.
>>s = set(["0024","haha"," 0024"])
>>s
set(["0024","hah a"])>>s.remove("00 24")
>>s
set(["haha"])
This sounds cool.
But is there a command I can convert the "set" back to a "list"?
yeah...
>>l = list(s) l
["haha"]
bahoo wrote:
On Apr 3, 2:31 pm, "Matimus" <mccre...@gmail .comwrote:
>It depends on your application, but a 'set' might really be what you want, as opposed to a list.
>>>>s = set(["0024","haha"," 0024"]) s
set(["0024","hah a"])>>s.remove("00 24")
>>>>s
set(["haha"])
This sounds cool.
But is there a command I can convert the "set" back to a "list"?
That would be list(). So what you want is
s = set(["0024","haha"," 0024"])
s.remove("0024" )
l = list(s)
or something like it. It seems, a priori, unlikely that you only want to
remove items with that specific value, Is this part of some larger problem?
regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
Recent Ramblings http://holdenweb.blogspot.com This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Kin®sole |
last post by:
Hi
I'm very new to VB (using VB6)
I have two lists one blank and one containing names in the format of surname
and then forename.I also have a combo box containing forenames.When I
select a forename from my combo box I need to add the corresponding surname
into the blank list box.What is the best way to do this? hope this make
sense
TIA
|
by: mavis |
last post by:
Could you please help me with this xsd definition?
I need to define a set of elements occur in any order with multiple
occurrences, but the same kind of elements need to group together...
Sth like this....
<xs:element name="A">
<xs:complexType>
<xs:all>
|
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 usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
| |
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 launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |