chia you chen
    • 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 New
    • 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 Note Insights Versions and GitHub Sync 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
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    --- title: List Comprehension tags: sprouts slideOptions: theme: solarized transition: 'fade' --- ## List Comprehension! Be pythonic --- # Pythonic? ---- # Very python ---- ## Why? - 簡潔 - 可讀 - 入境隨俗 ---- ## Example 翻轉一個list ---- ## C style ```python= A = [1,2,3,4,5] B = [] for i in range(len(A)-1, 0 ,-1): B.append(A[i]) ``` ---- ## Python style ```python= A = [1,2,3,4,5] B = A[::-1] ``` ---- ## Advance 將一到一百所有的偶數輸出 ---- ## C style ```python= for i in range(1,101): if i % 2 == 0: print(i) ``` ---- ## Python style ```python= # 利用list的語法特性 print(list(range(1,101))[1::2]) ``` --- ## List comprehension? ---- ## imagine a scenario ---- ## Question 輸出0~100之間,所有被2整除但不被5整除的數。 ---- ## Intuitive way ```python= for i in range(101): if i % 2 == 0 and i % 5 != 0: print(i) ``` ---- ## Pythonic way ```python= print([i for i in range(101) if i % 2 == 0 and i % 5 != 0]) ``` --- ## 正直善良的寫法 ```python= for i in range(101): if i % 2 == 0 and i % 5 != 0: print(i) ``` ## 黑魔法 ```python= print([i for i in range(101) if i % 2 == 0 and i % 5 != 0]) ``` ---- ## Definition ![](https://i.imgur.com/wiiEiFc.png) ---- ## List Comprehension 從入門到放棄,四步驟 ---- ## 心法 - 有list有\[\] - 想要的在前面 - 修飾的在後面 - 源頭在中間 ---- ## 黑魔法解析 ```python= [i for i in range(101) if i % 2 == 0 and i % 5 != 0] ``` - 有list有\[\] - i 是我想要的 - `if i % 2 == 0 and i % 5 != 0` 是修飾用的 - `for i in range(101)` 是源頭 ---- ### 當成篩子 ![](https://i.imgur.com/MueqI2S.png =300x400) --- ## 黑魔法練習s - 1 : 1-50所有的數 - 2 : 1-50所有5的倍數 - 3 : 1-100所有3的倍數但不是9的倍數 - 4 : 1-100所有2的次方數 ---- ## 黑魔法練習-1 - 1-50所有的數 ```python= [i for i in range(1,51)] ``` ---- ## 黑魔法練習-2 - 1-50所有5的倍數 ```python= [i for i in range(1,51) if i % 5 == 0] ``` ---- ## 黑魔法練習-3 - 1-100所有3的倍數但不是9的倍數 ```python= [i for i in range(1,101) if i % 3 == 0 and i % 9 != 0] ``` ---- ## 黑魔法練習-4 - 1-100所有2的次方數 ```python= [2**i for i in range(10) if 2**i < 101 and 2**i > 0] ``` --- ## 實用黑魔法 ---- ### 輸入處理-DNA雜訊處理 DNA有四種含氮鹼基,分別為ATGC,但在實際上,資料往往會出現在雜訊,因此必須過濾掉不可能出現的鹼基 - Input : ATCGATGCHATGCATFC ---- ## Intuitive Style ```python= k = input() new_k = "" for i in k: if i in "ATGC": new_k+=i ``` ---- ## 黑魔法Style ```python= "".join([i for i in input() if i in "ATGC"]) ``` - for i in input() - 以i遍歷input()回傳字串的每個字元 - if i in "ATGC" - 只有在"ATGC"裡的i才會通過 - i - Return i --- ## 進階黑魔法 如果要把錯誤用x取代呢? ---- ## Intuitie Style ```python= k = input() new_k = "" for i in k: if i in "ATGC": new_k+=i ``` ---- ## 黑魔法Style else 怎麼辦??? ---- ## Conditional Expression - `a if [condition] else b` - 以下簡稱CE ---- ## Example ```python= x = 3 k = x if x % 2 == 0 else x + 1 # 若x是2的倍數回傳x否則回傳x+1 print(k) # 4 ``` ---- ## if else 可以巢狀 ```python= if a == b: if b == c: # do something ``` ---- ## CE也可這樣 ```python= x = 9 "A" if x % 2 == 0 else \ "B" if x % 3 == 0 else "C" ``` ---- ## Ans: "B" ---- ## 精闢解說 - `"B" if x % 2 == 0 else ("B" if x % 3 == 0 else "C")` - 回傳`("B" if x % 3 == 0 else "C")` - `("B" if x % 3 == 0 else "C")` - 回傳`"B"` ---- # 回歸正題 ---- ## Original ```python= #[回傳值 迭代 遮罩] [i for i in input() if i in "ATGC"] ``` ## New ```python= #[Expression 迭代 遮罩] [i if i in "ATGC" else 'x' for i in input() ] ``` ---- ## [Expression 迭代 遮罩] --- ## 進階黑魔法練習-1 請輸出1-100之間2的倍數,若他也是3的倍數的話,請輸出原數字,否則請在數字前加上7122 ---- ## Solution ```python= [int("7122"+str(i)) if i % 3 != 0 else i for i in range(1,101) if i % 2 == 0] ``` - for i in range(101) - 迭代1~100之間的整數 - if i % 2 ==0 - 只讓2的倍數通過 - int("7122"+str(i)) if i % 3 != 0 else i - conditional expression ---- ## 進階黑魔法練習-2 ---- ## 改成一行版 ```python= my_list = [] for i in range(0,100): if i % 2 == 0: my_list.append(i) elif i % 7 == 0: my_list.append(-i) else: continue print(mylist) ``` ---- ## Solution ```python= [i if i % 2 == 0 else -i for i in range(0,100)\ if i % 2 == 0 or i % 7 == 0] ``` --- # 精益求精 ---- ## 脈絡 - for -> list comprehension - if else -> list comprehension ---- ## 巢狀迴圈 輸出9x9乘法表 ```python= for i in range(1,10): for j in range(1,10): print(i,j,i*j) ``` ---- ## 黑魔法 ```python= [(i,j,i*j) for i in range(1,10) for j in range(1,10))] ``` ---- ## 黑魔法細講 - Expression - `(i,j,i*j)` - Iter - for i in range(1,10) for j in range(1,10) - 一次 - i 跟 j 都要拿r ---- ## 這不一樣啊 ---- ## 小魔法 (only for py3) ```python= [print(i,j,i*j) for i in range(1,10) for j in range(1,10))] ``` ---- ## 小魔法解說 - 在python3,print是一個function,也就是說他屬於expression,而輸出是他的副作用,在python2,print是一個statement,因此無法丟到list comprehension內。 ---- ## 遮罩 x 巢狀 ---- ## 變形題 1 - 一樣是九九乘法表,但是對於每個數字只要輸出到n x q (q為偶數)。 ---- ## 老解法 ```python= for i in range(1,10): for j in range(1,10): if j % 2 == 0: print(i,j,i*j) ``` ---- ## list comprehension一解 ```python= [print(i,j,i*j) for i in range(1,10) for j in range(2,10,2)] ``` 但是我不要這個 ---- ## Try One xxx要是啥呢 ```python= [print(i,j,i*j) for i in range(1,10) for j in range(1,10) xxx] ``` ---- ## Ans ```python= [print(i,j,i*j) for i in range(1,10) \ for j in range(1,10) if j % 2 == 0] ``` ---- ## 變形題 2 - 一樣是九九乘法表,但是對於每個數字只要輸出到n x q (n小於5)。 ---- ## 老解法 ```python= for i in range(1,10): if i < 5 == 0: for j in range(1,10): print(i,j,i*j) ``` ---- ## list comprehension一解 ```python= [print(i,j,i*j) for i in range(1,5) for j in range(1,10)] ``` 但是我不要這個 ---- ## Try One xxx 或 yyy 要是啥呢 ```python= [print(i,j,i*j) for i in range(1,10) xxx for j in range(1,10) yyy] ``` ---- ## Ans ```python= [print(i,j,i*j) for i in range(1,10) \ if i < 5 for j in range(1,10)] ``` --- ## 還沒完喔XD ---- ## 剛剛的引言 - 巢狀for轉換 ---- ## 巢狀 ? - list comprehension可以巢狀嗎? ---- ## Yape - 因為list是個物件r ---- ## 題目 - 請用list儲存一個 5 x 5 Identity矩陣 ---- ## I 矩陣 ```python= 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 ``` ---- ## 簡單? - 這可是某知名外企面試題r ---- ## 老解法 ```python= identity = [] for i in range(5): row = [] for j in range(5): row.append(1 if i == j else 0) identity.append(row) ``` ---- ## 黑魔法 ```python= [[1 if i == j else 0 for i in range(5)] for j in range(5)] ``` --- ## 觸類旁通 - list : 『我有comprehension,很猛!!!』 - dict : 『你有,我也有 ! ! !』 ---- ## Dict Comprehension - 把 `[]` 換成 `{}` - 從單值便成key value配對 ---- ## Example ```python= {i: "hi"*i for i in range(5)} ``` ---- ## 衝突 - 如果有一個key對應到很多個value呢? ```python= {0: "hi"*i for i in range(5)} ``` - last one survive ---- ## 回憶過去 下列何者會有error呢? `[{ i : i for i in range(5)} for j in range(5)]` `{[i for i in range(5)] : "hi" for q in range(5)}` ---- ## 觸類旁通(誤) - tuple : 『把`[]`改成`()`是不是我也可以了?』 - 謎之聲 : 『拍謝,那是我~』 --- ## Final Challenge ---- ## Problem Given two string a, b, --- # Summary ---- ## Pros 1. 潮 2. 快 3. 看起來很像大神<(_ _)> ---- ## Cons 1. 可讀性低落 2. 腦袋很累 ---- ## Things Not Mentioned - Generator comprehension - Set comprehension --- ## Homework - 一行解掉judge上5題 - 不限題目 - Report - Code - 解說 - Mailto : b04611015@csie.ntu.edu.tw - Title : 資芽作業 『list comprehension』 --- # Q&A

    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