如何使for循环保持返回值?
How to make a for loop keep returning values?
我必须设置两个循环。一个保持列表中负数总数的总和,并且是 returned。另一个将底片附加到一个单独的列表中也被 returned。我只能 return 每个循环中的第一个值。
# Given the parameter list, returns the count of negative numbers in the list
def num_negatives(list):
negatives = 0
for neg in list:
if neg < 0:
negatives += 1
return negatives
# Given the parameter list, returns a list that contains only the negative numbers in the parameter list
def negatives(list):
negList = []
for negItems in list:
if negItems < 0:
negList.append(negItems)
return negList
# Prompts the user to enter a list of numbers and store them in a list
list = [float(x) for x in input('Enter some numbers separated by whitespace ').split()]
print()
# Output the number of negatives in the list
print('The number of negatives in the list is', num_negatives(list))
print()
# output the list of negatives numbers
print('The negatives in the list are ', end = '')
for items in negatives(list):
print(items, ' ', sep = '', end = '')
print('\n')
如果我在程序开始时输入值 1 2 3 4 5 -6 7 -8 9 -12.7
,我应该会收到这个。
The number of negatives in the list is 3
The negatives in the list are -6.0 -8.0 -12.7
相反,我只得到:
The number of negatives in the list is 1
The negatives in the list are -6.0
您的 return 放在了错误的位置。您不想 return 多次。您只想在函数完成后 return 最终值 一次 运行。所以你的函数应该是这样的:
# Find number of negative numbers in the list
def num_negatives(list):
negatives = 0
for neg in list:
if neg < 0:
negatives += 1
return negatives
def negatives(list):
negList = []
for negItems in list:
if negItems < 0:
negList.append(negItems)
return negList
我认为您正在搜索 yield
,这是创建可迭代对象的优雅解决方案。
def num_negatives(list):
negatives = 0
for neg in list:
if neg < 0:
yield neg # I return the negative number directly.
def negatives(list):
negList = []
negatives_generator = num_negatives(list)
for negItem in negatives_generator:
negList.append(negItem)
return negList
然后你终于可以打印否定列表和len(negatives)
,你现在都有了。
更多详情:What does yield do?
我必须设置两个循环。一个保持列表中负数总数的总和,并且是 returned。另一个将底片附加到一个单独的列表中也被 returned。我只能 return 每个循环中的第一个值。
# Given the parameter list, returns the count of negative numbers in the list
def num_negatives(list):
negatives = 0
for neg in list:
if neg < 0:
negatives += 1
return negatives
# Given the parameter list, returns a list that contains only the negative numbers in the parameter list
def negatives(list):
negList = []
for negItems in list:
if negItems < 0:
negList.append(negItems)
return negList
# Prompts the user to enter a list of numbers and store them in a list
list = [float(x) for x in input('Enter some numbers separated by whitespace ').split()]
print()
# Output the number of negatives in the list
print('The number of negatives in the list is', num_negatives(list))
print()
# output the list of negatives numbers
print('The negatives in the list are ', end = '')
for items in negatives(list):
print(items, ' ', sep = '', end = '')
print('\n')
如果我在程序开始时输入值 1 2 3 4 5 -6 7 -8 9 -12.7
,我应该会收到这个。
The number of negatives in the list is 3
The negatives in the list are -6.0 -8.0 -12.7
相反,我只得到:
The number of negatives in the list is 1
The negatives in the list are -6.0
您的 return 放在了错误的位置。您不想 return 多次。您只想在函数完成后 return 最终值 一次 运行。所以你的函数应该是这样的:
# Find number of negative numbers in the list
def num_negatives(list):
negatives = 0
for neg in list:
if neg < 0:
negatives += 1
return negatives
def negatives(list):
negList = []
for negItems in list:
if negItems < 0:
negList.append(negItems)
return negList
我认为您正在搜索 yield
,这是创建可迭代对象的优雅解决方案。
def num_negatives(list):
negatives = 0
for neg in list:
if neg < 0:
yield neg # I return the negative number directly.
def negatives(list):
negList = []
negatives_generator = num_negatives(list)
for negItem in negatives_generator:
negList.append(negItem)
return negList
然后你终于可以打印否定列表和len(negatives)
,你现在都有了。
更多详情:What does yield do?