Python Homework - creating a new list

前端 未结 6 894
闹比i
闹比i 2021-01-22 19:36

The assignment:

Write a function called splitList(myList, option) that takes, as input, a list and an option, which is either 0 or 1. If the

6条回答
  •  臣服心动
    2021-01-22 19:59

    def splitList(myList, option):
        return [i for i in myList if i<0] if option==0 else [i for i in myList if i>0]
    
    
    # test
    >>> myList=[2, 3, -1, 4, -7]
    >>> splitList(myList, 0)
    [-1, -7]
    >>> splitList(myList, 1)
    [2, 3, 4]
    >>> 
    

提交回复
热议问题