如何检查列表参数是否为None

时间:2019-12-05 19:19:54

标签: python algorithm

我正在做此问题设置,并且当列表为None时,我无法通过测试用例。

我已经对几乎所有情况进行了测试,并且我注意到其他用户使用了与我的代码非常相似的代码来实现它,我需要调试和检查我的条件语句中是否缺少测试用例。 / p>

返回否定的测试用例正在测试以查看List是否为None,我通过在Python解释器中进行测试发现该列表不一定表示为空。

以下是代码:

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])

3 个答案:

答案 0 :(得分:0)

感谢@JohnGordon,我只需要删除条件语句周围的for循环,就可以了

def likes(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])

答案 1 :(得分:0)

我不确定您要使用代码中的for i in names:行做什么。我认为您正在尝试遍历names的列表。如果是这种情况,我会将行更改为for name in names:

请记住,list = Nonelist = []不同。 list = None时,列表未定义。另一方面,list = []表示一个空列表。

这是一个可能有用的示例。

def checkListVal(valList):
# Checks to see if valList stores some data
if valList == None:
    print("valList is undefined")
    # Returns out of this function
    return

# Checks to see if the list is empty
if len(valList) == 0:
    print("valList is empty")
    return

for val in valList:
    if val == "val1":
        print("val1 in valList")
    elif val == "val2":
        print("val2 in valList")
    else:
        print("val equals " + val)

valList1 = ["val1", "val2", "val3"]
valList2 = []
valList3 = None

checkListVal(valList1)
print()

checkListVal(valList2)
print()

checkListVal(valList3)

输出如下:

val1 in valList
val2 in valList
val equals val3

valList is empty

valList is undefined

答案 2 :(得分:0)

主要问题是,如果None是一个可能的参数,则for循环将崩溃。如注释中所述,该循环无论如何都与逻辑无关,因为它仅在第一次迭代时返回。我们只对列表长度(如果存在)的属性感兴趣,而不对列表元素感兴趣,因此删除循环。

话虽如此,这里的逻辑非常混乱,并且有许多冗余分支。具有3个或4个以上分支或嵌套大于几个级别的条件很难推理,应予以消除。

  • 分支

    elif len(names) > 2 and len(names) > 4 and len(names) != 3:
    

    可以简化为elif len(names) > 4,因为如果len(names)大于4,我们可以确定它肯定是!= 3> 2

  • 分支if not names:涵盖了案例names is Nonelen(names) <= 0(这里不需要<),因此我们可以抛出另外两个分支。 / p>

  • 分支

    elif len(names) > 2 and len(names) <= 4 and len(names) != 3:
    

    elif len(names) == 4相同。

退后一步,只有5种情况:

  1. 我们没有名字
  2. 我们有1个名字
  3. 我们有2个名字
  4. 我们有3个名字
  5. 我们有4个或更多名称

这里是重新编写以清楚地表达这一点:

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]))

输出:

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