Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用我已有的返回列表中的第一个正数(如果可能)_Python_Python 3.x - Fatal编程技术网

Python 使用我已有的返回列表中的第一个正数(如果可能)

Python 使用我已有的返回列表中的第一个正数(如果可能),python,python-3.x,Python,Python 3.x,我有一些python练习函数,我正试图弄清楚,但在使用以下函数时遇到了问题: def find_positive(lst): '''(lst) -> num Return the index of the first occurrence of a positive number in the list of numbers lst. Return None if the list does not have any positive numbers >>>

我有一些python练习函数,我正试图弄清楚,但在使用以下函数时遇到了问题:

def find_positive(lst):
 '''(lst) -> num
 Return the index of the first occurrence of a positive number in 
 the list of numbers lst. Return None if the list does not 
 have any positive numbers
 >>>find_positive([-10, -4, -2, 8, 3, 5, -4])
 8
 >>>find_positive([-2, -4, -9, 0])
 None
 >>>find_positive([5, 3, 9])
 5
 '''
我想也许我可以制作一个只有正数的新列表,然后返回列表中的第一个数字(即索引0)。我可以通过以下方法实现这一点:

  newL = lst[:]
for item in lst:
    if item <= 0:
        newL.remove(item)
return newL[0]
newL=lst[:]
对于lst中的项目:

如果项目我将对其进行评论,以便您能够理解:

def find_positive(lst):
    for index, item in enumerate(lst): # Loop that tracks index and item value
        if item > 0: # If the item is bigger than 0...
            return index # Return the index of that item
    return None # Returns None otherwise
说明有点不清楚,好像您需要返回第一个正项的索引或值。如果要返回值而不是索引,则应更改
found=index
,并将其设置为
found=item
。以下是它的文档及其工作原理

评论后更新
这是一个例子,说明了“不返回”是多么有用

my_variable_name = find_positive([-4, -2, 0, 1, 2, 3, 4]) # Call the function
if my_variable_name is None: # Check if None was returned
    print("hey, no positives found")
else:
    print("I found {} as first positive".format(my_variable_name))

在询问之前,
{}
是用于格式化字符串的占位符。这样,我们就可以构建比字符串插入中间的代码更多的可读字符串。

< p>我会对此进行注释,以便您能够理解:

def find_positive(lst):
    for index, item in enumerate(lst): # Loop that tracks index and item value
        if item > 0: # If the item is bigger than 0...
            return index # Return the index of that item
    return None # Returns None otherwise
说明有点不清楚,好像您需要返回第一个正项的索引或值。如果要返回值而不是索引,则应更改
found=index
,并将其设置为
found=item
。以下是它的文档及其工作原理

评论后更新
这是一个例子,说明了“不返回”是多么有用

my_variable_name = find_positive([-4, -2, 0, 1, 2, 3, 4]) # Call the function
if my_variable_name is None: # Check if None was returned
    print("hey, no positives found")
else:
    print("I found {} as first positive".format(my_variable_name))

在询问之前,
{}
是用于格式化字符串的占位符。这样,我们就可以构建比字符串插入中间的代码更多的可读字符串。

不知道为什么你会把它变得这么复杂。

def find_positive(lst):
    for x in lst:
         if x > 0:
             return x

不知道你为什么把事情弄得这么复杂

def find_positive(lst):
    for x in lst:
         if x > 0:
             return x


问题是它应该返回第一个正数的索引,而不是数字本身。你确定你解决了正确的问题吗?对于
[-5,-3,6,4]
它应该返回
2
。你为什么不回答汤姆的问题?非常感谢,我甚至都没意识到。幸运的是,人们对解决这两个问题都发表了评论,所以现在我将知道如何解决这两个问题。谢谢@如果你的问题得到了回答,请回答。谢谢。问题是它应该返回第一个正数的索引,而不是数字本身。你确定你解决了正确的问题吗?对于
[-5,-3,6,4]
它应该返回
2
。你为什么不回答汤姆的问题?非常感谢,我甚至都没意识到。幸运的是,人们对解决这两个问题都发表了评论,所以现在我将知道如何解决这两个问题。谢谢@如果你的问题得到了回答,请回答。谢谢。你正在返回最后一个负数的索引。请重试。正数大于零。您还应该在看到索引后立即返回它。按原样,您返回的是最后一个负数的索引,无需中断,
如果项>0:返回索引
Hm,yes@Reti43,代码可以这样改进。将更新它。谢谢Saelyth和其他人!我并没有在课堂上学习过枚举,但我会在你们提供的链接上了解它,希望能找到答案。没有人做什么?我以为它应该返回字符串None,但我意识到它不是,因为它不在doc字符串的字符串引号中-它只是返回nothing吗?并且返回最后一个负数的索引。请重试。正数大于零。您还应该在看到索引后立即返回它。按原样,您返回的是最后一个负数的索引,无需中断,
如果项>0:返回索引
Hm,yes@Reti43,代码可以这样改进。将更新它。谢谢Saelyth和其他人!我并没有在课堂上学习过枚举,但我会在你们提供的链接上了解它,希望能找到答案。没有人做什么?我以为它应该返回字符串None,但我意识到它不是,因为它不在文档字符串的字符串引号中-它只是什么也不返回吗?