[英]Is there an inbuilt `all or not any` in Python?
只是想知道是否有人知道內置版本:
bool_list: List[bool] = # some non-empty list of booleans
result = all(bool_list) or not any(bool_list)
# What I'm looking for
result = allsame(bool_list)
不是內置的,而是以一種有效的方式完全滿足您的需求:
來自itertools 食譜:
def all_equal(iterable):
"Returns True if all the elements are equal to each other"
g = groupby(iterable)
return next(g, True) and not next(g, False)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.