简体   繁体   中英

Confusion about Python Functions and Lists

I am trying to create a function to remove an item from the passed list either by a specified index, or item passed.

If the user wishes to remove an item from the list using an index, the third argument passed will be “index” , if the user wishes to remove the first item found in the list using the item passed, the second argument will be “{item}”

For example, to remove the item at index 3 from a list, this would be the command myFunction(myList,3,”index”)

I am quite confused about this function part. I have written code that does exactly what the question seems to ask, but it does not use a function. My code is below :

mylist = ["one" , "two" ,"three" , "four" , "five"]
print "list is composed of: "+ str(mylist)
name = raw_input("Index of item to be removed. ex.  1")
name2 = raw_input('"item to be removed. ex. four')
name3 = int(name)
del mylist[name3]
mylist.remove(name2)
print mylist

It appears that I need to create a function to do this, and then pass in my list, the index/item, etc.) but I am very lost on this part.

You really need to work on your questionsmithing skills. It's very difficult to understand what you are trying to accomplish. After making about half a dozen assumptions, I think this is what you are trying to do:

def listRemover(mylist,index_or_name,mytype):
    if mytype == "index":
        del mylist[index_or_name]

    if mytype == "name":
        mylist.remove(index_or_name)

It's obvious though that there are some gaping holes in your basic knowledge of python. You need to study what a function is, why they are useful, and how to use them.

It appears that I need to create a function to do this, and then pass in my list, the index/item, etc.) but I am very lost on this part.

Google it! (query = "define function python")

Show your research. The basic form of a function is:

def funcname(arg1, arg2, arg3):
   # now you can use the vars arg1, arg2, and arg3.
   # rename them to whatever you want.
   arg1[0] = "bannanas"

so,

array = ['mango', 'apple']
funcname(array)
print(array) # -> ['bannanas', 'apple']

The question (I think) is: " If the user wishes to remove an item from the list using an index, the third argument passed will be “index”, if the user wishes to remove the first item found in the list using the item passed, the second argument will be “{item}” "

The purpose of this exercise (presumably) is to practice writing a function. Yes you could do it without a function but right now you need the practice of writing a function and passing parameters. Functions are a very important part of programming, but this is not a good place to go into that.

So first we define our function:

def removeItem( theList, theItem, typeOfItem=None ):

Notice I have given a default value of None because the third parameter is optional.

The first thing we will do is to test typeOfItem . The question says that is it is an index then it will say "index" else the second parameter will say "{item}" . So it will be one or the other. (What to do if that is not the case is a question you should ask).

The index part is easy:

    if typeOfItem == "index":
        del(theList[theItem])

but now its a bit more complicated, because of the { } , which we have to remove:

    else:
        theList.remove(theItem[1:-1])

This last part is removing a slice , which starts at character 1 (the second character) and ends at the final character -1, thus removing the { }

So the final function code, with tests, is:

def removeItem( theList, theItem, typeOfItem=None ):
    if typeOfItem == "index":
        del(theList[theItem])
    else:
        theList.remove(theItem[1:-1])

mylist = ["one" , "two" ,"three" , "four" , "five"]
removeItem(mylist, 3, "index")
print mylist

mylist = ["one" , "two" ,"three" , "four" , "five"]
removeItem(mylist, "{two}")
print mylist

Notice an important feature of the function and the list. If you alter the list inside the function then it also alters it outside the function as well - it is the same list. That is not the case with numbers and strings.

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