简体   繁体   English

关于Python函数和列表的困惑

[英]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}” 如果用户希望使用索引从列表中删除一个项目,则传递的第三个参数将是“index” ,如果用户希望使用传递的项目从列表中删除第一个参数,则第二个参数将是“{item}”

For example, to remove the item at index 3 from a list, this would be the command myFunction(myList,3,”index”) 例如,要从列表中删除索引3处的项目,这将是命令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. 很明显,尽管您在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") (查询=“定义函数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}” " 问题(我认为)是:“ 如果用户希望使用索引从列表中删除一个项目,则如果用户希望使用该项目从列表中删除第一个项目,则传递的第三个参数将是“ index”。通过,第二个参数将为“ {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. 注意,由于第三个参数是可选的,因此我给了默认值None

The first thing we will do is to test typeOfItem . 我们要做的第一件事是测试typeOfItem The question says that is it is an index then it will say "index" else the second parameter will say "{item}" . 问题是说它是一个索引,然后它将说"index"否则第二个参数将说"{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 { } 最后一部分是删除切片 ,该切片从字符1(第二个字符)开始,到最后一个字符-1结束,因此删除了{ }

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. 数字和字符串不是这种情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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