2013-03-08 68 views
0

我正在嘗試編寫一個名爲splitList(myList, option)的函數,該函數以01作爲參數列表和選項。如果該選項的值爲0該函數返回myList是陰性,如果選項的值是1該函數返回myList是,即使由元素的列表組成元素的列表(我們認爲0〜是一個偶數,因爲它可以被2)整除。可以被2整除:Python

例如:

splitList([1,-3,5,7,-9,-11,0,2,-4], 0) 

將返回列表:

[-3,-9,-11,-4] 

凡爲:

splitList([1,-3,5,7,-9,-11,0,2,-4], 1) 

將返回列表:

[0,2,-4] 

對於這個問題我必須使用for loop

以下是我有:

def splitList(myList, option): 
    negativeValues = [] 
    positiveValues = [] 
    evenValues = [] 
    for i in range(0,len(myList)):  
     if myList[i] < 0: 
      negativeValues.append(myList [i]) 
     else: 
      positiveValues.append(myList [i]) 

    for element in myList: 
     if option == 1: 
      myList [i] % 2 == 0 
      evenValues.append(myList [i]) 
      return evenValues 
     else: 
      return negativeValues 

我不能讓它做的唯一一件事就是爲排序列表,並返回所有的整除2

+0

看到切片功能。這就是我要說的。 – Tom 2013-03-08 18:11:54

+3

@Tom:'slice'函數將如何提供幫助?我的意思是,如果你能說更多的話。 – hughdbrown 2013-03-08 18:41:03

+0

@hughdbrown沒關係,誤解了這個問題。 – Tom 2013-03-08 19:11:16

回答

0

我相信這是你在哪裏試圖實現:

def splitList(myList,option): 

    result = [] 

    if option == 0: 
     for item in myList: 
      if (item < 0): 
       result.append(item) 
    elif option == 1: 
     for item in myList: 
      if (item % 2 == 0): 
       result.append(item) 
    else: 
     return "Option Error" 

    return sorted(result) 

print splitList([1,-3,5,7,-9,-11,0,2,-4], 0) 
print splitList([1,-3,5,7,-9,-11,0,2,-4], 1) 
print splitList([1,-3,5,7,-9,-11,0,2,-4], 2) 

輸出:

[-11, -9, -4, -3] 
[-4, 0, 2] 
Option Error 
+0

這對我有效。你會如何轉換並使用「while循環」? – user2149455 2013-03-08 18:31:18

+0

'while循環'在這裏不適合。 – 2013-03-08 18:37:12

+0

如何寫一個while循環與給定的情況? – user2149455 2013-03-08 18:52:16

4

使用循環號碼就在這裏,因爲有一個標準功能filter,你想要做什麼有點多餘:返回與匹配給定謂語其中一個列表,這些元素的新名單。

讓我們首先定義謂詞:

def is_even(x): 
    return x % 2 == 0 

def is_negative(x): 
    return x < 0 

然後你就可以在filter方面很容易地定義你的函數:

def splitList(myList, option): 
    predicate = is_negative if option == 0 else is_even 
    return filter(predicate, myList) 
+0

作爲練習,我建議你自己編寫'filter'函數。 – Kos 2013-03-08 18:14:27

+2

你也可以使用'lambdas'使它更短。我個人更喜歡一個如果有一個額外的變量有兩個返回語句,但這是一個品味問題。一個要求是使用'for',否則我會看到代碼中的美... – ted 2013-03-08 18:16:13

+2

+1我喜歡你的答案,但是你對此做了什麼:「對於這個問題,你必須使用」for循環」。」也許:'如果謂詞(x)'返回[x for myList]' – hughdbrown 2013-03-08 18:24:37

1

你返回太快。你首先必須完成foor循環,然後返回,而不是從循環內部返回。

for i in range(5): 
    print i 
    numbers.append(i) 
    return numbers //wrong: exit the function on the first pass in the loop. 

for i in range(5): 
    print i 
    numbers.append(i) 

return numbers  //right 

除此之外爲什麼你計算負valuse列表,如果你不需要它?

2

您可以從這些簡單的構建所有的變種:

def even_list(numbers): 
    return [x for x in numbers if not (x & 1)] 

def odd_list(numbers): 
    return [x for x in numbers if x & 1] 

def negative_list(numbers): 
    return [x for x in numbers if x < 0] 

def positive_list(numbers): 
    return [x for x in numbers if x > 0] 

然後測試:

>>> def test(): 
...  numbers = list(range(-3, 4)) 
...  print even_list(numbers) 
...  print odd_list(numbers) 
...  print positive_list(numbers) 
...  print negative_list(numbers) 
... 
>>> test() 
[-2, 0, 2] 
[-3, -1, 1, 3] 
[1, 2, 3] 
[-3, -2, -1] 

後來:所以從@Kos偷,你可以寫split_list這樣的:

def split_list(myList, option): 
    predicate = negative_list if not option else even_list 
    return predicate(myList) 

或者:

如果
def split_list(myList, option): 
    predicates = [negative_list, even_list] 
    return predicates[option](myList) 

不知道它滿足您的需求,如果for-loop在一個被調用函數中的列表理解。

Also:「函數名稱應該是小寫,爲了提高可讀性,必須使用由下劃線分隔的單詞。」

+0

+1列表理解 – ted 2013-03-08 18:21:57

+2

沒看到這個:'對於這個問題,你必須使用「for循環」。呸騙子。 – hughdbrown 2013-03-08 18:23:22

+1

真正的列表理解在技術上並不是一個可以爭論的for循環,但我認爲很高興看到這個選項,並且我將它算得足夠接近,你基本上已經改變了順序。 – ted 2013-03-08 18:25:32

1
def splitList(myList,option): 

    negative_numbers = [i for i in myList if i < 0] 
    even_numbers = [i for i in myList if i % 2 == 0] 

    return sorted(even_numbers) if option else sorted(negative_numbers) 
+0

'odds = [i for myList if i%2 == 0] ' - 代碼與變量不匹配。'i%2 == 0'不會找到奇數。 – hughdbrown 2013-03-08 18:30:13

+0

@hughdbrown你是對的,錯誤的命名。 – jurgenreza 2013-03-08 18:33:44