iT邦幫忙

2021 iThome 鐵人賽

DAY 4
0
Modern Web

基礎python,讓你學點東西系列 第 4

[Day4]-基本串列(list)

  • 串列基本定義
  1. 串列可以儲存不同的資料型態,如:整數、字串、浮點數,基本格式如下:
    List = [元素1,元素2…..]
    元素1 = List[0]
    元素2 = List[1]
    https://ithelp.ithome.com.tw/upload/images/20210916/20140644lLP2g3abbM.png
    https://ithelp.ithome.com.tw/upload/images/20210916/20140644McJAligG8z.png

  2. 串列切片
    List[a:b] – 讀取索引a到b-a個索引元素
    List[:a] – 讀取從索引0到索引a-1的每個元素
    List[:-a] – 讀取從頭的元素,但不含最後a個
    List[a:] – 讀取從索引a開始到最後的元素
    List[-a:] – 讀取後面a個元素
    List[:] – 讀取所有的元素
    List[a:b:c] – 每隔c,讀取索引a到b-a個索引元素
    https://ithelp.ithome.com.tw/upload/images/20210916/20140644uAqd6B0RTV.png
    https://ithelp.ithome.com.tw/upload/images/20210916/20140644JsjLnGveZy.png

  3. 更改、增加及刪除元素內容

  • 更改
    https://ithelp.ithome.com.tw/upload/images/20210916/201406448THh0DXxLc.png
    https://ithelp.ithome.com.tw/upload/images/20210916/20140644poY76swUoN.png

  • 增加
    List.append(‘新增元素’) 固定加在最後面
    https://ithelp.ithome.com.tw/upload/images/20210916/20140644XVAHCFz8jT.png
    https://ithelp.ithome.com.tw/upload/images/20210916/20140644XjE53u9810.png
    List.insert(n,元素內容) 加在n+1的位子
    https://ithelp.ithome.com.tw/upload/images/20210916/201406442A8s5gEftu.png
    https://ithelp.ithome.com.tw/upload/images/20210916/20140644wCSIuAXKk2.png

  • 刪除
    del list[n] 刪除第n+1的元素
    https://ithelp.ithome.com.tw/upload/images/20210916/20140644sYO4N6nuhh.png
    https://ithelp.ithome.com.tw/upload/images/20210916/20140644boY6ssFcoy.png
    Pop_list = list.pop() 刪除最後一個元素
    Pop_list = list.pop(n) 刪除第n+1個元素
    https://ithelp.ithome.com.tw/upload/images/20210916/20140644MJlkUp8rsr.png
    https://ithelp.ithome.com.tw/upload/images/20210916/20140644IqqZSnXHo5.png
    List.remove(刪除的元素內容)
    https://ithelp.ithome.com.tw/upload/images/20210916/20140644bNmY3lvqSn.png
    https://ithelp.ithome.com.tw/upload/images/20210916/20140644CemOJghl8u.png


上一篇
[Day3]-if敘述
下一篇
[Day5]-串列的相關用法
系列文
基礎python,讓你學點東西30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言