十三、python列表方法汇总
'''
1.append():更新列表
'''
l=[]
l.append('111')
l.append('[123,456]')
print l
--------------------------------------------
['111', '[123,456]']
---------------------------------------------------------------------------------------------
'''
2.del:删除列表
'''
l=['111','222','abc','women','[11,22,33]']
print l
del l[1]
print l
--------------------------------------------
['111', '222', 'abc', 'women', '[11,22,33]']
['111', 'abc', 'women', '[11,22,33]']
---------------------------------------------------------------------------------------------
'''
3.len():获取列表长度
'''
l=['111','666','888','abc']
print len(l)
--------------------------------------------
4
---------------------------------------------------------------------------------------------
'''
4.+:拼接列表,*用于重复列表
'''
l1=['aaa','bbb','ccc','ddd']
l2=['11','22','33']
print l1+l2
print l1*4
--------------------------------------------
['aaa', 'bbb', 'ccc', 'ddd', '11', '22', '33']
['aaa', 'bbb', 'ccc', 'ddd', 'aaa', 'bbb', 'ccc', 'ddd', 'aaa', 'bbb', 'ccc', 'ddd', 'aaa', 'bbb', 'ccc', 'ddd']
---------------------------------------------------------------------------------------------
'''
5.list(tuple):将元祖转化成列表
'''
tup=('aaa','111','abx')
print list(tup),type(list(tup))
--------------------------------------------
['aaa', '111', 'abx'] <type 'list'>
---------------------------------------------------------------------------------------------
'''
6.count():用于统计某个元素在列表中出现的次数
'''
l=['123','abc','123','aaa','bbb']
print l.count('123')
print l.count('aaa')
--------------------------------------------
2
1
---------------------------------------------------------------------------------------------
'''
7.index():用于从列表中找出某个值第一个匹配项的索引位置
'''
l=['11','222','aaa','bbb','222','cc']
print l.index('222')
print l.index('cc')
--------------------------------------------
1
5
---------------------------------------------------------------------------------------------
'''
8.extend():函数用于在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表)。
'''
l1=['11','22','33']
l2=['aa','bb','cc']
l1.extend(l2)
print l1
--------------------------------------------
['11', '22', '33', 'aa', 'bb', 'cc']
---------------------------------------------------------------------------------------------
'''
9.insert(obj,str):指定位置插入指定对象
'''
l=['11','22','33','44']
l.insert(1,['aa','bb','cc'])
print l
--------------------------------------------
['11', ['aa', 'bb', 'cc'], '22', '33', '44']
---------------------------------------------------------------------------------------------
'''
10.pop([index=-1]):移除列表中的一个元素(默认为最后一个元素),可选择元素
'''
l=['11','22','33','44']
print l
l.pop()
print l
l.pop(1)
print l
--------------------------------------------
['11', '22', '33', '44']
['11', '22', '33']
['11', '33']
---------------------------------------------------------------------------------------------
'''
11.remove(obj):用于移除列表中某个值的第一个匹配项。
'''
l=['11','222','aaa','bbb','222','cc']
l.remove('222')
print l
--------------------------------------------
['11', 'aaa', 'bbb', '222', 'cc']
---------------------------------------------------------------------------------------------
'''
12.reverse():反向列表中的元素
'''
l=['11','22','33','44']
l.reverse()
print l
--------------------------------------------
['44', '33', '22', '11']
---------------------------------------------------------------------------------------------
'''
13.sort():用于对原列表进行排序(默认升序),如果指定参数,则使用比较函数指定的比较函数。
reverse -- 排序规则,reverse = True 降序, reverse = False 升序(默认)。
'''
l=['11','222','aaa','bbb','222','cc']
l.sort()
print l
l=['11','222','aaa','bbb','222','cc']
l.sort(reverse=True)
print l
--------------------------------------------
['11', '222', '222', 'aaa', 'bbb', 'cc']
['cc', 'bbb', 'aaa', '222', '222', '11']
---------------------------------------------------------------------------------------------
当有些人一出生就有的东西,我们要为之奋斗几十年才拥有。但有一样东西,你一辈子都不会有,那就是我们曾经一无所有。