標題:
串列 (二)
[打印本頁]
作者:
鄭繼威
時間:
2022-8-18 22:22
標題:
串列 (二)
串列相關操作
• 顯示串列資料型態
type()
• 顯示串列元素數量
len()
• 串列元素新增
append()->新增在最後
insert(n,value)->指定位置新增
• 串列元素刪除
pop()->刪除最後一個(也可以指定(idx)移除)
remove(value)->指定刪除(指定(value)移除)
• 串列元素排序
sort()->預設reverse=0(不反向)
(reverse=1->降冪(由大到小))
假設有一串列其內容如下:
list=[12,-3,-50,32,25,11]
試以sort()、reverse()、len()、pop()、append()、insert()等函式,完成如下操作:
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)
複製代碼
作者:
鄭繼威
時間:
2022-8-19 13:15
本帖最後由 鄭繼威 於 2022-8-19 13:18 編輯
list=["信","義","和","平"]
print("原始list",list)
print("--新增--")
list.append("apple")
print("append:",list)
list.insert(1,"banana")
print("insert:",list)
print("--刪除--")
list.pop()
print("pop:",list)
list.remove("banana")
print("remove",list)
list2=[1,2,3,100,99,50,0,12,-3,-50,32,25,11]
print("--排序--")
list2.sort()
print("sort由小到大:",list2)
list2.sort(reverse=True)
print("sort由大到小:",list2)
print("--反向--")
list2.reverse()
print("list2 reverse:",list2)
list.reverse()
print("list reverse:",list)
print("--長度--")
print("list的長度",len(list))
print("list2的長度",len(list2))
複製代碼
作者:
陳牧謙
時間:
2022-8-19 13:31
本帖最後由 陳牧謙 於 2022-8-19 13:42 編輯
list=["忠","孝","仁","愛",]
print("原始list",list)
print("--新增--")
list.append("apple")
print("append:",list)
list.insert(1,"banana")
print("insert:",list)
print("--刪除--")
list.pop()
print("pop:",list)
list.remove("banana")
print("remove:",list)
list2=[1,2,3,100,99,50,0,12,-3,-50,32,25,11]
print("--排序--")
list2.sort()
print("sort由小到大:",list2)
list2.sort(reverse=True)
print("sort由大到小:",list2)
print("--反向--")
list2.reverse()
print("list2 reverse:",list2)
list.reverse()
print("list reverse:",list)
print("--長度--")
print("list的長度",len(list))
print("list2的長度",len(list2))
複製代碼
作者:
吳俊頡
時間:
2022-8-19 13:34
list=["信","義","和","平"]
print("原始list",list)
list.append("apple")
print("append:",list)
list.insert(1,"banana")
print("insert:",list)
list.pop()
print("pop:",list)
list.remove("banana")
print("remove",list)
list2=[1,3,5,7,9,11,13,15,17]
list2.sort()
print("sort由小到大:",list2)
list2.sort(reverse=True)
print("sort由大到小:",list2)
list2.reverse()
print("list2 reverse:",list2)
list.reverse()
print("list reverse:",list)
複製代碼
作者:
彭郁程
時間:
2022-8-19 13:36
"
list=["信","義","和","平"]
print("原list",list)
list.append("watermelon")
print("append:",list)
list.insert(1,"fruit")
print("insert:"),list
list.pop()
print("pop:",list)
list.remove("fruit")
print("remove",list)
list2=[1,2,3,100,99,50,0,12,-3,-50,32,25,11]
list2.sort(reverse=True)
print("sort由大到小:",list2)
list2.sort()
print("sort由小到大:",list2)
list2.reverse()
print("reverse:",list2)
list.reverse()
print("list reverse:",list)
複製代碼
作者:
胡凰月
時間:
2022-8-19 13:44
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)
複製代碼
作者:
簡宗漢
時間:
2022-8-19 13:46
a=[1,2,3,4,5,6,7,8,300,284,]
print(a[0:20])
print(a[1]+a[4]-a[8]+a[1])
print(len(a))
a.pop(2)
print(a)
a.remove(1)
print(a)
a.sort()
print(a)
a.sort(reverse=True)
print(a)
a.reverse()
print(a)
a.append("apple")
print(a)
a.insert(1,"banana")
print(a)
複製代碼
作者:
姜宏叡
時間:
2022-8-19 13:47
99,100,101,102-25,53,852]
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)
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2