【问题标题】:How can I write a function that returns all negative numbers as well as checks if the input are only integers and floating point numbers? [duplicate]如何编写一个返回所有负数并检查输入是否仅为整数和浮点数的函数? [复制]
【发布时间】:2022-02-21 07:29:50
【问题描述】:

编写一个函数 find_negatives,它接受一个数字列表 l 作为参数,并返回 l 中所有负数的列表。如果 l 中没有负数,则返回一个空列表。

def find_negatives(l):
    l_tmp = []
    for num in l:
        if num < 0:
            l_tmp.append(num)
    return l_tmp

#Examples
find_negatives([8, -3.5, 0, 2, -2.7, -1.9, 0.0])     # Expected output: [-3.5, -2.7, -1.9]
find_negatives([0, 1, 2, 3, 4, 5])                   # Expected output: []

这是我在下面遇到问题的部分:

编写一个函数 find_negatives2,其工作原理与 find_negatives 相同,并添加以下两个内容:

如果 l 不是列表,则返回字符串“无效的参数类型!” 如果 l 中的任何元素既不是整数也不是浮点数,则返回字符串“Invalid parameter value!”

我目前所拥有的

def find_negatives2(l):
    l_tmp1 = []
    if type(l) != list:
        return "Invalid parameter type!"
    else:
       for num in l:

Examples:
find_negatives2([8, -3.5, 0, 2, -2.7, -1.9, 0.0])      # Expected output: [-3.5, -2.7, -1.9]
find_negatives2([0, 1, 2, 3, 4, 5])                    # Expected output: []
find_negatives2(-8)                                    # Expected output: 'Invalid parameter type!'
find_negatives2({8, -3.5, 0, 2, -2.7, -1.9, 0.0})      # Expected output: 'Invalid parameter type!'
find_negatives2([8, -3.5, 0, 2, "-2.7", -1.9, 0.0])    # Expected output: 'Invalid parameter value!'
find_negatives2([8, -3.5, 0, 2, [-2.7], -1.9, 0.0])    # Expected output: 'Invalid parameter value!'

我不确定如何继续。我不确定如何检查列表中的每种类型

【问题讨论】:

  • 有什么问题?是有错误还是不知道如何处理?
  • 我不知道该怎么做。我不确定如何检查列表中的每种类型

标签: python for-loop


【解决方案1】:

你在正确的轨道上;你只需要为类型比较做一个循环:

# (...)
else:
    for num in l:
        if type(num) not in (int, float):
            return "Invalid parameter type!"
        # (...)

【讨论】:

    【解决方案2】:

    你可以试试下面的代码:

    def find_negatives2(l):
        l_tmp1 = []
        if not isinstance(l, list):
            return "Invalid parameter type!"
        else:
           for num in l:
             if not (isinstance(num, float) or isinstance(num, int)):
               return "Invalid parameter value!"
             else:
               if num < 0:
                 l_tmp1.append(num)
        return l_tmp1
    assert find_negatives2([8, -3.5, 0, 2, -2.7, -1.9, 0.0])      == [-3.5, -2.7, -1.9]
    assert find_negatives2([0, 1, 2, 3, 4, 5])                    == []
    assert find_negatives2(-8)                                    == 'Invalid parameter type!'
    assert find_negatives2({8, -3.5, 0, 2, -2.7, -1.9, 0.0})      == 'Invalid parameter type!'
    assert find_negatives2([8, -3.5, 0, 2, "-2.7", -1.9, 0.0])    == 'Invalid parameter value!'
    assert find_negatives2([8, -3.5, 0, 2, [-2.7], -1.9, 0.0])    == 'Invalid parameter value!'
    

    所有断言都将被验证。

    说明

    isinstance 检查它的第一个参数是否是第二个参数的实例。使用此功能,您可以检查任何变量类型。请注意,使用type(l) != list 不是检查变量类型的好方法。如果您有兴趣了解原因,link 可能会对您有所帮助。

    【讨论】:

    • 这行得通,但我有一个问题。 check = false 有什么作用?
    • @PythonNewb 对不起,我在以前的答案版本中使用了它,忘记删除它。我已根据您的评论编辑了答案。
    【解决方案3】:

    您可以通过以下方式实现您的目标:

    def find_negatives(l):
        if type(l) != list or any(str(x).isnumeric() for x in l) == False:
            return 'Invalid parameter type!'
        l_tmp = []
        for num in l:
            if num < 0:
                l_tmp.append(num)
        return l_tmp
    

    您所要做的就是检查输入是否为列表,以及输入的任何元素是否不是数字。

    【讨论】:

    • hmmm... 将值转换为文本,然后检查它是否会生成有效数字,这对我来说听起来不是什么大问题。此外,根据问题陈述,字符串“-1”不应该是一个有效数字,因为它是一个字符串。
    • any(str(x).isnumeric() for x in l) 等于 False 如果您检查的字符串中有 none 是数字。您应该检查 if not all 其中的数字(如果您想这样做的话)。
    猜你喜欢
    • 2020-11-26
    • 2013-02-25
    • 1970-01-01
    • 1970-01-01
    • 2011-08-07
    • 2020-05-05
    • 2011-08-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多