Removing Negative Elements in a List - Python
问题 So, I`m trying to write a function that removes the negative elements of a list without using .remove or .del. Just straight up for loops and while loops. I don`t understand why my code doesn`t work. Any assistance would be much appreciated. def rmNegatives(L): subscript = 0 for num in L: if num < 0: L = L[:subscript] + L[subscript:] subscript += 1 return L 回答1: A note to your code: L = L[:subscript] + L[subscript:] does not change your list. For example >>> l = [1,2,3,4] >>> l[:2] + l[2:] [1