473,785 Members | 2,811 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

after del list , when I use it again, prompt 'not defined'.how could i delete its element,but not itself?

after del list , when I use it again, prompt 'not defined'.how could i
delete its element,but not itself?
except list.remove(val ) ,are there other ways to delete list 's
elements?

and , I wrote:

list1=[]
def method1():
global list1
list1=['a','b','c','d']
def method2():
list2=list1
list2.remove['a']
main():
global list1
method1()
method2()
print list1[0]

it prints 'b'.
How could I keep the list1 not to change when remove list2's elements?

Jun 2 '06 #1
7 1356
python wrote:
after del list , when I use it again, prompt 'not defined'.how could i
delete its element,but not itself?


This is a way:
a = range(10)
del a[:]
a [] a.append(20)
a

[20]

Bye,
bearophile

Jun 2 '06 #2
python wrote:

[snip]

How could I keep the list1 not to change when remove list2's elements?


You can't when the names list1 and list2 refer to the same list. Try
making list2 a copy of list1,

list2 = list(list1)

Duncan
Jun 2 '06 #3
be************@ lycos.com wrote:
python wrote:
after del list , when I use it again, prompt 'not defined'.how could i
delete its element,but not itself?


This is a way:
a = range(10)
del a[:] or simply
a = [] a []

a.append(20)
a

[20]

Bye,
bearophile

Jun 2 '06 #4
python a écrit :
after del list , when I use it again, prompt 'not defined'.how could i
delete its element,but not itself?
except list.remove(val ) ,are there other ways to delete list 's
elements?

and , I wrote:

list1=[]
def method1():
Why naming a function "method" ?
global list1
globals are evil.
list1=['a','b','c','d']
def method2():
list2=list1
this makes list2 point to the same object as list1.
list2.remove['a']
main():
global list1
method1()
method2()
print list1[0]

it prints 'b'.
Of course. Python's "variable" are really just names referencing
objects. Making two names pointing to the same object does'nt make two
different objects, just two references to one object.

How could I keep the list1 not to change when remove list2's elements?


Copy the list.
Jun 2 '06 #5
python a écrit :
after del list , when I use it again, prompt 'not defined'.how could i
delete its element,but not itself?


FWIW, del don't delete an object (not directly at least), it just delete
the name<->object association. If (and only if) it was the only name
referencing that object, then the object will be disposed too.
list1 = [1, 2]
list2 = list1
id(list1) 1078043852 id(list2) 1078043852 del list2
list1 [1, 2] list2 Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name 'list2' is not defined

Jun 2 '06 #6
>>>>> SuperHik <ju************ @gmail.com> (S) escribió:
S> be************@ lycos.com wrote:
python wrote:
after del list , when I use it again, prompt 'not defined'.how could i
delete its element,but not itself?

This is a way:
>> a = range(10)
>> del a[:]S> or simply
S> a = []>> a
[]
Then you *have* deleted the list and created a new one, which is different
from keeping the list and deleting the elements:
a = range(10)
b = a
del a[:]
a [] b [] a.append(100)
a [100] b [100] a=[]
b [100]

--
Piet van Oostrum <pi**@cs.uu.n l>
URL: http://www.cs.uu.nl/~piet [PGP 8DAE142BE17999C 4]
Private email: pi**@vanoostrum .org
Jun 2 '06 #7
Piet van Oostrum a écrit :
>>SuperHi k <ju************ @gmail.com> (S) escribió:
S> be************@ lycos.com wrote:
python wrote:

>after del list , when I use it again, prompt 'not defined'.how could i
>delete its element,but not itself?

This is a way:

>>>a = range(10)
>>>del a[:]
S> or simply
S> a = []
>>>a

[]

Then you *have* deleted the list


Not necessarily:
a = range(10)
b = a
id(a) 1078034316 id(b) 1078034316 a is b True a = []
b [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] id(a) 1078043724 id(b) 1078034316


What as been deleted is the association ('binding') between the name 'a'
and the list object at 1078034316. This object won't be suppressed until
there are no more names bound to it.
and created a new one, which is different
from keeping the list and deleting the elements:


indeed !-)

Jun 3 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
2216
by: Generic Usenet Account | last post by:
To settle the dispute regarding what happens when an "erase" method is invoked on an STL container (i.e. whether the element is merely removed from the container or whether it also gets deleted in the process), I looked up the STL code. Erase certainly does not delete the memory associated with the element. However, it appears that the destructor on the element is invoked. I wonder why it has to be this way. In my opinion, this renders...
4
3604
by: JS | last post by:
I have a file called test.c. There I create a pointer to a pcb struct: struct pcb {   void *(*start_routine) (void *);   void *arg;   jmp_buf state;   int    stack; };   struct pcb *pcb_pointer;
16
13617
by: Brian D | last post by:
I have a multiple select list that is created dynamically based on a previous selection on an asp page. The first thing I do is to clear the curent option list by document.form1.itemcross.length = 0; The only problem is that it leaves the optgroups. How do I also get rid of the optgroups? Thanks
1
7294
by: gnawz | last post by:
Hi guys, I have a couple of php files that perform various tasks. I will use fields in my system and provide code as well I need help as follows: My database contains the fields Category and Brand I need to make some changes on a a number of brands in a Category
0
9645
marktang
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...
0
9480
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,...
0
10329
Oralloy
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...
0
10152
jinu1996
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...
0
9950
tracyyun
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...
1
7500
isladogs
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...
0
6740
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5381
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...
3
2880
bsmnconsultancy
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...

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.