返回列表 發帖

串列 (二)

本帖最後由 鄭繼威 於 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=[1,2,3,4,5,6,7,8,9,10]
  2. list.reverse()
  3. print("反過來",list)
  4. list.pop()
  5. print("少一個",list)
  6. list.append(-20)
  7. print("-20",list)
複製代碼

TOP

本帖最後由 張育維 於 2023-7-11 09:59 編輯
  1. listA=["alcahol",17.1,67,[7,25],"allowence"]
  2. listB=[12,-51,42,45,-4,54,-84]
  3. listB.append(-54)
  4. print(listB)
  5. listB.insert(6,100)
  6. print(listB)

  7. listB.pop()
  8. listB.pop(2)
  9. print(listB)
  10. listB.remove(42)
  11. print(listB)

  12. listB.sort()
  13. print(listB)
  14. listB.sort(reverse=True)
  15. listA.reverse
  16. print(listA)
複製代碼

TOP

本帖最後由 歐陽霖 於 2023-7-11 10:10 編輯
  1. list2=[2,9,6,8,2.6]
  2. list2.append(20)
  3. print(f"list2 append(20){list2}")
  4. list2.insert(2,"apple")
  5. print(f"list2 insert(2,apple){list2}")
  6. list2.remove("apple")
  7. print(f"list2 remove(apple){list2}")
  8. list2.sort()#排序(小到大)
  9. print(list2)
  10. list2.sort(reverse=True)#排序(大到小)
  11. print(list2)
  12. list2.pop(2)
  13. print(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

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

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

--

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=[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=["忠","孝","仁","50","信","義","和"]
  2. list.append("平")
  3. list.insert(3,"愛")
  4. list.remove("50")
  5. list.reverse()
  6. print(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=["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. 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=[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

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(-9)
  11. print("append一元素:",list)
  12. list.insert(2,7)
  13. print("insert一元素:",list)
複製代碼

TOP

返回列表