返回列表 發帖

串列 (二)

本帖最後由 鄭繼威 於 2024-7-9 09:35 編輯

串列相關操作

• 顯示串列資料型態
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)
複製代碼

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

TOP

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

TOP

ist=[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)

TOP

ist=[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)

TOP

ist=[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)

TOP

排序前: [12, -3, -50, 32, 25, 11]
排序後(遞增): [-50, -3, 11, 12, 25, 32]
排序後(遞減): [32, 25, 12, 11, -3, -50]
元素數量: 6
pop掉一元素 [32, 25, 12, 11, -3]
append一元素 [32, 25, 12, 11, -3, -20]
insert一元素 [32, 25, 12, 11, 0, -3, -20]

TOP

# list2 = [12,-3,-50,32,25,11]
# print(list2)
# list2.sort()
# print(list2)
# list2.sort(reverse = True)
# print(list2)
# print(len(list2))
# a = list2.pop()
# print(a)
# print(list2)
# list2.append(1000000)
# print(100000)
# print(list2)
# list2.insert(0,10000000)
# print(10000000)
# print(list2)

TOP

排序前: [12, -3, -50, 32, 25, 11]
排序後(遞增): [-50, -3, 11, 12, 25, 32]
排序後(遞減): [32, 25, 12, 11, -3, -50]
元素數量: 6
pop掉一元素 [32, 25, 12, 11, -3]
append一元素 [32, 25, 12, 11, -3, -20]
insert一元素 [32, 25, 12, 11, 0, -3, -20]

TOP

本帖最後由 黃煜翔 於 2025-1-24 10:18 編輯

_list = [12,-3,-50,32,25,11]
print(_list)
_list.sort()           #123
print("排序後(遞增):"+str(_list))
_list.reverse()      #321
print("排序後(遞減):"+str(_list))
print("元素數量:"+str(len(_list)))
_list.pop()            #尾端刪除
print("pop掉一元素:"+str(_list))
_list.append(-20)  #尾端新增
print("append一元素:"+str(_list))
_list.insert(4,0)     #中間插入
print("insert一元素:"+str(_list))

TOP

list2 = [12,-3,-50,32,25,11]
print(list2)
list2.sort()
print(list2)
list2.sort(reverse = True)
print(list2)
print(len(list2))
a = list2.pop()
print(a)
print(list2)
list2.append(1000000)
print(100000)
print(list2)
list2.insert(0,10000000)
print(10000000)
print(list2)

TOP

l排序前: [12, -3, -50, 32, 25, 11]
排序後(遞增): [-50, -3, 11, 12, 25, 32]
排序後(遞減): [32, 25, 12, 11, -3, -50]
元素數量: 6
pop掉一元素 [32, 25, 12, 11, -3]
append一元素 [32, 25, 12, 11, -3, -20]
insert一元素 [32, 25, 12, 11, 0, -3, -20]

TOP

  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Fri Jan 24 10:12:20 2025

  4. @author: student
  5. """
  6. list=[12,-3,-50,32,25,11,99,99]
  7. print(f"原本的list={list}")
  8. list.append(56)
  9. print(f"append後的list={list}")
  10. list.insert(2,999)
  11. print(f"insert後的list={list}")
  12. list.pop(0)
  13. print(f"pop後的list={list}")
  14. list.remove(99)
  15. print(f"remove後的list={list}")
  16. print(f"原本的list={list}")
  17. list.sort(reverse=1)
  18. print(f"sort後的list={list}")
  19. list=[12,-3,-50,32,25,11,99,99]
  20. print(f"原本的list={list}")
  21. list.reverse()
  22. print(f"reverse後的list={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

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)

TOP

回復 1# 鄭繼威


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

TOP

回復 1# 鄭繼威


   # a=[33,45,51,"hello",True,33]
# print(len(a))
# a.insert(1,36)
# a.append(42)
# print(a)
# temp=a.pop(1)
# print(temp)
# find=a.index(33,2)
# a.pop(find)
# print(a)
a=[1,2,3,4,8,4,9,2]

a.sort()
a.reverse()
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

回復 1# 鄭繼威


    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)

TOP

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)

TOP

返回列表