python commonly used method in the list

                                                                           Python list list   

(The following is a comparison of the primary view for white notes)

  I. Introduction:

  • Python list is built ordered, variable sequence, all the elements of a list on a pair of brackets "[]" in, and be separated by commas;
  • The list can be CRUD, every operation, will fill in the position of the full list to ensure that there is no gap in the list
  • list can be stored integer, decimal, string and the like, or even a list of tuples, dictionaries, set, it is a strong list, and a means of supporting multiple types.

 

 

 

 

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方法删除指定元素

student.remove("张天赐") //remove()括号中填写列表中谁,就删除谁

 print(student)//输出下,查看张天赐有没有被删除

输出结果:['小明', '小红', '刘飞']

 

5.insert方法:在任意位置新增列表元素

 student.insert(0,"白起")//在第0个位置新增了白起,那么白起会在0位置,0位置之前的人会往后移,    列表名字.insert(位置,“要插入的内容”)

print(student)//查看下输出结果

输出结果:['白起', '张天赐', '小明', '小红', '刘飞']

 

6.pop方法:删除任意位置的列表元素

student.pop(0)    //0表示位置,删除排序为第0个的人,如果pop(n),则删除第n个元素
print(student) //查看输出结果
输出结果:['小明', '小红', '刘飞']
 
7.+号组合两个列表
print(number + student)   //输出两个列表里的元素
输出结果:[1, 2, 3, 9, 5, 6, '张天赐', '小明', '小红', '刘飞']
 
8.reverse()顺序翻转
student.reverse() //student列表里的字段会反过来排序
print(student) //输出查看一下
输出结果:['刘飞', '小红', '小明', '张天赐']
 
9.sort排序方法:可以升序可以降序,不过只针对于数字,此例子不适用student列表
number.sort()  //让列表number进行升序排序
print(number)  //输出,查看下结果
输出结果:[1,2,3,5,6,9]
number.sort(reverse=True) //reverse = true的话,会降序排序
输出结果:[9,6,5,3,2,1]
 
10.sorted()方法:不会改变原列表的顺序
print(sorted(number)) //直接输出number,是升序排序的
print(number)    //输出原来的number,是原来的顺序
输出结果:1,2,3,5,6,9
                  1,2,3,9,5,6

11.len方法:查看list有多少个元素

print(len(student)) //直接输出student列表中的元素数量
输出结果:4
 
12.mxa方法:查看list中元素最大的值
print(max(number)) //查看number列表中
输出结果:9
13.min方法:查看list中元素最小的值
print(min(number)) //查看number列表中
输出结果:1
 

 14.clear方法:清空列表

student.clear()//清空student列表

输出结果:[]

 

 

本次分享就到这里了,欢迎大家补充~~

Guess you like

Origin www.cnblogs.com/ztcbug/p/12050572.html