Python 生成器对象和 .join

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14447081/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 11:32:08  来源:igfitidea点击:

Python generator objects and .join

pythonstringlistpointersgenerator

提问by eazar001

Just a fundamental question regarding python and .join() method:

只是一个关于 python 和 .join() 方法的基本问题:

file1 = open(f1,"r")
file2 = open(f2,"r")
file3 = open("results","w")

diff = difflib.Differ()
result = diff.compare(file1.read(),file2.read())
file3.write("".join(result)),

The above snippet of code yields a nice output stored in a file called "results", in string format, showing the differences between the two files line-by-line. However I notice that if I just print "result" withoutusing .join(), the compiler returns a message that includes a memory address. After trying to write the result to the file withoutusing .join(), I was informed by the compiler that only strings and character buffers may be used in the .join() method, and not generator objects. So based off of all the evidence that I have adduced, please correct me if I am wrong:

上面的代码片段产生了一个很好的输出,以字符串格式存储在名为“results”的文件中,逐行显示了两个文件之间的差异。但是我注意到,如果我只打印“结果”而不使用 .join(),编译器会返回一条包含内存地址的消息。在尝试使用 .join()将结果写入文件后,编译器通知我在 .join() 方法中只能使用字符串和字符缓冲区,而不是生成器对象。因此,根据我提出的所有证据,如果我错了,请纠正我:

  1. result = diff.compare(file1.read(),file2.read())<---- result is a generator object?

  2. resultis a list of strings, with resultitself being the reference to the first string?

  3. .join()takes a memory address and points to the first, and then iterates over the rest of the addresses of strings in that structure?

  4. A generator object is an object that returns a pointer?

  1. result = diff.compare(file1.read(),file2.read())<---- 结果是生成器对象?

  2. result是一个字符串列表,它result本身是对第一个字符串的引用?

  3. .join()获取一个内存地址并指向第一个,然后迭代该结构中字符串的其余地址?

  4. 生成器对象是返回指针的对象吗?

I apologize if my questions are unclear, but I basically wanted to ask the python veterans if my deductions were correct. My question is less about the observable results, and more so about the inner workings of python. I appreciate all of your help.

如果我的问题不清楚,我深表歉意,但我基本上想问问蟒蛇老手我的推论是否正确。我的问题不是关于可观察到的结果,而是关于 python 的内部工作原理。我感谢您的所有帮助。

采纳答案by BrenBarn

joinis a method of strings. That method takes any iterable and iterates over it and joins the contents together. (The contents have to be strings, or it will raise an exception.)

join是一种字符串方法。该方法接受任何可迭代对象并对其进行迭代并将内容连接在一起。(内容必须是字符串,否则会引发异常。)

If you attempt to write the generator object directly to the file, you will just get the generator object itself, not its contents. join"unrolls" the contents of the generator.

如果您尝试将生成器对象直接写入文件,您将只获得生成器对象本身,而不是其内容。 join“展开”生成器的内容。

You can see what is going with a simple, explicit generator:

你可以看到一个简单的、显式的生成器发生了什么:

def gen():
    yield 'A'
    yield 'B'
    yield 'C'

>>> g = gen()
>>> print g
<generator object gen at 0x0000000004BB9090>
>>> print ''.join(g)
ABC

The generator doles out its contents one at a time. If you try to look at the generator itself, it doesn't dole anything out and you just see it as "generator object". To get at its contents, you need to iterate over them. You can do this with a forloop, with the nextfunction, or with any of various other functions/methods that iterate over things (str.joinamong them).

生成器一次分发一份内容。如果您尝试查看生成器本身,它不会发出任何内容,您只会将其视为“生成器对象”。要获取其内容,您需要遍历它们。您可以使用for循环、next函数或任何其他迭代事物的函数/方法(str.join其中)来做到这一点。

When you say that result "is a list of string" you are getting close to the idea. A generator (or iterable) is sort of like a "potential list". Instead of actually beinga list of all its contents all at once, it lets you peel off each item one at a time.

当你说结果“是一个字符串列表”时,你就接近这个想法了。生成器(或可迭代的)有点像“潜在列表”。它实际上不是一次列出所有内容的列表,而是让您一次剥离每个项目。

None of the objects is a "memory address". The string representation of a generator object (like that of many other objects) includes a memory address, so if you print it (as above) or write it to a file, you'll see that address. But that doesn't mean that object "is" that memory address, and the address itself isn't really usable as such. It's just a handy identifying tag so that if you have multiple objects you can tell them apart.

没有一个对象是“内存地址”。生成器对象的字符串表示(与许多其他对象一样)包括一个内存地址,因此如果您将其打印(如上所述)或将其写入文件,您将看到该地址。但这并不意味着对象“是”那个内存地址,而且地址本身并不是真正可用的。它只是一个方便的识别标签,因此如果您有多个对象,您可以将它们区分开来。