標題:
串列 (二)
[打印本頁]
作者:
鄭繼威
時間:
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()等函式,完成如下操作:
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)
複製代碼
作者:
董炫霆
時間:
2023-7-11 09:56
list=[1,2,3,4,5,6,7,8,9,10]
list.reverse()
print("反過來",list)
list.pop()
print("少一個",list)
list.append(-20)
print("-20",list)
複製代碼
作者:
張育維
時間:
2023-7-11 09:57
本帖最後由 張育維 於 2023-7-11 09:59 編輯
listA=["alcahol",17.1,67,[7,25],"allowence"]
listB=[12,-51,42,45,-4,54,-84]
listB.append(-54)
print(listB)
listB.insert(6,100)
print(listB)
listB.pop()
listB.pop(2)
print(listB)
listB.remove(42)
print(listB)
listB.sort()
print(listB)
listB.sort(reverse=True)
listA.reverse
print(listA)
複製代碼
作者:
歐陽霖
時間:
2023-7-11 10:03
本帖最後由 歐陽霖 於 2023-7-11 10:10 編輯
list2=[2,9,6,8,2.6]
list2.append(20)
print(f"list2 append(20){list2}")
list2.insert(2,"apple")
print(f"list2 insert(2,apple){list2}")
list2.remove("apple")
print(f"list2 remove(apple){list2}")
list2.sort()#排序(小到大)
print(list2)
list2.sort(reverse=True)#排序(大到小)
print(list2)
list2.pop(2)
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
list1=[213,5467821,38,3,57,86,5,5375,44]
print(f"list1={list1}")
list1.reverse()
print(f"list1={list1}")
list1.sort()
print(f"list1={list1}")
list1.reverse()
print(f"list1={list1}")
list1.pop(4)
print(f"list1={list1}")
list1.append(5)
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
for i in range(1,6,1):
print(f"第{i}名")
for i in range(1,101,6):
print(i)
for i in range(5,10,1):
print(i)
複製代碼
作者:
鄭繼威
時間:
2023-8-3 09:36
--
作者:
得銓
時間:
2023-8-3 10:01
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)
複製代碼
作者:
矯博宇
時間:
2023-8-3 10:05
a=[12,-3,-76,32,25,76]
print("排序前:",a)
a.sort()
print("排序後(遞增):",a)
a.reverse()
print("排序後(遞減):",a)
print("元素數量:",len(a))
a.pop()
print("pop掉一元素",a)
a.append(25)
print("append一元素",a)
a.insert(3,0)
print("insert一元素",a)
複製代碼
作者:
高士涵
時間:
2023-8-3 10:07
list=["忠","孝","仁","50","信","義","和"]
list.append("平")
list.insert(3,"愛")
list.remove("50")
list.reverse()
print(list)
複製代碼
作者:
張子搴
時間:
2023-8-3 10:09
list=["忠","孝","仁","愛","信","義","和","平"]
list.append(50)
print(f"list append:{list}")
list.insert(5,6)
print(f"list insert:{list}")
list.pop(2)
print(f"list pop:{list}")
list.remove(1)
複製代碼
作者:
鄧維成
時間:
2023-8-3 10:09
list=["5","6","1","-9","-200","-5","8","954"]
print(f"before:{list}")
list.sort()
print(f"after:{list}")
list.pop()
print(f"{list}")
list.append(-2)
print(f"{list}")
list.insert(5,9)
print(f"{list}")
list.remove("-200")
print(f"{list}")
複製代碼
作者:
盧承新
時間:
2023-8-3 10:11
a=["忠","孝","仁","愛","信","義","和","平"]
print(type(a))
print(len(a))
a.append(2)
print(a)
a.insert(3,6)
print(a)
a.pop()
print(a)
a.remove(6)
print(a)
a.sort()
print(a)
a.sort(reverse=1)
print(a)
a.reverse()
print(a)
複製代碼
作者:
呂宗晉
時間:
2023-8-3 10:11
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)
複製代碼
作者:
鄭繼威
時間:
2023-8-3 10:13
7
作者:
盧芷萱
時間:
2023-8-3 10:16
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(-9)
print("append一元素:",list)
list.insert(2,7)
print("insert一元素:",list)
複製代碼
作者:
李逢翔
時間:
2023-8-3 10:16
list=["忠","孝","仁","愛","信","義","和","平"]
print(list[1:6])
list.append(369)
print(f"list.append:{list}")
list.insert(3,"ABC")
print(f"list.insert:{list}")
list.pop(1)
print(f"list.pop:{list}")
list.remove("愛")
print(f"list.remove:{list}")
list.reverse()
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
list=["d","e","m","i","s","e"]
print("normal:",list[0:7])
list.reverse()
print("reverse",list[0:7])
list.reverse()
list.pop()
print("pop",list[0:6])
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/)
Powered by Discuz! 7.2