列表的增删改查
list1 = list('this is a list')
增加
list1.append('!')
list1.insert(2,'this is index 3')
删除
list1.pop(-1)
del list1[-1]
if '!' in list1:
list1.remove('!')
修改
list1[0] = '0'
list1[0:2] = list('05')
list1[1:1] = list('1234')
list1[1:5] = []
查找
查找下标
if 'a' in list1:
index = list1.index('a')
拼接列表
list2 = ['new','list']
list1.extend(list2)
print list1
倒序
list1.reverse();
print list1
吧重复的去掉
l1 = ['b','c','d','c','a','a']
l2 = []
for i in l1:
if not i in l2:
l2.append(i)
print (l2)
元组 ,
元组不能修改
创建
tuple1 = ()
tuple1 = 1,
tuple1 = 1,2,3
tuple1 = tuple([1,2,3,4])
print (tuple1)
网友评论