La funzione Python any() restituisce True se uno qualsiasi degli elementi di un dato iterabile (Lista, Dizionario, Tupla, set, ecc.) è True altrimenti restituisce False.
Esempio
Input: [True, False, False] Output: True Input: [False, False, False] Output: False>
Sintassi della funzione Python any()
La funzione any() in Python ha la seguente sintassi:
Sintassi: qualsiasi (iterabile)
- Iterabile: È un oggetto iterabile come un dizionario, una tupla, una lista, un set, ecc.
Ritorna: Restituisce Vero se uno qualsiasi degli elementi è Vero.
Python any() Esempio di funzione
Funzione Python any() sulle liste in Pitone . L'esempio seguente restituisce True poiché almeno un elemento nell'elenco (terzo elemento) è True.
Python3
# a List of boolean values> l>=> [>False>,>False>,>True>,>False>,>False>]> print>(>any>(l))> |
>
>
Produzione:
True>
Elenchi di funzioni Python any()
In questo esempio, ilany()>la funzione viene utilizzata per verificare se qualche valore nell'elenco lo èTrue>. Se almeno un elemento in Elenco Python ÈTrue>, restituirà 'Vero'; altrimenti restituirà 'False'. Inoltre, c'è un passaggio per verificare se tutti gli elementi in List soddisfano le condizioni in Python. Ciò si ottiene utilizzando ilall()>funzione stessa.
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))> |
>
>
Produzione:
True False False>
Funzionamento della funzione any() con tuple
In questo esempio vedremo l'uso diany()>funzione attivata Tuple Python , fornendo un modo per verificare se qualche valore è vero in una tupla. Usando any() possiamo verificare se tutti gli elementi in un elenco sono True. Se almeno un singolo elemento nella tupla è True, la funzione any() restituirà 'True' altrimenti restituirà 'False' anche se la tupla è vuota.
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))> |
>
>
Produzione:
True False True False>
Funzionamento di any() Funzione con Sets
In questo esempio vedremo l'uso diany()>funzione attivata Set Python , dimostrando come può essere utilizzato per verificare se qualche valore è vero in un insieme. La funzione any() sugli insiemi agisce in modo simile come per una lista o una tupla. Se almeno un singolo elemento in un set risulta essere 'Vero', restituirà 'Vero'.
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))> |
>
generici Java
>
Produzione:
True False True False>
Funzionamento della funzione any() con dizionari
Nel caso di un dizionario, se tutte le chiavi del file dizionario sono false o il dizionario è vuoto, la funzione any() in Python restituisce False. Se almeno una chiave è True, any() restituisce 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))> |
>
>
Produzione:
True False True False>
Funzionamento della funzione any() con Strings
In questo esempio, vedremo come funziona la funzione Python any() Stringa di pitone . La funzione any() restituisce True, se è presente almeno 1 carattere nella stringa. Questo utilizzo delany()>La funzione ti consente di verificare se qualche valore è vero in una stringa, determinando di fatto se la stringa è vuota o meno.
Python3
# Non-Empty String> s>=> 'Hi There!'> print>(>any>(s))> # Non-Empty String> s>=> '000'> print>(>any>(s))> # Empty string> s>=> ''> print>(>any>(s))> |
>
>
Produzione:
True True False>
Python any() Funzione con una condizione
In questo esempio, ilany()>La funzione in Python controlla qualsiasi elemento che soddisfa una condizione e restituisce True nel caso in cui trovi un valore True. Questa funzione è particolarmente utile per verificare se tutti/qualsiasi elemento in List soddisfano le condizioni in Python. Fornisce un modo conveniente per determinare se almeno un elemento in un iterabile è vero.
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)> |
>
>
Produzione:
The original list : [4, 5, 8, 9, 10, 17] Does any element satisfy specified condition ? : True>
P ython any() Funzione con ciclo For
In questo esempio, implementeremo la funzione any() utilizzando Funzioni Python e un per ciclo e per verificare se tutti gli elementi in List sono True. La funzione my_any() restituisce True se qualsiasi elemento dell'iterabile è True, altrimenti restituisce 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))> |
>
>
Produzione:
True>