返回列表 發帖

串列 (二)

假設有一串列其內容如下:
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. 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. 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. 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. 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. 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. 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. o=[12,-3,-50,32,25,11]
  2. print("排序前",o)
  3. o.sort();
  4. print("排序後(遞增)",o)
  5. o.reverse();
  6. print("排序後(遞減)",o)
  7. print("元素數量",len(o))
  8. o.pop()
  9. print("pop掉一元素",o)
  10. o.append(-20)
  11. print("append一元素",o)
  12. o.insert(4,0)
  13. print("insert一元素",o)
複製代碼

TOP

  1. list=[12,-3,-50,32,25,11]
  2. print("list:", list)
  3. list.sort();
  4. print("numbers(least to greatest):",list)
  5. list.reverse();
  6. print("numbers(greatest to smallest):",list)
  7. print("number of numbers:",len (list))
  8. list.pop()
  9. print("popped smalest number",list)
  10. list.append(-20)
  11. print("appended a number",list)
  12. list.insert(4,0)
  13. print("inserted numbers",list)
複製代碼

TOP

  1. list=[2004,6,22,2005,2,5]
  2. print("Unsorted: ", list)
  3. list.sort()
  4. print("From least to Greatest:", list)
  5. list.reverse()
  6. print("From Greatest to Least: ", list)
  7. print("Numer of Elements: ", len(list))
  8. list. pop(5)
  9. print("After Deleting one Element: ", list)
  10. list. pop(2)
  11. print("After Deleting two Elements: ", list)
  12. list. append(9)
  13. list.sort()
  14. print("After Adding a new Element: ", list)
  15. list. append(23)
  16. list.sort()
  17. print("After Adding two new Elements: ", list)
  18. list. pop(1)
  19. print("After Deleting one Element: ", list)
  20. list. pop(0)
  21. print("After Deleting two Elements: ", list)
  22. list.insert(0,3)
  23. print("After Adding a new Element: ", list)
  24. list.insert(1,4)
  25. list.sort()
  26. print("After Adding a new Element: ", list)
複製代碼
._.

TOP

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

TOP

返回列表