Python Quick Start Programming Chapter 5 Programming Questions

Given the lists li_num1 = [4,5,2,7] and li_num2 = [3,6], please merge these two lists into one list and sort the elements in the merged list in descending order

  1. Use the extend() method to add all the elements of another list at the end of the list at once
  2. Use the sort() method to arrange the elements in a list in a specific order

Sample code:

#python第5章编程第一题

li_num1 = [4,5,2,7]         #创建li_num1和li_num2列表
li_num2 = [3,6]
li_num1.extend(li_num2)     #用extend()函数将li_num2加入li_num1的末尾
li_num1.sort(reverse=True)  #用sort()函数将列表中的元素按降序排列
                    #参数reverse用于控制列表元素排列方式,参数可取值True(降序)或Flase(升序)
print(li_num1)

operation result:

Guess you like

Origin blog.csdn.net/ZcRook1e/article/details/124833213