简体   繁体   English

创建一个 function 以列表作为参数并仅保留大于 5 的数字

[英]Create a function which takes a list as an argument and keeps only the numbers that are greater than 5

I am trying to create a function that takes a list as an argument and keeps only the numbers that are greater than 5 by creating a new empty list inside the function and only append it with numbers that are greater than five, looping over the given one.我正在尝试创建一个 function ,它以一个列表作为参数,并通过在 function 中创建一个新的空列表来仅保留大于 5 的数字,并且只有 append 它的数字大于五,循环遍历给定的一个. However, append does not return anything or it returns None.但是,append 不返回任何内容或返回 None。

lst = [[0, 1, 5, 6, 9], [5, 5, 5, 5], [42, 1337], [-100, 100]]

def more_than_five(lst):
  result = []
  for i in range(len(lst)):
    if i > 5:
      result.append(i)
  return result


Output should be (individually. not as one list):

[6,9]
[]
[42, 1337]
[100]

You can use a double comprehension to iterate over a list of lists:您可以使用双重理解来遍历列表列表:

def more_than_five(lst):
    return [[i for i in l if i > 5] for l in lst]
print(more_than_five(lst))

# Output
[[6, 9], [], [42, 1337], [100]]

Without a comprehension:没有理解:

def more_than_five(lst):
    result = []
    for j, l in enumerate(lst):
        result.append([])
        for i in l:
            if i > 5:
                result[j].append(i)
    return result
print(more_than_five(lst))

# Output
[[6, 9], [], [42, 1337], [100]]

Update : If your function only processes one list at a time:更新:如果您的 function 一次只处理一个列表:

def more_than_five(lst):
    return [i for i in lst if i > 5]

print(more_than_five(lst[0]))
print(more_than_five(lst[1]))
# and so on

Or:或者:

def more_than_five(lst):
    result = []
    for i in lst:
        if i > 5:
            result.append(i)
    return result

print(more_than_five(lst[0]))
print(more_than_five(lst[1]))
# and so on

Output: Output:

[6, 9]
[]
[42, 1337]
[100]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 编写一个 function,称为 filter_out(),它以整数列表作为输入,返回仅保留数字过滤的列表 - Write a function, called filter_out(), which takes a list of integers as input and return the list filtered by only keeping numbers 如何在 Python 中创建一个接受数字和整数列表的函数? - How can I create a function in Python that takes a list of numbers and an integer? 列表中大于 x 的数字序列的长度 - Length of sequences of numbers greater than x in a list 仅将参数传递给 function 如果需要该参数 Python - Only pass an argument to a function if it takes that argument Python 如何使输出仅是列表中大于 x 的数字? - How do I get the output to only be numbers within a list that are greater than x? 遗失:定义一个函数,该函数接受一个数字元组,并仅返回该元组中偶数的和 - Lost: Defining a function which takes a tuple of numbers and returns the sum of only the even numbers in the tuple 如何使用带有多个参数的键函数进行排序? - How to sort using key function which takes more than one argument? 无法创建线程,因为只有当我给它一个长度超过 1 的字符串时,“需要 1 个位置参数,但给出 4 个” - Can't create thread because "Takes 1 positional argument but 4 given" only when I give it a string longer than 1 使用将列表作为参数的 function 修改全局列表的正确方法是什么? - What is the right way to modify a global list using a function which takes the list as an argument? 为什么限制函数仅将int作为参数? - Why limit function takes only int as argument?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM