返回列表 發帖

串列 (二)

假設有一串列其內容如下:
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. a=[]
  2. for i in range(1,100,2):
  3.     a.insert(0,i)
  4. for b in a:
  5.     print(b)
複製代碼

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("刪除一元素",list)
  10. list.append(-20)
  11. print("增加一元素",list)#加到最後
  12. list.insert(4,0)
  13. print("加入一元素",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,"a","b","c","d"]
  2. print(type(a[4]))
  3. print(type(a[6]))
  4. print(len(a))
  5. a.append("e")
  6. print(a)
  7. a.insert(11,"f")
  8. print(a)
  9. a.pop(0)
  10. print(a)
  11. a.remove("f")
  12. print(a)
  13. b=[5,0,1,4,6,1]
  14. b.sort()
  15. print(b)
  16. b.reverse()
  17. print(b)
複製代碼

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=[11,-4,-25,50,1,15]

  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)

  14. list.clear()

  15. print(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

[code][/code]list=[12,-3,-50,32,25,11]
print("排序前:",list)
list.sort();
print("排序後(遞增):",list)
list.reverse();
print("排序後(遞減):",list)
print("元素數量:",len(list))
list.pop()
print("pop掉一元素",list)
list.append(-20)
print("append一元素",list)
list.insert(4,0)
print("insert一元素",list)# -*- coding: utf-8 -*-

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

TOP

返回列表