logo

Pythonのany()関数

Python の any() 関数は、指定された iterable(List、Dictionary、Tuple、set など) の要素のいずれかが True の場合に True を返し、それ以外の場合は False を返します。



  Input:   [True, False, False]   Output:   True     Input:   [False, False, False]   Output:   False>

Python any() 関数の構文

Python の any() 関数の構文は次のとおりです。

構文: 任意(反復可能)

  • 反復可能: これは、辞書、タプル、リスト、セットなどの反復可能なオブジェクトです。

戻り値: いずれかの項目が True の場合、True を返します。



Python any() 関数の例

リストに対する Python any() 関数 パイソン 。以下の例では、リスト内の少なくとも 1 つの要素 (3 番目の要素) が True であるため、True を返します。

Python3






# a List of boolean values> l>=> [>False>,>False>,>True>,>False>,>False>]> print>(>any>(l))>

>

>

出力:

True>

Python any() 関数リスト

この例では、any()>関数は、リスト内の値が正しいかどうかを確認するために使用されます。True>。少なくとも 1 つの要素があれば、 Python リスト True>、「True」を返します。それ以外の場合は「False」を返します。さらに、List 内のすべての要素が Python の条件を満たしているかどうかを確認するステップがあります。これは、all()>機能そのもの。

Python3




# All elements of list are True> l>=> [>4>,>5>,>1>]> print>(>any>(l))> # All elements of list are False> l>=> [>0>,>0>,>False>]> print>(>any>(l))> # Some elements of list are> # True while others are False> # l = [1, 0, 6, 7, False]> # print(any(l))> # Empty list> l>=> []> print>(>any>(l))>

>

>

出力:

True False False>

タプルを使用した any() 関数の動作

この例では、any()>機能オン Python タプル 、タプル内の値が true かどうかを確認する方法を提供します。 any() を使用すると、リスト内のすべての項目が True かどうかを確認できます。タプル内の少なくとも 1 つの要素が True の場合、any() 関数は「True」を返し、それ以外の場合は、タプルが空であっても「False」を返します。

Python3




# All elements of tuple are True> t>=> (>2>,>4>,>6>)> print>(>any>(t))> # All elements of tuple are False> t>=> (>0>,>False>,>False>)> print>(>any>(t))> # Some elements of tuple are True while> # others are False> t>=> (>5>,>0>,>3>,>1>,>False>)> print>(>any>(t))> # Empty tuple> t>=> ()> print>(>any>(t))>

>

>

出力:

True False True False>

セットを使用した any() 関数の動作

この例では、any()>機能オン Python セット を使用して、セット内の値が true であるかどうかを確認する方法を示します。セットに対する any() 関数は、リストやタプルに対する場合と同様に動作します。セット内の少なくとも 1 つの要素が「True」と評価された場合、「True」を返します。

Python3




# All elements of set are True> s>=> {>1>,>1>,>3>}> print>(>any>(s))> # All elements of set are False> s>=> {>0>,>0>,>False>}> print>(>any>(s))> # Some elements of set are True while others are False> s>=> {>1>,>2>,>0>,>8>,>False>}> print>(>any>(s))> # Empty set> s>=> {}> print>(>any>(s))>

>

>

出力:

True False True False>

辞書を使用した any() 関数の動作

辞書の場合、すべてのキーが 辞書 false の場合、または辞書が空の場合、Python の any() 関数は False を返します。少なくとも 1 つのキーが True の場合、any() は True を返します。

Python3




# All keys of dictionary are true> d>=> {>1>:>'Hello'>,>2>:>'Hi'>}> print>(>any>(d))> # All keys of dictionary are false> d>=> {>0>:>'Hello'>,>False>:>'Hi'>}> print>(>any>(d))> # Some keys of dictionary> # are true while others are false> d>=> {>0>:>'Salut'>,>1>:>'Hello'>,>2>:>'Hi'>}> print>(>any>(d))> # Empty dictionary> d>=> {}> print>(>any>(d))>

>

>

出力:

True False True False>

文字列を使用した any() 関数の動作

この例では、Python の any() 関数がどのように動作するかを見ていきます。 Python 文字列 。 any() 関数は、文字列に少なくとも 1 文字があれば True を返します。この使用法は、any()>関数を使用すると、文字列内の値が true かどうかをチェックして、文字列が空かどうかを効果的に判断できます。

Python3




# Non-Empty String> s>=> 'Hi There!'> print>(>any>(s))> # Non-Empty String> s>=> '000'> print>(>any>(s))> # Empty string> s>=> ''> print>(>any>(s))>

>

>

出力:

True True False>

条件付きの Python any() 関数

この例では、any()>Python の関数は、条件を満たす要素をチェックし、True 値が見つかった場合は True を返します。この関数は、Python でリスト内のすべてまたは任意の要素が条件を満たしているかどうかを確認する場合に特に便利です。これは、反復可能な要素内の少なくとも 1 つの要素が true であるかどうかを判断する便利な方法を提供します。

Python3




# Python3 code to demonstrate working of any()> # To Check if any element in list satisfies a condition> # initializing list> test_list>=> [>4>,>5>,>8>,>9>,>10>,>17>]> # printing list> print>(>'The original list : '>, test_list)> # Check if any element in list satisfies a condition> # Using any()> res>=> any>(ele>>>10> for> ele>in> test_list)> # Printing result> print>(>'Does any element satisfy specified condition ? : '>, res)>

>

>

出力:

The original list : [4, 5, 8, 9, 10, 17] Does any element satisfy specified condition ? : True>

P For ループを使用した ython any() 関数

この例では、次を使用して any() 関数を実装します。 Python 関数 そして for ループ そして、List 内のすべての要素が True であるかどうかを確認します。 my_any() 関数は、反復可能要素のいずれかが True の場合は True を返し、それ以外の場合は False を返します。

Python3




# this function gives same result as built-in any() function> def> my_any(list_x):> >for> item>in> list_x:> >if> item:> >return> True> >return> False> x>=> [>4>,>5>,>8>,>9>,>10>,>17>]> print>(my_any(x))>

夕食対夕食
>

>

出力:

True>