返回列表 發帖

串列 (二)

串列相關操作

• 顯示串列資料型態
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)
複製代碼

本帖最後由 鄭繼威 於 2022-8-19 13:18 編輯
  1. list=["信","義","和","平"]
  2. print("原始list",list)

  3. print("--新增--")
  4. list.append("apple")
  5. print("append:",list)
  6. list.insert(1,"banana")
  7. print("insert:",list)

  8. print("--刪除--")
  9. list.pop()
  10. print("pop:",list)
  11. list.remove("banana")
  12. print("remove",list)

  13. list2=[1,2,3,100,99,50,0,12,-3,-50,32,25,11]
  14. print("--排序--")
  15. list2.sort()
  16. print("sort由小到大:",list2)
  17. list2.sort(reverse=True)
  18. print("sort由大到小:",list2)

  19. print("--反向--")
  20. list2.reverse()
  21. print("list2 reverse:",list2)
  22. list.reverse()
  23. print("list reverse:",list)

  24. print("--長度--")
  25. print("list的長度",len(list))
  26. print("list2的長度",len(list2))
複製代碼

TOP

本帖最後由 陳牧謙 於 2022-8-19 13:42 編輯
  1. list=["忠","孝","仁","愛",]
  2. print("原始list",list)

  3. print("--新增--")
  4. list.append("apple")
  5. print("append:",list)
  6. list.insert(1,"banana")
  7. print("insert:",list)

  8. print("--刪除--")
  9. list.pop()
  10. print("pop:",list)
  11. list.remove("banana")
  12. print("remove:",list)


  13. list2=[1,2,3,100,99,50,0,12,-3,-50,32,25,11]
  14. print("--排序--")
  15. list2.sort()
  16. print("sort由小到大:",list2)
  17. list2.sort(reverse=True)
  18. print("sort由大到小:",list2)

  19. print("--反向--")
  20. list2.reverse()
  21. print("list2 reverse:",list2)
  22. list.reverse()
  23. print("list reverse:",list)

  24. print("--長度--")
  25. print("list的長度",len(list))
  26. print("list2的長度",len(list2))
複製代碼

TOP

  1. list=["信","義","和","平"]
  2. print("原始list",list)
  3. list.append("apple")
  4. print("append:",list)
  5. list.insert(1,"banana")
  6. print("insert:",list)
  7. list.pop()
  8. print("pop:",list)
  9. list.remove("banana")
  10. print("remove",list)
  11. list2=[1,3,5,7,9,11,13,15,17]
  12. list2.sort()
  13. print("sort由小到大:",list2)
  14. list2.sort(reverse=True)
  15. print("sort由大到小:",list2)
  16. list2.reverse()
  17. print("list2 reverse:",list2)
  18. list.reverse()
  19. print("list reverse:",list)
複製代碼

TOP

  1. "

  2. list=["信","義","和","平"]
  3. print("原list",list)


  4. list.append("watermelon")
  5. print("append:",list)
  6. list.insert(1,"fruit")
  7. print("insert:"),list


  8. list.pop()
  9. print("pop:",list)
  10. list.remove("fruit")
  11. print("remove",list)

  12. list2=[1,2,3,100,99,50,0,12,-3,-50,32,25,11]


  13. list2.sort(reverse=True)
  14. print("sort由大到小:",list2)
  15. list2.sort()
  16. print("sort由小到大:",list2)



  17. list2.reverse()
  18. print("reverse:",list2)
  19. list.reverse()
  20. print("list reverse:",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. a=[1,2,3,4,5,6,7,8,300,284,]
  2. print(a[0:20])
  3. print(a[1]+a[4]-a[8]+a[1])
  4. print(len(a))
  5. a.pop(2)
  6. print(a)
  7. a.remove(1)
  8. print(a)
  9. a.sort()
  10. print(a)
  11. a.sort(reverse=True)
  12. print(a)
  13. a.reverse()
  14. print(a)
  15. a.append("apple")
  16. print(a)
  17. a.insert(1,"banana")
  18. print(a)
複製代碼

TOP

  1. 99,100,101,102-25,53,852]
  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

返回列表