split("/")[-1] 和 split("/",-1)的区别
转载自:https://blog.csdn.net/jialibang/article/details/84989279
#coding=utf-8 str="http://www.runoob.com/python/att-string-split.html" print("0:%s"%str.split("/")[-1]) print("1:%s"%str.split("/")[-2]) print("2:%s"%str.split("/")[-3]) print("3:%s"%str.split("/")[-4]) print("4:%s"%str.split("/")[-5]) print("5:%s"%str.split("/",-1)) print("6:%s"%str.split("/",0)) print("7:%s"%str.split("/",1)) print("8:%s"%str.split("/",2)) print("9:%s"%str.split("/",3)) print("10:%s"%str.split("/",4)) print("11:%s"%str.split("/",5))
结果是:
0:att-string-split.html 1:python 2:www.runoob.com 3: 4:http: 5:['http:', '', 'www.runoob.com', 'python', 'att-string-split.html'] 6:['http://www.runoob.com/python/att-string-split.html'] 7:['http:', '/www.runoob.com/python/att-string-split.html'] 8:['http:', '', 'www.runoob.com/python/att-string-split.html'] 9:['http:', '', 'www.runoob.com', 'python/att-string-split.html'] 10:['http:', '', 'www.runoob.com', 'python', 'att-string-split.html'] 11:['http:', '', 'www.runoob.com', 'python', 'att-string-split.html']
即: -1 :全切 0 :不切 1:切一刀 (每一块都保留)
[-1]:将最后一块切割出来 [-2]:将倒数第二块切割出来 (只保留切出来的一块)