1、len() 得出列表中有多少数据项;
student = ["zhangsan", "lisi", "wangwu", "zhangergou"] print(len(student)) print(student[0]) print(student[3])
4
zhangsan
zhangergou
2、append() 在列表末尾追加一个数据项
def append(self, *args, **kwargs): # real signature unknown """ Append a single item to the end of the bytearray. #在bytearray的末尾追加一个项。 item The item to be appended. """ pass
student.append("liudehua") print(student)
['zhangsan', 'lisi', 'wangwu', 'zhangergou', 'liudehua']
3、extend() 在列表末尾增加一个数据项集合;
def extend(self, *args, **kwargs): # real signature unknown """ Append all the items from the iterator or sequence to the end of the bytearray. #将迭代器或序列中的所有项添加到bytearray的末尾 iterable_of_ints The iterable of items to append. """ pass
student.extend(["guofucheng", "chenglong"]) print(student)
['zhangsan', 'lisi', 'wangwu', 'zhangergou', 'liudehua', 'guofucheng', 'chenglong']
*append与extend区别:
append:只是加入一项,加入集合也只是为列表的最后一项,他的序列值固定
extend:加入一个数据项集合
append:
data = [1, 2, 3] data.append([4, 5]) print(data)
[1, 2, 3, [4, 5]]
extend:
data = [1, 2, 3] data.extend([6, 7]) print(data)
[1, 2, 3, 6, 7]
4、pop() 在列表末尾删除一个数据项;
def pop(self, *args, **kwargs): # real signature unknown """ Remove and return a single item from B. #从B中移除并返回一个项。 index The index from where to remove the item. -1 (the default value) means remove the last item. If no index argument is given, will pop the last item. """ pass
student.pop() print(student)
['zhangsan', 'lisi', 'wangwu', 'zhangergou', 'liudehua', 'guofucheng']
5、remove() 在列表中删除一个特定的数据项
student.remove("guofucheng") print(student) student.remove(student[0]) print(student)
['zhangsan', 'lisi', 'wangwu', 'zhangergou', 'liudehua']
['lisi', 'wangwu', 'zhangergou', 'liudehua']
6、insert() 在特定位置前面增加一个数据项;
student.insert(0,"liangliang") print(student) student.insert(2,"liuneng") print(student)
['liangliang', 'lisi', 'wangwu', 'zhangergou', 'liudehua']
['liangliang', 'lisi', 'liuneng', 'wangwu', 'zhangergou', 'liudehua']
7、count() 统计某个数据项在列表中出现的次数
student.count("liangliang")
1
*8、copy()将数据类型进行复制
deepcopy() 我们寻常意义上的复制,复制后作为一个独立的个体存在,改变复制后的值对原来没有影响。
import copy
data = [1,2, [3,4]]
data2 = copy.deepcopy(data)
data2 [2][0] = 1
print(data)
print(data2)
[1, 2, [3, 4]]
[1, 2, [1, 4]]
copy()只复制第一层,将原来的数据打上标签,当其中一个标签变化是,两个数据都会发生改变。
import copy
data = [1,2, [3,4]]
data1 = data.copy()
data1 [2][0] = 1
print(data)
print(data1)
[1, 2, [1, 4]]
[1, 2, [1, 4]]