I am attempting to use all()
but it is not working for me:
我试图使用all(),但它不适合我:
>>> names = ["Rhonda", "Ryan", "Red Rackham", "Paul"]
>>> all([name for name in names if name[0] == "R"])
True
>>>
I am trying to check if all the names begin with "R"
, and even though I added "Paul"
to names
, all()
still returns True
. How do I fix this so that all()
returns False
until "Paul"
is removed?
我试图检查所有名称是否以“R”开头,即使我将“Paul”添加到名称中,all()仍然返回True。我如何解决这个问题,以便all()返回False,直到删除“Paul”为止?
4 个解决方案
#1
You misunderstand how all
works. From the docs:
你误解了一切是如何运作的。来自文档:
all(iterable)
Return
True
if all elements of theiterable
are true (or if theiterable
is empty).如果iterable的所有元素都为true(或者iterable为空),则返回True。
In your code, you are first collecting all names that start with R
into a list and then passing this list to all
. Doing this will always return True
because non-empty strings evaluate to True
.
在您的代码中,您首先将所有以R开头的名称收集到列表中,然后将此列表传递给所有名称。执行此操作将始终返回True,因为非空字符串计算为True。
Instead, you should write:
相反,你应该写:
all(name[0] == "R" for name in names)
This will pass an iterable of booleans to all
. If all of them are True
, the function will return True
; otherwise, it will return False
.
这会将一个可迭代的布尔值传递给所有人。如果所有这些都是True,则该函数将返回True;否则,它将返回False。
As an added bonus, the result will now be computed lazily because we used a generator expression instead of a list comprehension. With the list comprehension, the code needed to test all strings before determining a result. The new code however will only check as many as necessary.
作为额外的奖励,现在结果将被懒惰地计算,因为我们使用了生成器表达式而不是列表推导。使用列表理解,在确定结果之前测试所有字符串所需的代码。但是,新代码只会根据需要进行检查。
#2
names = ["Rhonda", "Ryan", "Red Rackham", "Paul"]
print all(map(lambda name: name[0] == "R", names))
# prints False
names = ["Rhonda", "Ryan", "Red Rackham"]
print all(map(lambda name: name[0] == "R", names))
# prints True
#3
names = ["Rhonda", "Ryan", "Red Rackham", "Paul"]
if all(c[0] == "R" for c in names):
print "ALL MATCH"
Demo:
#4
The reason why you were getting wrong result was because you are already creating a new list using list comprehension by applying the desired condition, So if we do a little breakdown then:
你得到错误结果的原因是因为你已经通过应用所需条件使用列表理解创建了一个新列表,所以如果我们做了一点细分那么:
>>> print [name for name in names if name[0] == "R"]
>>> ['Rhonda', 'Ryan', 'Red Rackham']
>>> print all(['Rhonda', 'Ryan', 'Red Rackham'])
>>> True
So the correct way may be:
所以正确的方法可能是:
names = ["Rhonda", "Ryan", "Red Rackham", "Paul"]
res = all(map(lambda x : x[0]=="R", names))
# map() returns: [True, True, True, False]
# all([True, True, True, False]) == False
print res
#1
You misunderstand how all
works. From the docs:
你误解了一切是如何运作的。来自文档:
all(iterable)
Return
True
if all elements of theiterable
are true (or if theiterable
is empty).如果iterable的所有元素都为true(或者iterable为空),则返回True。
In your code, you are first collecting all names that start with R
into a list and then passing this list to all
. Doing this will always return True
because non-empty strings evaluate to True
.
在您的代码中,您首先将所有以R开头的名称收集到列表中,然后将此列表传递给所有名称。执行此操作将始终返回True,因为非空字符串计算为True。
Instead, you should write:
相反,你应该写:
all(name[0] == "R" for name in names)
This will pass an iterable of booleans to all
. If all of them are True
, the function will return True
; otherwise, it will return False
.
这会将一个可迭代的布尔值传递给所有人。如果所有这些都是True,则该函数将返回True;否则,它将返回False。
As an added bonus, the result will now be computed lazily because we used a generator expression instead of a list comprehension. With the list comprehension, the code needed to test all strings before determining a result. The new code however will only check as many as necessary.
作为额外的奖励,现在结果将被懒惰地计算,因为我们使用了生成器表达式而不是列表推导。使用列表理解,在确定结果之前测试所有字符串所需的代码。但是,新代码只会根据需要进行检查。
#2
names = ["Rhonda", "Ryan", "Red Rackham", "Paul"]
print all(map(lambda name: name[0] == "R", names))
# prints False
names = ["Rhonda", "Ryan", "Red Rackham"]
print all(map(lambda name: name[0] == "R", names))
# prints True
#3
names = ["Rhonda", "Ryan", "Red Rackham", "Paul"]
if all(c[0] == "R" for c in names):
print "ALL MATCH"
Demo:
#4
The reason why you were getting wrong result was because you are already creating a new list using list comprehension by applying the desired condition, So if we do a little breakdown then:
你得到错误结果的原因是因为你已经通过应用所需条件使用列表理解创建了一个新列表,所以如果我们做了一点细分那么:
>>> print [name for name in names if name[0] == "R"]
>>> ['Rhonda', 'Ryan', 'Red Rackham']
>>> print all(['Rhonda', 'Ryan', 'Red Rackham'])
>>> True
So the correct way may be:
所以正确的方法可能是:
names = ["Rhonda", "Ryan", "Red Rackham", "Paul"]
res = all(map(lambda x : x[0]=="R", names))
# map() returns: [True, True, True, False]
# all([True, True, True, False]) == False
print res