如何检查列表参数是否为None [英] How to check if a List Argument is None
问题描述
我正在做此问题设置,并且当列表为None时,我无法通过测试用例.
我已经对几乎所有情况进行了测试,并且我注意到其他用户使用了与我的代码非常相似的代码来实现正确的目标,我需要调试和检查条件语句中是否缺少测试用例时需要帮助./p>
返回否定的测试用例正在测试以查看List是否为None,我通过在Python解释器中进行测试发现该列表不一定表示为空.
以下是代码:
def喜欢(名称):为我的名字:如果不是名字:返回没有人喜欢"elif len(名称)< = 0:返回没有人喜欢"elif len(名称)== 1:文字="{}喜欢这个"返回text.format(names [0])elif名称==无:返回没有人喜欢"Elif len(名称)>2并且len(names)< = 4和len(names)!= 3:附加= len(名称)-2文字="{},{}和{}其他类似的人"返回text.format(名称[0],名称[1],其他)Elif len(名称)>2和len(names)>4和len(names)!= 3:附加= len(名称)-2文字="{},{}和{}其他类似的人"返回text.format(名称[0],名称[1],其他)elif len(名称)== 2:文字="{}和{}像这样"返回text.format(名称[0],名称[1])Elif len(名称)== 3:文字="{},{}和{}像这样"返回text.format(名称[0],名称[1],名称[2])别的:文字="{}喜欢这个"返回text.format(names [0])
主要问题是,如果 None
是可能的参数,则 for
循环将崩溃.如注释中所述,此循环无论如何都与逻辑无关,因为它仅在第一次迭代时返回.我们只对列表长度(如果存在)的属性感兴趣,而不对列表元素感兴趣,因此删除循环.
话虽如此,这里的逻辑非常混乱,并且有许多冗余分支.具有3个或4个以上分支或嵌套大于几个级别的条件很难推理,应该将其消除.
-
分支
elif len(names)>2和len(names)>4和len(names)!= 3:
可以简化为
elif len(names)>4
,因为如果len(names)
大于4,我们肯定知道它肯定是!= 3
和>2
. -
分支
if not names:
涵盖了names为None
以及len(names)< = 0
(<
在这里是不必要的),因此我们可以抛出另外两个分支. -
分支
elif len(names)>2并且len(names)< = 4和len(names)!= 3:
与
elif len(names)== 4
相同.
退后一步,只有5种情况:
- 我们没有名字
- 我们有1个名字
- 我们有2个名字
- 我们有3个名字
- 我们有4个或更多名称
这里是一个重写,以清楚地表达这一点:
def喜欢(名称):如果不是名字:返回没有人喜欢"elif len(名称)== 1:返回f"{names [0]}喜欢此帖"elif len(名称)== 2:返回f"{names [0]}和{names [1]}这样"Elif len(名称)== 3:返回f"{names [0]},{names [1]}和{names [2]}这样"返回f"{names [0]},{names [1]}和{len(names-2}个其他类似的人"如果__name__ =="__main__":名称= ["alice","bob","carol","doug","eric","fred"]print("None =>",likes(None))对于范围内的我(len(names)):print(names [:i],"=>",点赞(names [:i]))
输出:
None =>没有人喜欢这个[] =>没有人喜欢这个['alice'] =>爱丽丝赞了['alice','bob'] =>爱丽丝和鲍勃这样['alice','bob','carol'] =>爱丽丝,鲍勃和卡罗尔这样的['alice','bob','carol','doug'] =>爱丽丝,鲍勃和另外2个这样的人['alice','bob','carol','doug','eric'] =>爱丽丝,鲍勃和另外3个这样的人
I am doing this problem set, and I cannot get passed the Test Case for when the List is None.
I have tested for almost all the cases, and I have noticed that other users got it right with a code that is very similar to mine, I need help debugging and checking if I am missing a test case in my Conditionals.
The test cases that come back negative are testing to see if the List is None, which I found out by testing in the Python interpreter that it does not necessarily imply empty.
Here is the Code:
def likes(names):
for i in names:
if not names:
return "no one likes this"
elif len(names) <= 0:
return "no one likes this"
elif len(names) == 1:
text = "{} likes this"
return text.format(names[0])
elif names == None:
return "no one likes this"
elif len(names) > 2 and len(names) <= 4 and len(names) != 3:
additional = len(names) - 2
text = "{}, {} and {} others like this"
return text.format(names[0], names[1], additional)
elif len(names) > 2 and len(names) > 4 and len(names) != 3:
additional = len(names) - 2
text = "{}, {} and {} others like this"
return text.format(names[0], names[1], additional)
elif len(names) == 2:
text = "{} and {} like this"
return text.format(names[0], names[1])
elif len(names) == 3:
text = "{}, {} and {} like this"
return text.format(names[0], names[1], names[2])
else:
text = "{} likes this"
return text.format(names[0])
The main issue is that if None
is a possible argument, the for
loop will crash. As mentioned in the comments, this loop isn't relevant to the logic anyway because it simply returns on the first iteration. We're only interested in properties about the length of the list (if it exists), not its elements, so remove the loop.
Having said that, the logic here is extremely confusing and there are many redundant branches. Conditionals that have more than 3 or 4 branches or nesting greater than a couple levels are difficult to reason about and should be eliminated.
The branch
elif len(names) > 2 and len(names) > 4 and len(names) != 3:
can be simplified to
elif len(names) > 4
because iflen(names)
is greater than 4, we know for sure it's definitely!= 3
and> 2
.The branch
if not names:
covers casesnames is None
as well aslen(names) <= 0
(the<
is unnecessary here), so we can toss out two more branches.The branch
elif len(names) > 2 and len(names) <= 4 and len(names) != 3:
is the same as
elif len(names) == 4
.
Taking a step back, there are only 5 cases:
- we have no names
- we have 1 name
- we have 2 names
- we have 3 names
- we have 4 or more names
Here's a re-write to express this clearly:
def likes(names):
if not names:
return "no one likes this"
elif len(names) == 1:
return f"{names[0]} likes this"
elif len(names) == 2:
return f"{names[0]} and {names[1]} like this"
elif len(names) == 3:
return f"{names[0]}, {names[1]} and {names[2]} like this"
return f"{names[0]}, {names[1]} and {len(names) - 2} others like this"
if __name__ == "__main__":
names = ["alice", "bob", "carol", "doug", "eric", "fred"]
print("None =>", likes(None))
for i in range(len(names)):
print(names[:i], "=>", likes(names[:i]))
Output:
None => no one likes this
[] => no one likes this
['alice'] => alice likes this
['alice', 'bob'] => alice and bob like this
['alice', 'bob', 'carol'] => alice, bob and carol like this
['alice', 'bob', 'carol', 'doug'] => alice, bob and 2 others like this
['alice', 'bob', 'carol', 'doug', 'eric'] => alice, bob and 3 others like this
这篇关于如何检查列表参数是否为None的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!