1、append:增加元素到列表尾部
L.append(object) -> None -- append object to end
2、clear:清空列表中所有元素
3、count:返回列表中指定值的数量
L.count(value) -> integer -- return number of occurrences of value
4、extend:用列表扩展列表的元素
#L.extend(iterable) -> None -- extend list by appending elements from the iterable
l1 = [1,2,3]
l2 = [3,4,5]
l1.extend(l2)
print(l1)
5、index:返回指定元素的索引位置
#L.index(value, [start, [stop]]) -> integer -- return first index of value
lx = ['today','is','a','good','day']
print(lx.index('is'))
结果:
6、insert:在指定索引位置插入
#L.insert(index, object) -- insert object before index
lx = ['today','is','a','good','day']
lx.insert(,'not')
print(lx)
结果:['today', 'is', 'not', 'a', 'good', 'day']
7、pop:删除列表指定位置的元素,默认为最后一个元素
#L.pop([index]) -> item -- remove and return item at index (default last)
lx = ['today','is','a','good','day']
lx.pop()
print(lx)
结果:['today', 'is', 'good', 'day']
8、remove:删除列表中指定的元素
#L.remove(value) -> None -- remove first occurrence of value
lx = ['today','is','a','good','day']
lx.remove('a')
print(lx)
结果:['today', 'is', 'good', 'day']
9、reverse:将列表中所有元素反转
#L.reverse() -- reverse *IN PLACE*
lx = ['today','is','a','good','day']
lx.reverse()
print(lx)
结果:['day', 'good', 'a', 'is', 'today']
10、sort:排序
#原址排序
x= [,,,,,,,]
x.sort()
print(x)
结果:[, , , , , , , ] #需要一个排序好的副本,通过切片方法将x赋值给y
x= [,,,,,,,]
y = x[:]
y.sort()
print(x)
print(y)
结果:
[, , , , , , , ]
[, , , , , , , ] #再一种通过sorted
x= [,,,,,,,]
y = sorted(x)
print(x)
print(y)
结果:
[, , , , , , , ]
[, , , , , , , ]
11、filter:使用一个自定义的涵数过滤一个序列,把函数的每一项传到自定义的函数里处理,最终一次性返回过滤的结果
def func(x):
if x >:
return True li = [,,,,]
new_list = filter(func,li)
print(list(new_list))
结果:[44,55]
12、zip:接受任意多个序列,返回一个元组
x = [,,]
y = [,,]
z = [,,]
zipped = zip(x,y,z)
yy = list(zipped)
xyz =zip(*yy) #zip的反函数
print(yy)
print(list(xyz))
结果:
[(, , ), (, , ), (, , )]
[(, , ), (, , ), (, , )]
13、tell:查看当前指针位置
seek:定位当前指针位置
'''test.llog
孔kongzhagen
''' f = open('test.log','r+',encoding='utf-8')
f.read()
print(f.tell()) #查看当前指针位置 f.seek()
f.read()
print(f.tell())
结果:,
14、truncate: truncate(n): 从文件的首行首字符开始截断,截断文件为n个字符;无n表示从当前位置起截断;截断之后n后面的所有字符被删除。其中win下的换行代表2个字符大小。
'''
# Assuming file has following lines
# This is 1st line
# This is 2nd line
# This is 3rd line
# This is 4th line
# This is 5th line
'''
f = open("test.log","r+")
print('Name of file %s'%f.name)
line = f.readline()
print('Read line:%s'%line)
f.truncate(f.tell()) #文件保留了第一行,其余被截断
line = f.readline()
print('Read line:%s'%line)
f.close()
结果:
Name of file test.log
Read line:# Assuming file has following lines Read line:# This is 1st line
其它
numList = [,,,,,]
strList = ['a','b','c','d','e']
numList.append() # numList列表增加一个值
print numList
print numList.count() # 列表中2出现的次数
numList.extend(strList) # strList的内容增加到numList后面
print numList
print numList.index() # numList中23的索引位置
numList.insert(,) # 在numList的3索引位置添加45
print numList
print numList.pop() # 获取并删除numList最后一个值
numList.remove() # 从numList中删除4
print numList
numList.reverse() # 获取numList的反向列表
print numList