Board logo

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

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

• 串列,又稱為「清單」或「列表」,與其他語言的
「陣列(Array)」相同,其功能與變數相類似。
  索引值是從0 開始計數,索引值不可超出串列的範圍
• 宣告
串列名稱(變數名稱)= [元素1, 元素2, ......]
• 範例
list1 = [1, 2, 3, 4, 5]
#元素皆為整數
list2 = ["香蕉", "蘋果", "橘子"]
#元素皆為字串
list3 = [1, "香蕉", True]
#包含不同資料型態元素

• 取得串列元素
變數名稱= 串列名稱[索引值]


假設有一串列其內容如下:
list=["忠","孝","仁","愛","信","義","和","平"]

試取出串列中的第1到第5個元素

  1. list=["忠","孝","仁","愛","信","義","和","平"]
  2. print(list[0])
  3. print(list[1:6]) #左邊是開始(有包含),右邊是結束(沒有包含)。
  4. print(list1[-1]) #取出最後一個元素值
複製代碼

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

  1. a=[1,2,3,4,5,6,7,8,9,10]
  2. print(a)
  3. print(a[5])
  4. print(a[0:5])
  5. print(a[-1])
複製代碼

作者: 董恩瑞    時間: 2023-7-11 09:28

list1=[2,6,2,"apple",2.6,[5,6]]
print(list1[0])
print(list1[5])
print(list1[0:3])
print(list1[1:4])
print(list1[-1])
作者: 曾冠瑋    時間: 2023-7-11 09:28

list1=[1,2,3,4,"banana",5,6,]
print(list1[0])
print(list1[2:6])
print(list1[-1:4])
print(len(list1)-1)
作者: 張育維    時間: 2023-7-11 09:30

  1. listA=["alcahol",17.1,67,[7,25],"allowence"]
  2. print(listA)
  3. print(listA[3])
  4. print(listA[-4])
  5. print(listA[2:4])
複製代碼

作者: 歐陽霖    時間: 2023-7-11 09:32

  1. list1=[5,109,25,True,"alert",2.9,[18,6,90]]
  2. print(list1)
  3. print(list1[2])
  4. print(list1[0:5])
  5. print(list1[0-len(list1)])
  6. print(list1[len(list1)-1])(list1[-1])
複製代碼

作者: 劉恆    時間: 2023-7-11 09:35

list1=[1,2,3,4,5,6,7,8,9,10]
print(list1[4])
print(list[-2])
print(list1[1:6])
作者: 黃柏翰    時間: 2023-7-11 09:35

list1=[1,2,3,4,5,6,7,8,9]
print(list1[4])
print(list1[1:6])
print(list1[-2])
作者: 方昱安    時間: 2023-7-11 09:36

  1. list1=[213,5467821,38,3,57,86,5,5375,44]
  2. print(list1[0])
  3. print(list1[0:8])
  4. print(list1[-6:-6])
  5. print(list1[-6:-4])
  6. print(str(list1[-6:-1])+str(list1[0:5]))
  7. print(list1[len(list1)-1])
複製代碼

作者: 得銓    時間: 2023-8-3 09:20

  1. list=["忠","孝","仁","愛","信","義","和","平"]
  2. print(list[1:6])
複製代碼

作者: 盧芷萱    時間: 2023-8-3 09:20

  1. list=["忠","孝","仁","愛","信","義","和","平"]

  2. print(list[1:6])
複製代碼

作者: 盧承新    時間: 2023-8-3 09:22

  1. a=["忠","孝","仁","愛","信","義","和","平"]
  2. print(a[0])
  3. print(a[1:6])
  4. print(a[-1])
複製代碼

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

本帖最後由 鄧維成 於 2023-8-3 09:25 編輯

list=["忠","孝","仁","愛","信","義","和","平"]
print(list[1:6])
print(list[-1])
print(list[4])
作者: 矯博宇    時間: 2023-8-3 09:22

  1. a=[1,2,3,4,5,6,7,8]
  2. print(a)
  3. print(a[0])
  4. print(a[3:5])
  5. print(a[-3])
複製代碼

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

  1. list=["忠","孝","仁","愛","信","義","和","平"]
  2. print(list[0:6])
  3. print(list[-3])
  4. print(list[-3:-5)]
  5. print(list[1:5)]
複製代碼

作者: 高士涵    時間: 2023-8-3 09:23

  1. list=["忠","孝","仁","愛","信","義","和","平"]
  2. print(list[1:6])
  3. print(list[-1])
  4. print(list[-8:-7])
  5. print(list[3:6])
  6. print(list[-4:-3])
複製代碼

作者: 得銓    時間: 2023-8-3 09:25

  1. list=["忠","孝","仁","愛","信","義","和","平"]
  2. print(list[0])
  3. print(list[1:6])
  4. print(list[-1])
複製代碼

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

7
作者: 張絜晰    時間: 2023-8-3 09:26

  1. list=["y","o","u","r"," ","m","o","m"]
  2. print (list[0])
  3. print(list[1:7])
  4. print(list[-1])
複製代碼

作者: 李逢翔    時間: 2023-8-3 09:26

  1. list=["忠","孝","仁","愛","信","義","和","平"]
  2. print(list[1:6])
複製代碼

作者: 呂宗晉    時間: 2023-8-3 09:29

  1. list=["忠","孝","仁","愛","信","義","和","平"]
  2. print(list[0])
  3. print(list[1:6])
  4. print(list1[-1])
複製代碼

作者: 陳珺    時間: 2023-8-3 09:43

list=["忠","孝","仁","愛","信","義","和","平"]
print(list[1:6])
print(list)
print(list[0])
作者: 李逢翔    時間: 2023-8-3 09:59

  1. list=["忠","孝","仁","愛","信","義","和","平"]
  2. print(list[1:6])
複製代碼





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