Home > Backend Development > Python Tutorial > A brief introduction to the use of any() and all() in Python

A brief introduction to the use of any() and all() in Python

不言
Release: 2018-09-14 16:59:24
Original
6505 people have browsed it

This article brings you a brief introduction to the use of any() and all() in Python. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Introduction

In ordinary text processing work, I often encounter such a situation: use python to determine whether a string contains a elements in the list.

It will be very concise to use python’s built-in function any() at this time:

fruits = ['apple', 'orange', 'peach']
str = "I want some apples"
if any(element in str for element in fruits):    
print "string contains some fruits."
Copy after login

any()

In fact The any function is very simple: determine whether a tuple or list is all empty, 0, False. If it is all empty, 0, False, it returns False; if it is not all empty, it returns True.

all()

all function is just the opposite of any: determine whether a tuple or list is complete Is not empty, 0, False. If none are empty, returns True; otherwise returns False.

It should be noted here that the return value of empty tuple and empty list is True

Related recommendations:

python Usage analysis of map, any, and all functions

A brief introduction to the use of the readline() method in Python

The above is the detailed content of A brief introduction to the use of any() and all() in Python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template