Board logo

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

作者: 鄭繼威    時間: 2023-7-10 15:59     標題: 串列 (二)

本帖最後由 鄭繼威 於 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)
複製代碼

作者: 董炫霆    時間: 2023-7-11 09:56

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

作者: 張育維    時間: 2023-7-11 09:57

本帖最後由 張育維 於 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)
複製代碼

作者: 歐陽霖    時間: 2023-7-11 10:03

本帖最後由 歐陽霖 於 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)
複製代碼

作者: 黃柏翰    時間: 2023-7-11 10:04

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}")
作者: 曾冠瑋    時間: 2023-7-11 10:05

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}")
作者: 董恩瑞    時間: 2023-7-11 10:06

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}")
作者: 方昱安    時間: 2023-7-11 10:09

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

作者: 劉恆    時間: 2023-7-11 10:10

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}")
作者: 歐陽霖    時間: 2023-7-11 10:31

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

作者: 鄭繼威    時間: 2023-8-3 09:36

--
作者: 得銓    時間: 2023-8-3 10:01

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

作者: 矯博宇    時間: 2023-8-3 10:05

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

作者: 高士涵    時間: 2023-8-3 10:07

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

作者: 張子搴    時間: 2023-8-3 10:09

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

作者: 鄧維成    時間: 2023-8-3 10:09

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

作者: 盧承新    時間: 2023-8-3 10:11

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

作者: 呂宗晉    時間: 2023-8-3 10:11

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

作者: 鄭繼威    時間: 2023-8-3 10:13

7
作者: 盧芷萱    時間: 2023-8-3 10:16

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

作者: 李逢翔    時間: 2023-8-3 10:16

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

作者: 陳珺    時間: 2023-8-3 10:35

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)
作者: 張絜晰    時間: 2023-8-3 10:39

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





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