1.remove: 鍒犻櫎鍗曚釜鍏冪礌锛屽垹闄ら涓鍚堟潯浠剁殑鍏冪礌锛屾寜鍊煎垹闄わ紝浠庡乏鍚戝彸渚濇鍒犻櫎绗﹀悎鏉′欢鐨勫
涓句緥璇存槑:

>>> 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锛氬畠鏄牴鎹储寮(鍏冪礌鎵鍦ㄤ綅缃)鏉ュ垹闄

涓句緥璇存槑:


>>> str=[1,2,3,4,5,2,6]
>>> 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浜戝叾瀹冪浉鍏虫枃绔狅紒



鏈枃URL锛http://www.odweb.cn/news_show.html?id=218