Sai - Đầu ra được trả về Sai nếu tất cả các phần tử trong một lần lặp là sai. Bất cứ khi nào đối tượng có thể lặp lại trống trong khi thực thi các hàm any(), chúng tôi sẽ nhận được đầu ra là Sai
Để có cái nhìn rõ hơn về tất cả các tình huống có thể xảy ra, chúng ta hãy tìm hiểu sâu hơn bên dưới
Các tình huống có thể xảy ra Giá trị đầu raTất cả có thể lặp lại là đúngTrueTất cả có thể lặp lại là saiSaiMột có thể lặp lại là đúng (những lần lặp khác là sai)TrueMột lần lặp lại là sai (những lần lặp lại khác là đúng)TrueEmpty IterableSai
Ngoại lệ của any() trong Python
Đối với các trường hợp ngoại lệ đối với any() trong python được xem xét, chúng ta cần lưu ý những điểm sau
Bất cứ khi nào chúng ta xử lý hàm any() với Từ điển, chúng ta cần nhớ rằng các khóa được đánh giá cho các tình huống trên
Điều này có nghĩa là nếu bất kỳ khóa nào của từ điển là đúng, thì đầu ra là đúng;
Bất cứ khi nào chúng ta xử lý các số nguyên như 0, 1, 2. trong các chuỗi khi chúng tôi giữ chúng trong "" (dấu phẩy đảo ngược), đầu ra luôn TRUE
Ví dụ về any() trong Python
# Consider two boolean lists having values as below. boolean_list_1 = [1, True, 0] boolean_list_2 = [0, False, 0] # Checking if the above lists with values result in output as TRUE or FALSE with any() function in python output_1 = any(boolean_list_1) output_2 = any(boolean_list_2) # Printing the output of the boolean lists print('output_1 = ', output_1) print('output_2 = ', output_2)
đầu ra
output_1 = True output_2 = False
Hàm any() trong Python là gì?
Trong bài viết này, chúng tôi sẽ đề cập đến hàm có sẵn trong python - Hàm any()
Trong hàm any() của python, đối tượng iterables ( list, tuples, set, v.v. ) được lấy làm đối số
any() trong python có thể được coi là một hàm Python sẵn có trả về đầu ra là True nếu bất kỳ giá trị nào của một đối tượng có thể lặp đã cho như Tuples, Strings, Set, List hoặc Dictionary là True
Trong khi thực thi, đôi khi, nếu đối tượng có thể lặp lại trống, đầu ra sẽ trở thành Sai
Điều này cũng ngụ ý rằng hàm any() của python trả về Sai nếu tất cả các phần tử trong một đối tượng có thể lặp lại như Tuples, Strings, List hoặc Dictionary là Sai
Ví dụ về hàm any() trong Python
ví dụ 1. any() trong Python với Danh sách
# Each list considers the five scenarios which we just understood theoretically # list_1 explains the First Scenario - all values are TRUE list_1 = [9, 1, 8, 3, True] result_1 = any(list_1) print("The OUTPUT of the list_1 is :", result_1) # list_2 explains the Second Scenario - all values are FALSE list_2 = [False, 0] result_2 = any(list_2) print("The OUTPUT of the list_2 is :", result_2) # list_3 explains the Third Scenario - One value is true [others are false] list_3 = [False, 4, 0] result_3 = any(list_3) print("The OUTPUT of the list_3 is :", result_3) # list_4 explains the Fourth Scenario - One value is false [others are true] list_4 = [8, 9, 2, 0] result_4 = any(list_4) print("The OUTPUT of the list_4 is :", result_4) # list_5 explains the Fifth Scenario - Empty Iterable list_5 = [] result_5 = any(list_5) print("The OUTPUT of the list_5 is :", result_5)
đầu ra
The OUTPUT of the list_1 is : True The OUTPUT of the list_2 is : False The OUTPUT of the list_3 is : True The OUTPUT of the list_4 is : True The OUTPUT of the list_5 is : False
ví dụ 2. any() trong Python với Tuples
# Each tuple considers the five scenarios which we just understood theoretically # tuple_1 explains the First Scenario - all values are TRUE tuple_1 = (9, 1, 8, 3, True) result_1 = any(tuple_1) print("The OUTPUT of the tuple_1 is :", result_1) # tuple_2 explains the Second Scenario - all values are FALSE tuple_2 = (False, 0) result_2 = any(tuple_2) print("The OUTPUT of the tuple_2 is :", result_2) # tuple_3 explains the Third Scenario - One value is true (others are false) tuple_3 = (False, 4, 0) result_3 = any(tuple_3) print("The OUTPUT of the tuple_3 is :", result_3) # tuple_4 explains the Fourth Scenario - One value is false (others are true) tuple_4 = (8, 9, 2, 0) result_4 = any(tuple_4) print("The OUTPUT of the tuple_4 is :", result_4) # tuple_5 explains the Fifth Scenario - Empty Iterable tuple_5 = () result_5 = any(tuple_5) print("The OUTPUT of the tuple_5 is :", result_5)
đầu ra
The OUTPUT of the tuple_1 is : True The OUTPUT of the tuple_2 is : False The OUTPUT of the tuple_3 is : True The OUTPUT of the tuple_4 is : True The OUTPUT of the tuple_5 is : False
ví dụ 3. any() bằng Python với Bộ
# Each set considers the five scenarios which we just understood theoretically # set_1 explains the First Scenario - all values are TRUE set_1 = {9, 1, 8, 3, True} result_1 = any(set_1) print("The OUTPUT of the set_1 is :",result_1) # set_2 explains the Second Scenario - all values are FALSE set_2 = {False , 0} result_2 = any(set_2) print("The OUTPUT of the set_2 is :",result_2) # set_3 explains the Third Scenario - One value is true (others are false) set_3 = {False, 4 , 0} result_3 = any(set_3) print("The OUTPUT of the set_3 is :",result_3) # set_4 explains the Fourth Scenario - One value is false (others are true) set_4 = {8, 9, 2, 0} result_4 = any(set_4) print("The OUTPUT of the set_4 is :",result_4) # set_5 explains the Fifth Scenario - Empty Iterable set_5 = {} result_5 = any(set_5) print("The OUTPUT of the set_5 is :",result_5)
đầu ra
The OUTPUT of the set_1 is : True The OUTPUT of the set_2 is : False The OUTPUT of the set_3 is : True The OUTPUT of the set_4 is : True The OUTPUT of the set_5 is : False
Ví dụ 4. any() trong Python với Từ điển
MẸO. Bất cứ khi nào chúng ta xử lý hàm any() với Từ điển, chúng ta cần nhớ rằng các khóa được đánh giá cho các tình huống được đề cập ở trên
Điều này có nghĩa là nếu bất kỳ khóa nào của từ điển là đúng, thì đầu ra là đúng;
________số 8_______đầu ra
The OUTPUT of the dict_1 is : True The OUTPUT of the dict_2 is : False The OUTPUT of the dict_3 is : True The OUTPUT of the dict_4 is : True The OUTPUT of the dict_5 is : False
Ví dụ 5. any() trong Python có Chuỗi
Bất cứ khi nào chúng ta xử lý hàm any() với một đối tượng có thể lặp lại dưới dạng chuỗi, chúng ta có thể có hai tình huống sau đây
- Chuỗi NON-EMPTY cho đầu ra là TRUE
- Chuỗi EMPTY cho đầu ra là FALSE
MẸO. Bất cứ khi nào chúng ta xử lý các số nguyên như 0, 1, 2. trong các chuỗi khi chúng tôi giữ chúng trong "" (dấu phẩy đảo ngược), đầu ra luôn TRUE