小编use*_*743的帖子

Python:从数字列表中删除底片

问题是从数字中删除负数.

remove_negs([1, 2, 3, -3, 6, -1, -3, 1])被执行,其结果是:[1, 2, 3, 6, -3, 1].结果假设是[1, 2, 3, 6, 3, 1].发生的事情是,如果一行中有两个负数(例如-1, -3),那么第二个数字将不会被删除.def main():numbers = input("输入数字列表:")remove_negs(数字)

def remove_negs(num_list): 
  '''Remove the negative numbers from the list num_list.'''
    for item in num_list: 
        if item < 0: 
           num_list.remove(item) 

    print num_list

main()
Run Code Online (Sandbox Code Playgroud)

python list negative-number

6
推荐指数
3
解决办法
2万
查看次数

标签 统计

list ×1

negative-number ×1

python ×1