返回列表 發帖

串列 (二)

串列相關操作

• 顯示串列資料型態
type()
• 顯示串列元素數量
len()

• 串列元素新增
append()->新增在最後
insert(n,value)->指定位置新增

• 串列元素刪除
pop()->刪除最後一個(也可以指定(idx)移除)
remove(value)->指定刪除(指定(value)移除)

• 串列元素排序
sort()->預設reverse=0(不反向)
(reverse=1->降冪(由大到小))



假設有一串列其內容如下:
list=[12,-3,-50,32,25,11]

試以sort()、reverse()、len()、pop()、append()、insert()等函式,完成如下操作:

  1. list=[12,-3,-50,32,25,11]
  2. print("排序前:",list)
  3. list.sort()
  4. print("排序後(遞增):",list)
  5. list.reverse()
  6. print("排序後(遞減):",list)
  7. print("元素數量:",len(list))
  8. list.pop()
  9. print("pop掉一元素",list)
  10. list.append(-20)
  11. print("append一元素",list)
  12. list.insert(4,0)
  13. print("insert一元素",list)
複製代碼

  1. # -*- coding: utf-8 -*-
  2. """
  3. Spyder Editor

  4. This is a temporary script file.
  5. """
  6. list1 = [2,1,"忠","孝","仁","愛","信","義","和","平"]
  7. print("原始list1",list1)
  8. #list新增
  9. list1.append(-20)   #最後+
  10. print("append一元素",list1)
  11. list1.insert(1,5)   #指定+
  12. print("insert一元素",list1)


  13. #list清除
  14. list1.pop() #最後-
  15. print("pop一元素",list1)
  16. list1.remove(5) #指定-
  17. print("remove一元素",list1)


  18. list2=[12,-3,-50,32,25,11]


  19. print("原始list2:",list2)
  20. list2.sort(reverse=1)
  21. print("排序list2:",list2)        
複製代碼

TOP

  1. list=[12,-5,78,-4,34,35]
  2. print(f"原始list{list}")
  3. list.append(20)
  4. print(f"append一元素{list}")
  5. list.insert(0,8)
  6. print(f"insert一元素{list}")
  7. list.pop()
  8. print(f"pop一元素{list}")
  9. list.remove(-4)
  10. print(f"remove一元素{list}")
  11. list.sort(reverse=1)
  12. print(f"排序{list}")
  13. list.sort(reverse=0)
  14. print(f"排序{list}")
複製代碼

TOP

  1. list=[12,-3,-50,32,25,11]
  2. print("排序前:",list)
  3. list.sort()
  4. print("排序後(遞增):",list)
  5. list.reverse
  6. print("排序後(遞減):",list)
  7. print("元素數量:",len(list))
  8. list.pop()
  9. print("pop掉一元素",list)
  10. list.append(-20)
  11. print("append一元素",list)
  12. list.index
  13. print("index一元素",list)
複製代碼

TOP

  1. list=[12,-3,-50,32,25,11]
  2. print("排序前:",list)
  3. list.sort()
  4. print("排序後(遞增):",list)
  5. list.reverse()
  6. print("排序後(遞減):",list)
  7. print("元素數量:",len(list))
  8. list.pop()
  9. print("pop掉一元素:",list)
  10. list.append(-20)
  11. print("append掉一元素:",list)
  12. list.insert(4,0)
  13. print("insert掉一元素",list)
複製代碼

TOP

  1. list=[12,-3,-50,32,25,11]
  2. print("排序前:",list)
  3. list.sort()
  4. print("排序後(遞增)",list)
  5. list.reverse()
  6. print("排序後(遞減)",list)
  7. print("元素數量",len(list))
  8. list.pop()
  9. print("pop掉一元素",list)
  10. list.append(-20)
  11. print("append一元素",list)
  12. list.insert(4,0)
  13. print("insert一元素",list)
複製代碼

TOP

  1. list1 = [2,1,"忠","孝","仁","愛","信","義","和","平"]
  2. print("原始list1",list1)
  3. #list新增
  4. list1.append(-20)   #最後+
  5. print("append一元素",list1)
  6. list1.insert(1,5)   #指定+
  7. print("insertt一元素",list1)

  8. #list刪除
  9. list1.pop() #最後-
  10. print("pop一元素",list1)
  11. list1.remove(5) #指定-
  12. print("remove一元素",list1)

  13. list2=[12,-3,-50,32,25,11]

  14. print("原始list2:",list2)
  15. list2.sort(reverse=1)
  16. print("排序list2:",list2)
複製代碼

TOP

返回列表