Board logo

標題: 串列 (二) [打印本頁]

作者: tonyh    時間: 2018-7-14 09:40     標題: 串列 (二)

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

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

[attach]4450[/attach]
  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)
複製代碼

作者: 曲書辰    時間: 2018-7-14 09:56

本帖最後由 曲書辰 於 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)
複製代碼

作者: 古蕾娜    時間: 2018-7-14 09:56

  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)
複製代碼

作者: 古昇暘    時間: 2018-7-14 10:00

  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)
複製代碼





歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/) Powered by Discuz! 7.2