파이썬 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() 함수 파이썬 . 아래 예에서는 목록의 최소 하나의 요소(세 번째 요소)가 True이므로 True를 반환합니다.

파이썬3




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

산출:

True 

Python any() 함수 목록

이 예에서는 any()> 함수는 목록에 있는 값이 있는지 확인하는 데 사용됩니다. True> . 적어도 하나의 요소가 있는 경우 파이썬 목록 ~이다 True> , 'True'를 반환합니다. 그렇지 않으면 'False'를 반환합니다. 추가적으로 List의 모든 요소가 Python의 조건을 충족하는지 확인하는 단계가 있습니다. 이는 다음을 사용하여 달성됩니다. all()> 기능 자체.

파이썬3




# 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()> 기능 켜짐 파이썬 튜플 , 튜플에서 값이 true인지 확인하는 방법을 제공합니다. any()를 사용하면 목록의 모든 항목이 True인지 확인할 수 있습니다. 튜플의 요소 중 하나라도 True이면 any() 함수는 'True'를 반환하고, 그렇지 않으면 튜플이 비어 있어도 'False'를 반환합니다.

파이썬3




# 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()> 기능 켜짐 파이썬 세트 , 집합의 값이 참인지 확인하는 데 어떻게 사용할 수 있는지 보여줍니다. 집합의 any() 함수는 목록이나 튜플의 경우와 유사하게 작동합니다. 집합에서 최소한 하나의 요소가 'True'로 평가되면 'True'를 반환합니다.

파이썬3




# 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를 반환합니다. 하나 이상의 키가 True이면 any()는 True를 반환합니다.

파이썬3




# 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() 함수가 어떻게 작동하는지 살펴보겠습니다. 파이썬 문자열 . any() 함수는 문자열에 문자가 1개 이상 있으면 True를 반환합니다. 이 사용법은 any()> 함수를 사용하면 문자열의 값이 true인지 확인하여 문자열이 비어 있는지 여부를 효과적으로 확인할 수 있습니다.

파이썬3




# 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를 반환합니다. 이 함수는 List의 모든 요소/모든 요소가 Python의 조건을 충족하는지 확인하는 데 특히 유용합니다. 이는 iterable의 하나 이상의 요소가 true인지 확인하는 편리한 방법을 제공합니다.

파이썬3




# 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 

For 루프가 있는 ython any() 함수

이 예에서는 다음을 사용하여 any() 함수를 구현합니다. 파이썬 함수 그리고 for 루프 List의 모든 요소가 True인지 확인합니다. my_any() 함수는 iterable의 요소 중 하나라도 True이면 True를 반환하고, 그렇지 않으면 False를 반환합니다.

파이썬3




# 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