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

P

python

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?
 
D

Duncan Smith

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
 
P

Piet van Oostrum

SuperHik said:
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 = []

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]
 
B

Bruno Desthuilliers

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.
 
B

Bruno Desthuilliers

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
>>> list1 = [1, 2]
>>> list2 = list1
>>> id(list1) 1078043852
>>> id(list2) 1078043852
>>> del list2
>>> list1 [1, 2]
>>> list2
Traceback (most recent call last):
 
B

Bruno Desthuilliers

Piet van Oostrum a écrit :
SuperHik <[email protected]> (S) escribió:

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 = []


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 !-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top