サーチ…


構文

  • フィルタ(関数、反復可能)
  • itertools.ifilter(function、iterable)
  • future_builtins.filter(関数、反復可能)
  • itertools.ifilterfalse(関数、反復可能)
  • itertools.filterfalse(function、iterable)

パラメーター

パラメータ詳細
関数状態または決定呼び出し可能な None次にフィルタリングに恒等関数を使用する( 位置のみ
繰り返し可能なフィルタリングされる反復可能( 位置限定

備考

ほとんどの場合、 理解やジェネレータの表現は、 filter()ifilter()より読みやすく、強力で、効率的です。

フィルターの基本使用

するために、 filter破棄にいくつかの基準に基づいてシーケンスの要素を:

names = ['Fred', 'Wilma', 'Barney']

def long_name(name):
    return len(name) > 5
Python 2.x 2.0
filter(long_name, names)
# Out: ['Barney']

[name for name in names if len(name) > 5] # equivalent list comprehension
# Out: ['Barney']


from itertools import ifilter
ifilter(long_name, names)       # as generator (similar to python 3.x filter builtin)
# Out: <itertools.ifilter at 0x4197e10>
list(ifilter(long_name, names)) # equivalent to filter with lists
# Out: ['Barney']

(name for name in names if len(name) > 5) # equivalent generator expression
# Out: <generator object <genexpr> at 0x0000000003FD5D38>
Python 2.x 2.6
# Besides the options for older python 2.x versions there is a future_builtin function:
from future_builtins import filter
filter(long_name, names)       # identical to itertools.ifilter
# Out: <itertools.ifilter at 0x3eb0ba8>
Python 3.x 3.0
filter(long_name, names)        # returns a generator
# Out: <filter at 0x1fc6e443470>
list(filter(long_name, names))  # cast to list
# Out: ['Barney']

(name for name in names if len(name) > 5) # equivalent generator expression
# Out: <generator object <genexpr> at 0x000001C6F49BF4C0>

機能のないフィルタリング

functionパラメータがNone場合、アイデンティティ関数が使用されます。

list(filter(None, [1, 0, 2, [], '', 'a']))  # discards 0, [] and ''   
# Out: [1, 2, 'a']
Python 2.x 2.0.1
[i for i in [1, 0, 2, [], '', 'a'] if i] # equivalent list comprehension
Python 3.x 3.0.0
(i for i in [1, 0, 2, [], '', 'a'] if i) # equivalent generator expression

短絡チェックとしてフィルタリングする

filter (python 3.x)とifilter (python 2.x)はジェネレータを返すので、 orandような短絡テストを作成するときに非常に便利です。

Python 2.x 2.0.1
 # not recommended in real use but keeps the example short:
from itertools import ifilter as filter
Python 2.x 2.6.1
 from future_builtins import filter

100より小さい最初の要素を見つけるには:

car_shop = [('Toyota', 1000), ('rectangular tire', 80), ('Porsche', 5000)]
def find_something_smaller_than(name_value_tuple):
    print('Check {0}, {1}$'.format(*name_value_tuple)
    return name_value_tuple[1] < 100
next(filter(find_something_smaller_than, car_shop))
# Print: Check Toyota, 1000$
#        Check rectangular tire, 80$
# Out: ('rectangular tire', 80)

next関数は次の要素(この場合は最初の要素)を与えるので、短絡する理由です。

相補関数:filterfalse、ifilterfalse

itertools -moduleにはfilterための補完的な関数がありfilter

Python 2.x 2.0.1
 # not recommended in real use but keeps the example valid for python 2.x and python 3.x
from itertools import ifilterfalse as filterfalse
Python 3.x 3.0.0
from itertools import filterfalse

ジェネレータ filterまったく同じように動作しfilterが、 False要素のみを保持します。

# Usage without function (None):
list(filterfalse(None, [1, 0, 2, [], '', 'a']))  # discards 1, 2, 'a' 
# Out: [0, [], '']

# Usage with function
names = ['Fred', 'Wilma', 'Barney']

def long_name(name):
    return len(name) > 5

list(filterfalse(long_name, names))
# Out: ['Fred', 'Wilma']

# Short-circuit useage with next:
car_shop = [('Toyota', 1000), ('rectangular tire', 80), ('Porsche', 5000)]
def find_something_smaller_than(name_value_tuple):
    print('Check {0}, {1}$'.format(*name_value_tuple)
    return name_value_tuple[1] < 100
next(filterfalse(find_something_smaller_than, car_shop))
# Print: Check Toyota, 1000$
# Out: ('Toyota', 1000)

# Using an equivalent generator:
car_shop = [('Toyota', 1000), ('rectangular tire', 80), ('Porsche', 5000)]
generator = (car for car in car_shop if not car[1] < 100)
next(generator)


Modified text is an extract of the original Stack Overflow Documentation
ライセンスを受けた CC BY-SA 3.0
所属していない Stack Overflow