gpt4 book ai didi

Python 列表和运算符

转载 作者:太空狗 更新时间:2023-10-30 02:33:02 25 4
gpt4 key购买 nike

来自艰难地学习 Python:

Python sees you mentioned mystuff and looks up that variable. It might have to look backwards to see if you created with =, look and see if it is a function argument, or maybe it's a global variable. Either way it has to find the mystuff first.

Once it finds mystuff it then hits the . (period) operator and starts to look at variables that are a part of mystuff. Since mystuff is a list, it knows that mystuff has a bunch of functions.

It then hits append and compares the name "append" to all the ones that mystuff says it owns. If append is in there (it is) then it grabs that to use. Next Python sees the ( (parenthesis) and realizes, "Oh hey, this should be a function." At this point it calls (aka runs, executes) the function just like normally, but instead it calls the function with an extra argument.

That extra argument is ... mystuff! I know, weird right? But that's how Python works so it's best to just remember it and assume that's alright. What happens then, at the end of all this is a function call that looks like: append(mystuff, 'hello') instead of what you read which is mystuff.append('hello').

他从哪里得到“mystuff”?而且我仍然不确定那个周期运算符是如何工作的(抱歉我是新手,请多多包涵),稍后我们会得到这个:

ten_things = "Apples Oranges Crows Telephone Light Sugar"

print "Wait there's not 10 things in that list, let's fix that."

stuff = ten_things.split(' ')

我没有看到最后一行之后该字符串如何变成一个列表,.split 会自动将它变成一个列表还是什么?他正在做的那个时期的“拆分”或“附加”事情的名称是什么?在编程中搞砸我的主要事情之一是我不知道很多东西实际上叫什么。我知道函数、变量等,但像 .split 这样的东西让我很困惑。

帮忙吗?

最佳答案

stuff = ten_things.split(' ') 不会更改 ten_things 的值。相反,它会创建一个名为 stuff 的新变量,并将 ten_things.split(' ') 创建的列表保存到其中。此处作为参数传递给 split 方法的空格很重要。它的意思是 Python 应该采用字符串 ten_things 并将其拆分,使用 split 的参数作为分隔符。

例子:

"这是一个字符串".split(' ') == ["This", "is", "a", "string"]

"This|is|a|string".split('|') == ["This", "is", "a", "string"]

关于Python 列表和运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15193027/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com