473,395 Members | 1,969 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 1337
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.nl>
URL: http://www.cs.uu.nl/~piet [PGP 8DAE142BE17999C4]
Private email: pi**@vanoostrum.org
Jun 2 '06 #7
Piet van Oostrum a écrit :
>>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


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
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...
4
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; }; ...
16
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 ...
1
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...
0
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...

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.