涓句緥璇存槑:
>>> str=[1,2,3,4,5,2,6]
>>> str.remove(2)
>>> str
[1, 3, 4, 5, 2, 6]
2.pop: 鍒犻櫎鍗曚釜鎴栧涓厓绱狅紝鎸変綅鍒犻櫎(鏍规嵁绱㈠紩鍒犻櫎)
>>> str=[0,1,2,3,4,5,6]
>>> str.pop(1) #pop鍒犻櫎鏃朵細杩斿洖琚垹闄ょ殑鍏冪礌
>>> str
[0, 2, 3, 4, 5, 6]
>>> str2=['abc','bcd','dce']
>>> str2.pop(2)
'dce'
>>> str2
['abc', 'bcd']
3.del锛氬畠鏄牴鎹储寮(鍏冪礌鎵鍦ㄤ綅缃)鏉ュ垹闄
涓句緥璇存槑:
>>> del str[1]
>>> str
[1, 3, 4, 5, 2, 6]
>>> str2=['abc','bcd','dce']
>>> del str2[1]
>>> str2
['abc', 'dce']
闄ゆ涔嬪锛宒el杩樺彲浠ュ垹闄ゆ寚瀹氳寖鍥村唴鐨勫笺
>>> str=[0,1,2,3,4,5,6]
>>> del str[2:4] #鍒犻櫎浠庣2涓厓绱犲紑濮嬶紝鍒扮4涓负姝㈢殑鍏冪礌(浣嗘槸涓嶅寘鎷熬閮ㄥ厓绱)
>>> str
[0, 1, 4, 5, 6]
del 涔熷彲浠ュ垹闄ゆ暣涓暟鎹璞(鍒楄〃銆侀泦鍚堢瓑)
>>> str=[0,1,2,3,4,5,6]
>>> del str
>>> str #鍒犻櫎鍚庯紝鎵句笉鍒板璞
Traceback (most recent call last):
File "<pyshell#27>", line 1, in <module>
str
NameError: name 'str' is not defined
娉ㄦ剰锛歞el鏄垹闄ゅ紩鐢(鍙橀噺)鑰屼笉鏄垹闄ゅ璞(鏁版嵁)锛屽璞$敱鑷姩鍨冨溇鍥炴敹鏈哄埗锛圙C锛夊垹闄ゃ
琛ュ厖: 鍒犻櫎鍏冪礌鐨勫彉鐩告柟娉
s1=(1,2,3,4,5,6)
s2=(2,3,5)
s3=[]
for i in s1:
if i not in s2:
s3.append(i)
print 's1-1:',s1
s1=s3
print 's2:',s2
print 's3:',s3
print 's1-2:',s1
浠ヤ笂灏辨槸Python 鍒犻櫎List鍏冪礌鐨勪笁绉嶆柟娉時emove銆乸op銆乨el鐨勮缁嗗唴瀹癸紝鏇村淇℃伅璇峰叧娉∣D浜戝叾瀹冪浉鍏虫枃绔狅紒