Python list list
(The following is a comparison of the primary view for white notes)
I. Introduction:
|
|
|
Examples are as follows
[1, 2, 3]
[ 'awng', 'NIER', 'smooth']
[ 'pay', 3.6, 100, [10, 20]]
[[ 'Fil', 210], [ 'file', 20 , 7]]
Second, the use
Example: students in a student: Zhang godsend, white, Xiao Ming, red, Liu Fei,
: There are digital numbers: 1,2,3,9,5,6
I first code words we put these names and numbers stored into a value in
(Enter the code for the Orange, green as a comment)
student = [ "Zhang heaven-sent", "white", "Xiao Ming", "red", "Liu Fei"] // credited to the student's name these few years, in order to list the format \
= of numer [1,2,3,4,5,6] // number stored in these few numbers
----------------------------------- dividing lines, this is the method, this is an example of the
1. the output value of any position.
Print (Student [0]) // Student [] brackets is 0, the output list the first one, is an output of the second, and so on
Output: Zhang providential
2.append method: At the end of the new
append("张天赐")//执行的话,就已经在末尾新增了个名字为张天赐的学生
print(student)//这时候我们执行下,看看张天赐有没有在列表的末尾新增成功
输出结果 ["张天赐",“小白”,“小明“,”小红“,”刘飞”,“张天赐”]
3.count方法:假如这时候我不知道student里有几个名字为张天赐的学生,我想统计下
print(count(张天赐))//count()括号里为谁,就是统计谁
输出结果:2
4.remove方法删除指定元素
print(student)//输出下,查看张天赐有没有被删除
输出结果:['小明', '小红', '刘飞']
5.insert方法:在任意位置新增列表元素
student.insert(0,"白起")//在第0个位置新增了白起,那么白起会在0位置,0位置之前的人会往后移, 列表名字.insert(位置,“要插入的内容”)
print(student)//查看下输出结果
输出结果:['白起', '张天赐', '小明', '小红', '刘飞']
6.pop方法:删除任意位置的列表元素
11.len方法:查看list有多少个元素
14.clear方法:清空列表
student.clear()//清空student列表
输出结果:[]
本次分享就到这里了,欢迎大家补充~~