2016-10-16 21 views
0

我開始告訴你這是爲了學校,因爲我正在學習使用Python進行編碼。請解釋爲什麼我應該做點什麼:)!我期待着學習不只是得到答案。刪除負值並打印原始和新列表

我想擺脫列表中的負項。我想打印清單之前(包括負項目)和之後(當然沒有負項目)。 我的問題是,它打印出原來的列表和新的列表,沒有否定項目前的打印和原來的一個後。 像這樣:

Before: [2, 7, -3, -3, 13, -14, 13, 5, 11, -4, 10, 5, 0, -5, -14, 
-2, -9, -14, 2, -10, -5, 8, 7] 
[2, 7, 13, 13, 5, 11, 10, 5, 0, 2, 8, 7] 
After: [2, 7, -3, -3, 13, -14, 13, 5, 11, -4, 10, 5, 0, -5, -14, -2, -9, 
-14, 2, -10, -5, 8, 7] 

這是我做了什麼,我只是似乎無法找出我應該做的......

import random 

def removeNegatives(listOfIntegers): 
    l = listOfIntegers[:]   #takes a copy of the list 
    for item in listOfIntegers:  
     if item < 0:    #checks if it is lower than 0 
      l.remove(item) 
    print l 



l = [] 
for i in xrange(0, random.randint(15,25)): #gives me the random numbers 
    l.append(random.randint(-15,15)) 

print "Before:", l #should only print out the original list of numbers 
removeNegatives(l) 
print "After:", l #should only print out the new list without the numbers that are <0 
+2

由於您沒有限制使用額外的列表,爲什麼不通過列表並只添加元素到新列表,如果他們是積極的? 'newList = [ele for ele在listOfIntegers如果ele> = 0]' –

+0

您不修改函數內部的全局'l'。你正在創建一個局部變量'l'並在那個變量上工作。和你的'打印'之後:「,l'然後再次打印出全局變量,它沒有被修改 – UnholySheep

回答

1

您沒有修改全局變量l在你的功能。

我提出這個代碼在Python,這應該正常工作:

import random 

def removeNegatives(listOfIntegers): 
    return [x for x in listOfIntegers if not x < 0] 

l = [] 
for i in xrange(0, random.randint(15,25)): #gives me the random numbers 
    l.append(random.randint(-15,15)) 

print "Before:", l #should only print out the original list of numbers 
l = removeNegatives(l) 
print "After:", l #should only print out the new list without the numbers that are <0 

它的方式更短。你怎麼看待這件事?

+0

我很抱歉。應該注意到,我不能改變任何東西在l = []和向下.... – benitso

+1

'不是x <0'?爲什麼不'x> = 0'? – UnholySheep

+0

@benitso在這種情況下,對你的問題最簡單的解決方法是將'global l'作爲你的'removeNegatives'函數的第一行(在該函數的其他部分之前)。我通常不會推薦,但它會使你的代碼工作 – UnholySheep

0

剛纔看到評論相對你不能夠修改L以下代碼= []

在這種情況下,你需要重新分配給listOfIntegers出來的功能

def removeNegatives(listOfIntegers): 
    global l 
    k = listOfIntegers[:]   #takes a copy of the list 
    for item in listOfIntegers:  
     if item < 0:    #checks if it is lower than 0 
      k.remove(item) 
    print k 
    l = k 

你讓當你進入該功能時,全局副本,你只需要在離開時將其重新命名爲修改後的副本。

編輯:在迭代它時修改列表的其他註釋並不準確,因爲您不修改正在迭代的列表,您正在修改列表的「複製」。雖然其他人對改進方法的簡潔性提出了很好的建議,但您的原始方法在上述調整中是完全有效的。

Edit2:volcano的'comment'相對於全局是正確的,全局語句應該添加到def裏面來執行它。參考火山的答案是最好的方法,但我會留下來討論這個問題。

+0

除非您在函數中限定* listOfIntegers * as * global *(brrr),否則此代碼將不會修改來自外部作用域的列表,您的最新作業將創建**本地作用域** listOfIntegers – volcano

1

修改外部列表的「最乾淨的」方法是更改​​其內容而不重新分配 - 哪些更改列表對象引用。循環遍歷列表時不能刪除元素,並且在遍歷副本時刪除每個不符合的元素是非常無效的。

但是你可以重新分配列表的內容,而無需重新分配列表對象的引用 - 使用的賦值

def removeNegatives(listOfIntegers): 
    listOfIntegers[:] = filter(lambda x: x >= 0, listOfIntegers) 

該代碼的左側創建非負值的新名單,並取代外部範圍列表的全部內容。

0

既然你學習Python中,這是一個學習的好地方list comprehension

$ cat /tmp/tmp.py 
_list = [2, 7, -3, -3, 13, -14, 13, 5, 11, -4, 10, 5, 0, -5, -14, 
     -2, -9, -14, 2, -10, -5, 8, 7] 

print("Before:",_list) 
print("After:",[a for a in _list if a >= 0]) 

$ python3 /tmp/tmp.py 
Before: [2, 7, -3, -3, 13, -14, 13, 5, 11, -4, 10, 5, 0, -5, -14, -2, -9, -14, 2, -10, -5, 8, 7] 
After: [2, 7, 13, 13, 5, 11, 10, 5, 0, 2, 8, 7] 

正如你所看到的,在列表理解階段負數的消除是簡潔,明瞭,如果你測試它,你會發現它比使用循環的可比解決方案更快。