返回列表 發帖

串列 (二)

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

試以sort()、reverse()、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. list=[12,-3,-50,32,25,11]
  2. print("before list: ",list)
  3. list.sort();
  4. print("after list(ascending): ",list)
  5. list.reverse();
  6. print("after list(ascending): ",list)
  7. print("ammount: ",len(list))
  8. list.pop()
  9. print("pop one ammount: ",list)
  10. list.append(-20);
  11. print("append one ammount: ",list)
  12. list.insert(4,0);
  13. print(*"isert one ammount: ",list)
複製代碼

TOP

  1. list=[42,95,-69,7,-22]
  2. print("Before sort: ",list)
  3. list.sort()
  4. print("After sort(ascending): ",list)
  5. list.reverse()
  6. print("Before sort(descending): ",list)
  7. print("Number of values:",len(list))
  8. list.pop()
  9. print("pop0 away one value",list)
  10. list.append(-20)
  11. print("append away one value",list)
  12. list.insert(4,0)
  13. print("insert one value",list)
複製代碼

TOP

本帖最後由 曲書辰 於 2018-7-14 09:57 編輯
  1. list=["A","B","C","D","e","G","F"]
  2. print("排序前",list)
  3. list.sort();
  4. print("排序後",list)
  5. list.reverse();
  6. print("排序後",list)
  7. print("排序後",list)
  8. print("元素數量",len(list))
  9. list.pop()
  10. print("pop掉一元素",list)
  11. list.append(-20)
  12. print("append一元素",list)
  13. list.insert(4,0)
  14. print("insert一元素",list)
複製代碼

TOP

返回列表