返回列表 發帖

串列 (二)

本帖最後由 鄭繼威 於 2023-7-11 10:08 編輯

串列相關操作

• 顯示串列資料型態
type()
• 顯示串列元素數量
len()

• 串列元素新增
append()->新增在最後
insert(idx,value)->指定位置新增

• 串列元素刪除
pop()->刪除最後一個(也可以指定(idx)移除)
remove(value)->指定刪除(指定(value)移除)

• 串列元素排序
sort()->預設reverse=0(不反向)
(reverse=1->降冪(由大到小))

• 串列元素反轉
reverse()
ex:list1=[12, -3, -50, 32, 25, 11]
list1.reverse()
[11, 25, 32, -50, -3, 12]



假設有一串列其內容如下:
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=["d","e","m","i","s","e"]
  2. print("normal:",list[0:7])
  3. list.reverse()
  4. print("reverse",list[0:7])
  5. list.reverse()
  6. list.pop()
  7. print("pop",list[0:6])
複製代碼
Attention Seeker </3

TOP

list=[12,-3,-50,32,25,11]
list.sort()
print(list)
list.reverse()
print(list)
list.pop(5)
print(list)
list.append(-20)
print(list)
list.insert(5,0)
print(list)

TOP

  1. list=["忠","孝","仁","愛","信","義","和","平"]
  2. print(list[1:6])
  3. list.append(369)
  4. print(f"list.append:{list}")
  5. list.insert(3,"ABC")
  6. print(f"list.insert:{list}")
  7. list.pop(1)
  8. print(f"list.pop:{list}")
  9. list.remove("愛")
  10. print(f"list.remove:{list}")
  11. list.reverse()
  12. print(f"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(-9)
  11. print("append一元素:",list)
  12. list.insert(2,7)
  13. print("insert一元素:",list)
複製代碼

TOP

7

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=["忠","孝","仁","愛","信","義","和","平"]
  2. print(type(a))
  3. print(len(a))
  4. a.append(2)
  5. print(a)
  6. a.insert(3,6)
  7. print(a)
  8. a.pop()
  9. print(a)
  10. a.remove(6)
  11. print(a)
  12. a.sort()
  13. print(a)
  14. a.sort(reverse=1)
  15. print(a)
  16. a.reverse()
  17. print(a)
複製代碼

TOP

  1. list=["5","6","1","-9","-200","-5","8","954"]
  2. print(f"before:{list}")
  3. list.sort()
  4. print(f"after:{list}")
  5. list.pop()
  6. print(f"{list}")
  7. list.append(-2)
  8. print(f"{list}")
  9. list.insert(5,9)
  10. print(f"{list}")
  11. list.remove("-200")
  12. print(f"{list}")
複製代碼

TOP

  1. list=["忠","孝","仁","愛","信","義","和","平"]
  2. list.append(50)
  3. print(f"list append:{list}")
  4. list.insert(5,6)
  5. print(f"list insert:{list}")

  6. list.pop(2)
  7. print(f"list pop:{list}")

  8. list.remove(1)
複製代碼

TOP

  1. list=["忠","孝","仁","50","信","義","和"]
  2. list.append("平")
  3. list.insert(3,"愛")
  4. list.remove("50")
  5. list.reverse()
  6. print(list)
複製代碼

TOP

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

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

--

TOP

  1. for i in range(1,6,1):
  2.     print(f"第{i}名")
  3. for i in range(1,101,6):
  4.     print(i)
  5. for i in range(5,10,1):
  6.     print(i)
複製代碼

TOP

list1=[1,6,3,"apple",2.6]
print(f"list1:{list1}")
list1.append("banana")
print(f"list append:{list1}")
list1.insert(2,111)
print(f"list append:{list1}")


list1.pop()
print(f"list pop(111):{list1}")
list1.remove(111)
print(f"list remove(111):{list1}")


list2=[12,-5,60,45,99,3]
print(f"list2:{list2}")
list2.sort(reverse=True)
print(f"list2 sort:{list2}")

list1.reverse()
print(f"list1 reverse:{list1}")

TOP

  1. list1=[213,5467821,38,3,57,86,5,5375,44]
  2. print(f"list1={list1}")
  3. list1.reverse()
  4. print(f"list1={list1}")
  5. list1.sort()
  6. print(f"list1={list1}")
  7. list1.reverse()
  8. print(f"list1={list1}")
  9. list1.pop(4)
  10. print(f"list1={list1}")
  11. list1.append(5)
  12. print(f"list1={list1}")
複製代碼

TOP

list1=[1,6,3,"apple",2.6]
print(f"list1:{list1}")
list1.append("banana")
print(f"list append:{list1}")
list1.insert(2,111)
print(f"list append:{list1}")


list1.pop()
print(f"list pop:{list1}")
list1.remove(111)
print(f"list remove(111):{list1}")


list2=[12,-5,60,45,99,3]
print(f"list2:{list2}")
list2.sort(reverse=True)
print(f"list2 sort:{list2}")

list1.reverse()
print(f"list1 reverse:{list1}")

TOP

list1=[15,-4,3,9,-7,]
print(f"list1:{list1}")
list1.append(20)
print(f"list:{list1}")
list1.insert(2,11)
print(f"list1:{list1}")
list1.pop(4)
print(f"list1 pop:{list1}")
list2=[12,-3,25,38,-9,41,]
print(f"list2:{list2}")
list2.sort
print(f"list2 sort:{list2}")

TOP

list1=[1,6,3,"apple",2.6]
print(f"list1:{list1}")
list1.append("banana")
print(f"list append:{list1}")
list1.insert(2,111)
print(f"list append:{list1}")


list1.pop()
print(f"list pop:{list1}")
list1.remove(111)
print(f"list remove(111):{list1}")


list2=[12,-5,60,45,99,3]
print(f"list2:{list2}")
list2.sort(reverse=True)
print(f"list2 sort:{list2}")

list1.reverse()
print(f"list1 reverse:{list1}")

TOP

返回列表