Introduction to Python Programming (007) - List Operations (2): Sorting and statistical operations of list elements

Introduction to Python Programming (007) - List Operations (2): Sorting and statistical operations of list elements

1. Sorting of list elements

1. Use the sort() method to sort list elements

The sort() method is case-sensitive. Sort items whose first letter is uppercase first, then sort items whose first letter is lowercase. The sort() method changes the order of the original list.

The syntax format of the sort() method is as follows:

listname.sort(key=NONE[,reverse=False])

illustrate:

(1) The default sorting rule is: sort uppercase letters first, then sort lowercase letters. Ascending.

(2) You can set the key = str.lower parameter to ignore case.

(3) Set reverse = True to achieve descending order.

For example:

list1=["Jack",'Mark',"tom","Black","jerry","White"]
list1.sort()
print(list1)
list1.sort(key=str.lower)
print(list1)
list1.sort(key=str.lower,reverse=True)
print(list1)

运行结果如下:
===================== RESTART: C:\Python\Python38\First.py =====================
['Black', 'Jack', 'Mark', 'White', 'jerry', 'tom']
['Black', 'Jack', 'jerry', 'Mark', 'tom', 'White']
['White', 'tom', 'Mark', 'jerry', 'Jack', 'Black']

2. Use the sorted() function to sort the list

The sorted() function sorts the list and assigns it to other lists without changing the order of the elements in the original list. The syntax format is as follows:

listname_new = sorted(listname, key=NONE, reverse=False)

For example:

list1 = ["Jack",'Mark',"tom","Black","jerry","White"]
list2 = sorted(list1)
list3 = sorted(list1,key=str.lower)
list4 = sorted(list1,reverse=True)
print(list1)
print(list2)
print(list3)
print(list4)

运行结果如下:
===================== RESTART: C:\Python\Python38\First.py =====================
['Jack', 'Mark', 'tom', 'Black', 'jerry', 'White']
['Black', 'Jack', 'Mark', 'White', 'jerry', 'tom']
['Black', 'Jack', 'jerry', 'Mark', 'tom', 'White']
['tom', 'jerry', 'White', 'Mark', 'Jack', 'Black']

3. Use the reverse() method to flip the list

The reverse() method sorts the list in reverse order. The syntax is as follows:

listname.reverse()

For example:

list1 = ["Jack",'Mark',"tom","Black","jerry","White"]
list1.reverse()
print(list1)

运行结果如下:
===================== RESTART: C:\Python\Python38\First.py =====================
['White', 'jerry', 'Black', 'tom', 'Mark', 'Jack']

2. Statistical operations on elements in the list

1. Count the number of elements in the list

Use the len() function to count the number of elements in a list. The syntax format is as follows:

len(listname)
list1 = ["Jack",'Mark',"tom","Black","jerry","White"]
str1="I'm a student."
print(len(list1))
print(len(str1))

运行结果如下:
===================== RESTART: C:\Python\Python38\First.py =====================
6
14

2. Count the number of times an element appears in the list

Use the count() method to count the number of occurrences of an element. The syntax format is as follows:

listname.len()

For example:

list1 = [2,3,5,7,11,13,13]
print(list1.count(3))
print(list1.count(13))

运行结果如下:
===================== RESTART: C:\Python\Python38\First.py =====================
1
2

3. Find the index of the specified value in the list

Use the index() method to return the index of the first element with the specified value. The syntax format is as follows:

listname.index(元素)

For example:

list1 = [2,3,5,7,11,13,13,5,7,25,7,29]
print(list1.index(7))     #第一个7的索引为3 
print(list1.index(7,4))   #第4个元素开始,第一个7为8

运行结果如下:
===================== RESTART: C:\Python\Python38\First.py =====================
3
8

4. Sum the elements in the list

Use the sum() function to sum the elements in a list. The syntax format of the sum() function is as follows:

sum(listname)

For example:

list1 = [2,3,5,7,11,13,13,5,7,25,7,29]
print(sum(list1))          # 对所有元素求和
print(sum(list1[1:4]))     # 对[3,5,7]求和
print(sum(list1[-4:-1]))   # 对[7,25,7]求和

运行结果如下:
===================== RESTART: C:\Python\Python38\First.py =====================
127
15
39

5. Use for loop to print list elements

The syntax format is as follows:

for x in 列表名: 
    print(x)

For example:

list1 = [1,2,3,4,5]
list2 = ["Jack","Mark","Tom","Jerry","Kate"]
for item1 in list1:print(item1)
for item2 in list2:print(item2)
    
运行结果如下:
===================== RESTART: C:\Python\Python38\First.py =====================
1
2
3
4
5
Jack
Mark
Tom
Jerry
Kate

Use the zip() function to pack an iterable object, pack the corresponding elements in the object into tuples, and then return a list composed of these tuples. The syntax is as follows:

zip(iterable, ...)

For example:

list1 = [1,2,3,4,5]
list2 = ["Jack","Mark","Tom","Jerry","Kate"]
list3 = [["20200214111","刘明"],["20200214112","张静静"],["20200214113","刘宏"]]
list4 = [25,30,28]
for item1,item2 in zip(list1,list2):
    print(str(item1)+"."+item2)
for item3,item4 in zip(list3,list4):
    print("学号:"+item3[0]+"\t姓名:"+item3[1]+"\t年龄:"+str(item4))
    
运行结果如下:
===================== RESTART: C:\Python\Python38\First.py =====================
1.Jack
2.Mark
3.Tom
4.Jerry
5.Kate
学号:20200214111	姓名:刘明	年龄:25
学号:20200214112	姓名:张静静	年龄:30
学号:20200214113	姓名:刘宏	年龄:28

Guess you like

Origin blog.csdn.net/weixin_44377973/article/details/132122395