如何使用Python';所有的功能都工作吗?
我试图理解如何使用Python';所有的功能都工作吗?,python,Python,我试图理解any()和all()Python内置函数是如何工作的 我试图比较元组,以便如果任何值不同,那么它将返回True,如果它们都相同,那么它将返回False。他们是如何在这种情况下返回[假,假,假] d是一个defaultdict(列表) 据我所知,这应该是输出 # [False, True, False] 因为(1,1)是相同的,(5,6)是不同的,(0,0)是相同的 为什么所有元组的计算结果都为False?您所问的代码来自我给出的答案。它旨在解决比较多位数组的问题,即1和0的集合 a
any()
和all()
Python内置函数是如何工作的
我试图比较元组,以便如果任何值不同,那么它将返回True
,如果它们都相同,那么它将返回False
。他们是如何在这种情况下返回[假,假,假]
d
是一个defaultdict(列表)
据我所知,这应该是输出
# [False, True, False]
因为(1,1)是相同的,(5,6)是不同的,(0,0)是相同的
为什么所有元组的计算结果都为False?您所问的代码来自我给出的答案。它旨在解决比较多位数组的问题,即
1
和0
的集合
any
和all
在您可以依赖值的“真实性”时非常有用,即它们在布尔上下文中的值。1是True
,0是False
,这是答案所利用的便利。5恰好也是真的
,所以当你把它混合到可能的输入中时。。。好。不起作用
您可以这样做:
[len(set(x)) > 1 for x in zip(*d['Drd2'])]
它缺乏上一个答案的美感(我真的很喜欢
any(x)而不是all(x)
),但它完成了任务。你可以粗略地将any
和all
分别看作是一系列逻辑或和运算符
任何
any
将在至少一个元素为Truthy时返回True
。了解
全部
all
仅当所有元素均为Truthy时才会返回True
真值表
+-----------------------------------------+---------+---------+
||任何|所有|
+-----------------------------------------+---------+---------+
|所有真实的价值观都是真实的|
+-----------------------------------------+---------+---------+
|所有Falsy值| False | False|
+-----------------------------------------+---------+---------+
|一个真实的价值(所有其他的都是虚假的)|真|假|
+-----------------------------------------+---------+---------+
|一个虚假的价值(所有其他的都是真实的)|真|假|
+-----------------------------------------+---------+---------+
|空可数|假|真|
+-----------------------------------------+---------+---------+
注1:官方文件中解释了空的iterable案例,如下所示
如果iterable的任何元素为True,则返回True
如果iterable为空,则返回False
由于没有一个元素为true,因此在本例中返回False
如果iterable的所有元素都为True(或iterable为空,则返回True
)
由于没有一个元素为false,因此在本例中返回True
注2:
关于any
和all
另一个需要了解的重要事项是,一旦他们知道结果,执行就会短路。优点是,整个iterable无需消耗。比如说,
>>> multiples_of_6 = (not (i % 6) for i in range(1, 10))
>>> any(multiples_of_6)
True
>>> list(multiples_of_6)
[False, False, False]
这里,(对于范围(1,10)中的i不是(i%6))
是一个生成器表达式,如果1和9中的当前数字是6的倍数,则返回True
any
迭代乘以\u 6
,当它遇到6
时,它会找到一个真实的值,因此它会立即返回True
,其余的乘以\u 6
。这就是我们在打印列表(6的倍数)
时看到的结果,7
、8
和9
这件优秀的东西被巧妙地运用在日常生活中
有了这个基本的理解,如果我们看一下你的代码,你就知道了
any(x) and not all(x)
这确保了,至少有一个值是真实的,但不是所有的。这就是它返回[False,False,False]
的原因。如果你真的想检查两个数字是否不一样
print [x[0] != x[1] for x in zip(*d['Drd2'])]
Python的any
和all
函数是如何工作的?
any
和all
获取iterables并返回True
如果元素中的任何元素和所有元素(分别)为True
>>> any([0, 0.0, False, (), '0']), all([1, 0.0001, True, (False,)])
(True, True) # ^^^-- truthy non-empty string
>>> any([0, 0.0, False, (), '']), all([1, 0.0001, True, (False,), {}])
(False, False) # ^^-- falsey
如果iterables为空,any
返回False
,all
返回True
>>> any([]), all([])
(False, True)
今天我在课堂上为学生演示所有
和任何
。他们对空iterables的返回值大多感到困惑。用这种方式解释会使许多灯泡亮起
短路行为
它们,any
和all
,都在寻找允许它们停止计算的条件。我给出的第一个示例要求他们计算整个列表中每个元素的布尔值
(请注意,list literal本身并不是惰性计算的——您可以通过迭代器得到它——但这只是为了说明目的。)
下面是任意和全部的Python实现:
def any(iterable):
for i in iterable:
if i:
return True
return False # for an empty iterable, any returns False!
def all(iterable):
for i in iterable:
if not i:
return False
return True # for an empty iterable, all returns True!
当然,真正的实现是用C语言编写的,而且性能更高,但是您可以用上面的方法代替,并用这个(或任何其他)答案中的代码得到相同的结果
all
all
检查元素是否为False
(这样它就可以返回False
),如果没有False
,则返回True
>>> all([1, 2, 3, 4]) # has to test to the end!
True
>>> all([0, 1, 2, 3, 4]) # 0 is False in a boolean context!
False # ^--stops here!
>>> all([])
True # gets to end, so True!
any
any
的工作方式是检查元素是否为True
(以便返回
>>> all([1, 2, 3, 4]) # has to test to the end!
True
>>> all([0, 1, 2, 3, 4]) # 0 is False in a boolean context!
False # ^--stops here!
>>> all([])
True # gets to end, so True!
>>> any([0, 0.0, '', (), [], {}]) # has to test to the end!
False
>>> any([1, 0, 0.0, '', (), [], {}]) # 1 is True in a boolean context!
True # ^--stops here!
>>> any([])
False # gets to end, so False!
def noisy_iterator(iterable):
for i in iterable:
print('yielding ' + repr(i))
yield i
>>> all(noisy_iterator([1, 2, 3, 4]))
yielding 1
yielding 2
yielding 3
yielding 4
True
>>> all(noisy_iterator([0, 1, 2, 3, 4]))
yielding 0
False
>>> any(noisy_iterator([0, 0.0, '', (), [], {}]))
yielding 0
yielding 0.0
yielding ''
yielding ()
yielding []
yielding {}
False
>>> any(noisy_iterator([1, 0, 0.0, '', (), [], {}]))
yielding 1
True
static PyObject *
builtin_any(PyObject *module, PyObject *iterable)
{
PyObject *it, *item;
PyObject *(*iternext)(PyObject *);
int cmp;
it = PyObject_GetIter(iterable);
if (it == NULL)
return NULL;
iternext = *Py_TYPE(it)->tp_iternext;
for (;;) {
item = iternext(it);
if (item == NULL)
break;
cmp = PyObject_IsTrue(item);
Py_DECREF(item);
if (cmp < 0) {
Py_DECREF(it);
return NULL;
}
if (cmp > 0) {
Py_DECREF(it);
Py_RETURN_TRUE;
}
}
Py_DECREF(it);
if (PyErr_Occurred()) {
if (PyErr_ExceptionMatches(PyExc_StopIteration))
PyErr_Clear();
else
return NULL;
}
Py_RETURN_FALSE;
}
static PyObject *
builtin_all(PyObject *module, PyObject *iterable)
{
PyObject *it, *item;
PyObject *(*iternext)(PyObject *);
int cmp;
it = PyObject_GetIter(iterable);
if (it == NULL)
return NULL;
iternext = *Py_TYPE(it)->tp_iternext;
for (;;) {
item = iternext(it);
if (item == NULL)
break;
cmp = PyObject_IsTrue(item);
Py_DECREF(item);
if (cmp < 0) {
Py_DECREF(it);
return NULL;
}
if (cmp == 0) {
Py_DECREF(it);
Py_RETURN_FALSE;
}
}
Py_DECREF(it);
if (PyErr_Occurred()) {
if (PyErr_ExceptionMatches(PyExc_StopIteration))
PyErr_Clear();
else
return NULL;
}
Py_RETURN_TRUE;
}
>>> any([False, False, False])
False
>>> any([False, True, False])
True
>>> all([False, True, True])
False
>>> all([True, True, True])
True
s = "eFdss"
s = list(s)
all(i.islower() for i in s ) # FALSE
any(i.islower() for i in s ) # TRUE
def any(iterable):
for item in iterable:
if item:
return True
return False
def all(iterable):
for item in iterable:
if not item:
return False
return True
M =[(1, 1), (5, 6), (0, 0)]
1) print([any(x) for x in M])
[True, True, False] #only the last tuple does not have any true element
2) print([all(x) for x in M])
[True, True, False] #all elements of the last tuple are not true
3) print([not all(x) for x in M])
[False, False, True] #NOT operator applied to 2)
4) print([any(x) and not all(x) for x in M])
[False, False, False] #AND operator applied to 1) and 3)
# if we had M =[(1, 1), (5, 6), (1, 0)], we could get [False, False, True] in 4)
# because the last tuple satisfies both conditions: any of its elements is TRUE
#and not all elements are TRUE
list = [1,1,1,0]
print(any(list)) # will return True because there is 1 or True exists
print(all(list)) # will return False because there is a 0 or False exists
return all(a % i for i in range(3, int(a ** 0.5) + 1)) # when number is divisible it will return False else return True but the whole statement is False .