<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # Python `filter()`函數 > 原文: [https://thepythonguru.com/python-builtin-functions/filter/](https://thepythonguru.com/python-builtin-functions/filter/) * * * 于 2020 年 1 月 7 日更新 * * * `filter()`函數將一個函數和一個序列作為參數并返回一個可迭代的對象,僅按順序產生要為其返回`True`的項目。 如果傳遞了`None`而不是函數,則將求值為`False`的序列中的所有項目刪除。 `filter()`的語法如下: **語法**: `filter(function or None, iterable) --> filter object` 這是一個例子: **Python 3** ```py >>> >>> def is_even(x): ... if x % 2 == 0: ... return True ... else: ... return False ... >>> >>> f = filter(is_even, [1, 3, 10, 45, 6, 50]) >>> >>> f <filter object at 0x7fcd88d54eb8> >>> >>> >>> for i in f: ... print(i) ... 10 6 50 >>> ``` 試試看: ```py def is_even(x): if x % 2 == 0: return True else: return False f = filter(is_even, [1, 3, 10, 45, 6, 50]) print(f) for i in f: print(i) ``` 要立即產生結果,我們可以使用`list()`函數。 **Python 3** ```py >>> >>> list(filter(is_even, [1, 3, 10, 45, 6, 50])) [10, 6, 50] >>> >>> >>> list(filter(None, [1, 45, "", 6, 50, 0, {}, False])) # function argument is None [1, 45, 6, 50] >>> ``` 試一試: ```py def is_even(x): if x % 2 == 0: return True else: return False print( list(filter(is_even, [1, 3, 10, 45, 6, 50])) ) # function argument is None print( list(filter(None, [1, 45, "", 6, 50, 0, {}, False])) ) ``` 在 Python 2 中,`filter()`返回實際列表(這不是處理大數據的有效方法),因此您無需將`filter()`包裝在`list()`調用中。 **Python 2** ```py >>> >>> filter(is_even, [1, 3, 10, 45, 6, 50]) [10, 6, 50] >>> ``` 這是其他一些例子。 **Python 3** ```py >>> >>> filter(lambda x: x % 2 != 0, [1, 3, 10, 45, 6, 50]) # lambda is used in place of a function [1, 3, 45] >>> >>> >>> list(filter(bool, [10, "", "py"])) [10, 'py'] >>> >>> >>> import os >>> >>> # display all files in the current directory (except the hidden ones) >>> list(filter(lambda x: x.startswith(".") != True, os.listdir(".") )) ['Documents', 'Downloads', 'Desktop', 'Pictures', 'bin', 'opt', 'Templates', 'Public', 'Videos', 'Music'] >>> ``` 試一試: ```py # lambda is used in place of a function print(filter(lambda x: x % 2 != 0, [1, 3, 10, 45, 6, 50])) print(list(filter(bool, [10, "", "py"]))) import os # display all files in the current directory (except the hidden ones) print(list(filter(lambda x: x.startswith(".") != True, os.listdir(".") )) ) ``` * * * * * *
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看