Allen Chou
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    --- type: slide tags: 資芽簡報 --- # List 2023 資訊之芽北區 Py 班 感謝去年講師濬哲讓我~~抄襲~~修改他的簡報 --- ## 來記一些資訊好了 ---- ```python= student1 = 100 ``` <span>沒什麼問題,那就加一個<!-- .element: class="fragment" data-fragment-index="1" --></span> ---- ```python= student1 = 100 student2 = 87 ``` <span>輕鬆簡單,那就再加幾個<!-- .element: class="fragment" data-fragment-index="1" --></span> ---- ```python= student1 = 100 student2 = 87 student3 = 77 student4 = 69 ``` <span>如果有 10 個? 20 個? 100 個?<!-- .element: class="fragment" data-fragment-index="1" --></span> --- ## List ---- - 可迭代(iterable) - 迭:輪流、更替,如:「更迭」。 - 《詩經.邶風.柏風》:「日居月諸,胡迭而微。」 - 可以重複 - 換言之,有不能重複的資料結構 e.g. set - 有序 - 換言之,有無序的資料結構 e.g. set <span>蛤?什麼是資料結構?<!-- .element: class="fragment" data-fragment-index="1" --></span> ---- 用法 ```python= list1 = [] # 一個空的 list list2 = list() # 另一個空的 list list = [] # 不要這樣!為什麼? ``` ---- 基本上要塞什麼都可以 ```python= list1 = [905, "NGGYU", False, [1987, 7, 27], {3.14}, 905] list2 = [list1] # 這會是什麼? ``` ---- Length ```python= list1 = [] print(len(list1)) #0 list1 = [1, 2, 3] print(len(list1)) #3 ``` ---- 猜猜看這會印出什麼 ```python= list1 = [1, 2, 3, [1, 2, 3], []] print(len(list1)) ``` ---- list 裡面的東西要怎麼讀出來? ```python= list1 = ["a", "b", "c", "d"] print(list1[0]) # a print(list1[3]) # d print(list1[-1]) # d ``` ---- (補充)等等,為什麼第一項是 `0`? `[n]` 裡面的數字是「位移量」,不是「第幾個」 `[0]` 是指「從 list 開頭位移 0 個位置」,所以是第一個值 ---- 也可以一次取好幾個 `list[start:end:step]` 首項(含)、尾項(不含)、公差(預設為 1) ```python= list1 = ["a", "b", "c", "d", "e", "f", "g"] print(list1[1:3]) # ["b", "c"] print(list1[0:-1]) # ["a", "b", "c", "d", "e", "f"] print(list1[0:5:2]) # ["a", "c", "e"] ``` 這稱為 Slicing ---- :chestnut::`grades` 裡面有全班的成績,請把前三個人的成績印出來,也把第偶數個人的成績印出來 ```python= grades = [100, 87, 78, 99, 47, 59, 39.49] ``` ---- :rice: 解 ```python= grades = [100, 87, 78, 99, 47, 59, 39.49] print(grades[:3]) print(grades[1::2]) ``` ---- 如何修改特定數值 ```python= list1 = ["a", "b", "c", "d", "e"] list1[1] = "p" # ["a", "p", "c", "d", "e"] list1[2:4] = ["p", "l"] # ["a", "p", "p", "l", "e"] ``` ---- list 的加法與乘法 ```python= list1 = [1, 2, 3] print(list1 + [4]) # [1, 2, 3, 4] print(list1 * 3) # [1, 2, 3, 1, 2, 3, 1, 2, 3] print(list1) # list1 沒有被改,所以是 [1, 2, 3] ``` ```python= list2 = [1, 2, 3] list2 += [4] print(list2) # [1, 2, 3, 4] list2 *= 3 print(list2) # [1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4] ``` ---- 如何知道一個值有沒有在 list 中出現? ```python= list1 = [1, 2, 3] print(1 in list1) # True print(10 in list1) # False ``` --- ## list 的 method 先不要管 method 是什麼,總之就是黑魔法 ---- `append`:加一項在最後面 ```python= list1 = [1, 2, 3] list1.append(10) # [1, 2, 3, 10] ``` ---- `extend`:加一群在最後面 ```python= list1 = [1, 2, 3] list1.extend([4, 5, 6]) # [1, 2, 3, 4, 5, 6] ``` ---- 四者差在哪裡? ```python= list1 = [1, 2, 3] list1p = list1 + [4, 5, 6] ``` ```python= list2 = [1, 2, 3] list2 += [4, 5, 6] ``` ```python= list3 = [1, 2, 3] list3.append([4, 5, 6]) ``` ```python= list4 = [1, 2, 3] list4.extend([4, 5, 6]) ``` ---- 四者差在哪裡? ```python= list1 = [1, 2, 3] list1p = list1 + [4, 5, 6] # list1: [1, 2, 3] # list1p: [1, 2, 3, 4, 5, 6] ``` ```python= list2 = [1, 2, 3] list2 += [4, 5, 6] # [1, 2, 3, 4, 5, 6] ``` ```python= list3 = [1, 2, 3] list3.append([4, 5, 6]) # [1, 2, 3, [4, 5, 6]] ``` ```python= list4 = [1, 2, 3] list4.extend([4, 5, 6]) # [1, 2, 3, 4, 5, 6] ``` ---- `insert(index, object)`:在指定位置插入一項 ```python= list1 = [1, 2, 3, 4] list1.insert(3, 7) # [1, 2, 3, 7, 4] list1.insert(100, 9) # [1, 2, 3, 7, 4, 9] ``` ---- `pop()`:回傳最後一項並將其刪掉 ```python= list1 = [1, 2, 3, 4] print(list1.pop()) # 4 list2 = [] print(list2.pop()) # IndexError ``` ![](https://i.imgur.com/23E1RNC.png) ([source](https://dictionary.cambridge.org/zht/%E8%A9%9E%E5%85%B8/%E8%8B%B1%E8%AA%9E-%E6%BC%A2%E8%AA%9E-%E7%B9%81%E9%AB%94/pop)) ---- `pop(index)`:回傳第 index 個值並將其刪掉 ```python= list1 = ['a', 'b', 'c', 'd'] print(list1.pop(2)) # 'c' print(list1.pop(10)) # IndexError ``` ---- `remove(obj)`:刪掉第一項值為 `obj` 的項 ```python= list1 = ['a', 'b', 'c', 'd', 'a', 'a'] list1.remove('a') # ['b', 'c', 'd', 'a', 'a'] list1.remove('z') # ValueError ``` ---- `clear()`:全部清空 ```python= list1 = [1, 2, 3] list1.clear() # [] ``` ---- `count(obj)`:值為 `obj` 的項出現幾次 ```python= list1 = [1, 1, 2, 3, 5, 8] print(list1.count(1)) # 2 print(list1.count(47)) # 0 ``` ---- `index(obj)` `index(obj, start)` `index(obj, start, end)` 回傳從 `start` 到 `end` 出現的第一個 `obj` 的 index ```python= list1 = [1, 2, 3, 1, 1, 2, 'no'] print(list1.index(1)) # 0 print(list1.index(1, 2)) # 3 print(list1.index(1, 4, 6)) # 4 print(list1.index("yes")) # ValueError ``` ---- `sort()`, `reverse()` 顧名思義就是排序跟反轉 ```python= list1 = [1, 9, 2, 8, 3, 7, 4, 6, 5] list1.sort() # [1, 2, 3, 4, 5, 6, 7, 8, 9] list1.reverse() # [9, 8, 7, 6, 5, 4, 3, 2, 1] list2 = list1[::-1] # 另一種 reverse 的方法,為什麼? ``` ---- 如何複製 list ```python= a = [1, 2, 3, 4] b = a a[0] = 5 print(a) print(b) ``` ---- 如何複製 list:`b = a.copy()`, `c = a[:]` ```python= list1 = [1, 2, 3, 4] list2 = list1 list3 = list1.copy() list4 = list1[:] list1[0] = 5 # [5, 2, 3, 4] print(list2) # [5, 2, 3, 4] print(list3) # [1, 2, 3, 4] print(list4) # [1, 2, 3, 4] ``` --- ## list 有關的 function 先不要管 function 是什麼,總之就是黑魔法 ---- `list(iterable)`:把任何 iterable (例如 `range`)轉成 list ```python= print(list(range(5))) # [0, 1, 2, 3, 4] ``` ---- `max(list1)`, `min(list1)`, `sum(list1)` ```python= list1 = [1, 2, 3] print(max(list1)) # 3 print(min(list1)) # 1 print(sum(list1)) # 6 ``` 如果不使用內建 function,你會怎麼寫這些功能? ---- `sorted(l)`:回傳一個排好的 list 和 `l.sort()` 差在哪? ```python= list1 = [1, 9, 2, 8, 3, 7, 4, 6, 5] list2 = sorted(list1) print(list1) # [1, 9, 2, 8, 3, 7, 4, 6, 5] print(list2) # [1, 2, 3, 4, 5, 6, 7, 8, 9] list3 = list1.sort() print(list1) # [1, 2, 3, 4, 5, 6, 7, 8, 9] print(list3) # None ``` <!-- ---- `map(func, iterable)`:對於每一項 `l[i]`,算出 `func(l[i])` ```python= char_list = ['1', '2', '3', '4'] print(map(int, char_list)) # <map object at ......> print(list(map(int, char_list))) # [1, 2, 3, 4] # [int('1'), int('2'), int('3'), int('4')] = [1, 2, 3, 4] ``` --> --- ## for 迴圈 ---- ```python= list1 = [1, 2, 3, 4, 6, 7, 8, 9] for i in list1: print(i) ``` ---- ```python= list1 = [1, 2, 3, 4, 6, 7, 8, 9] for i in range(len(list1)): print(i, ls[i]) ``` ---- ```python= list1 = [1, 2, 3, 4, 6, 7, 8, 9] for i, v in enumerate(list1): print(i, v) ``` ---- :chestnut::`grades` 裡面有全班的成績,請把 `grades` 裡面所有人的成績改成平方後除以 100 ```python= grades = [100, 87, 78, 99, 47, 59, 39.49] ``` ---- :rice: 解 ```python= for i, j in enumerate(grades): grades[i] = j**2 / 100 ``` ```python= for i in range(len(grades)): grades[i] = grades[i]**2 / 100 ``` ---- 常見錯誤 ```python= for i in grades: i = i**2/100 ``` ---- 補充:關於變數命名 - 當使用 list 裡面的內容命名時,記得要複數 - 存放成績的 list 應該名為 `grades` 而非 `grade` - 或是直接在變數名稱上註明這是一個 list - `grade_list` 當程式碼越來越長時,良好的命名習慣可以避免看不懂自己在寫什麼 --- ## 練習題 [Sprout OJ 327](https://neoj.sprout.tw/problem/327/) [Sprout OJ 3032](https://neoj.sprout.tw/problem/3032/) --- 補充:List Comprehension ---- ```python= [x for x in range(10)] # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] [x ** 2 for x in range(10)] # [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] ``` --- 總結一下,所以何時該用 list? - 有一些資料要儲存 - 不一定知道資料有多少個 - 資料可能增加、刪除、修改 - 這些資料可能有順序之分 - `[1, 2] != [2, 1]` --- ## 作業 [Sprout OJ 2005](https://neoj.sprout.tw/problem/2005/)

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully