简体   繁体   中英

List returning empty after putting it through removeEven function in other file

sorry about the code it is sloppy right now because I've been trying to fix it. Im using a function from another file to remove even numbers from my list but after I call the function the list returns empty.

from usefullFunctions import *
def main ():
    mylist1 = uRandomList(10,0,15)
    listLength = len(mylist1)
    print("list1 contains %s numbers " % listLength)
    print (mylist1)
    evenOut = removeEven(mylist1)
    print (mylist1)


main()

These are the two functions that I am calling from the other file.

def removeEven(listIn):
    result = []
    i = 0 
    while i < len(listIn):
        if (i % 2 ) == 0:
            listIn.pop(i)
        else:
            i = i + 1 
    return result
def uRandomList (num, minValue, maxValue):
    result = []
    for i in range (num):
        d1 = randint(minValue, maxValue)
        if d1 not in result:
            result.append(d1)
    return result 

I'm just trying to get it to remove even numbers from the list so that I can print it with the even numbers removed. Thank you for your help in advance.

Fixed function

def removeEven(listIn):
    i = 0 
    while i < len(listIn):
        if listIn[i] % 2  == 0:
            listIn.pop(i)
        else:
            i = i + 1 
    return 

You are not incrementing i in if block of removeEven() . So, say input list has 3 elements. When i is 0 initially, you remove element 0. len(listIn) becomes 2. In next iteration i is still 0 and you remove the first element again. This process continues and remove all elements from input list. The idea to loop until length of the input list while the loop is removing element from that list is not good. Also the function is returning an empty list.

As I can see, you are returning another list from removeEven() . So why not leave the input list as it is. Use something like -

def removeEven(listIn):
    return [v for index, val in enumerate(listIn) if index % 2 == 0]

To my eyes, its that you're declaring a list here, result = [] , then returning it return result without actually doing anything to it in between. So you've created an empty list, then returned it.

def removeEven(listIn):
    result = [] # first mention of "result"
    i = 0 
    while i < len(listIn):
        if (i % 2 ) == 0:
            listIn.pop(i)
        else:
            i = i + 1 
    return result # only other reference

Although the error here is pretty straightforward, I think maybe you have some misunderstandings about lists in Python. It could help to think about

  • Is the "removeEven" function supposed to modify the existing list? or is it supposed to return a new one? You're doing both here, which is likely not the best solution. calling listIn.pop() is modifying the original list being passed, but you've created a new list to be returned, and you've also created a new variable for it in your original file

In my opinion it might be more appropriate to return a new list in this case, so that's what i'll show you here

def with_even_removed(list_in):
    result = []

    i = 0
    while i < len(list_in):
        if i % 2 != 0:
            result.append(list_in[i])
        i += 1
    return result


l = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

l2 = with_even_removed(l)

print("original list =", l)
print("newly returned list =", l2)

Output

original list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
newly returned list = [1, 3, 5, 7, 9]

As you can see, the original list l was unchanged, but the returned list l2 is different, it has the even index numbers removed

Try defining a function that returns 0 for even numbers and 1 for odd numbers:

is_odd = lambda x: x % 2

Then pass that function into a list comprehension using the result from uRandomList :

odd_nums = [x for x in result if is_odd(x)]

Edit: While it is entirely possible to shorten this code by just putting the odd/even test inside the list comprehension, it's nice to practice passing functions about as you might have more complicated tests to do at some point.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM