Python x, = ... - 这个尾随逗号是逗号运算符吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16037494/
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 21:39:06  来源:igfitidea点击:

x, = ... - is this trailing comma the comma operator?

pythonmatplotlibtuples

提问by inzzz

I don't understand what does comma after variable lines,means: http://matplotlib.org/examples/animation/simple_anim.html

我不明白变量后的逗号是什么意思:http: //matplotlib.org/examples/animation/simple_anim.html

line, = ax.plot(x, np.sin(x))

If I remove comma and variable "line," becomes variable "line" then program is broken. Full code from url given above:

如果我删除逗号和变量“line”,变成变量“line”,那么程序就会被破坏。上面给出的 url 的完整代码:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

fig = plt.figure()
ax = fig.add_subplot(111)

x = np.arange(0, 2*np.pi, 0.01)        # x-array
line, = ax.plot(x, np.sin(x))

def animate(i):
    line.set_ydata(np.sin(x+i/10.0))  # update the data
    return line,

#Init only required for blitting to give a clean slate.
def init():
    line.set_ydata(np.ma.array(x, mask=True))
    return line,

ani = animation.FuncAnimation(fig, animate, np.arange(1, 200), init_func=init,
    interval=25, blit=True)
plt.show()

According to http://docs.python.org/3/tutorial/datastructures.html#tuples-and-sequencescomma after variable seems to be related to tuples containing only one item.

根据http://docs.python.org/3/tutorial/datastructures.html#tuples-and-sequences变量后的逗号似乎与仅包含一项的元组有关。

采纳答案by Martijn Pieters

ax.plot()returns a tuplewith oneelement. By adding the comma to the assignment target list, you ask Python to unpack the return value and assign it to each variable named to the left in turn.

ax.plot()返回一个包含一个元素的元组。通过在赋值目标列表中添加逗号,您可以要求 Python 解压返回值并将其依次分配给左侧命名的每个变量。

Most often, you see this being applied for functions with more than one return value:

大多数情况下,您会看到这适用于具有多个返回值的函数:

base, ext = os.path.splitext(filename)

The left-hand side can, however, contain any number of elements, and provided it is a tuple or list of variables the unpacking will take place.

然而,左侧可以包含任意数量的元素,并且只要它是一个元组或变量列表,就会进行解包。

In Python, it's the comma that makes something a tuple:

在 Python 中,逗号使某些内容成为元组:

>>> 1
1
>>> 1,
(1,)

The parenthesis are optional in most locations. You could rewrite the original code withparenthesis without changing the meaning:

括号在大多数位置是可选的。您可以在不改变含义的情况下括号重写原始代码:

(line,) = ax.plot(x, np.sin(x))

Or you could use list syntax too:

或者你也可以使用列表语法:

[line] = ax.plot(x, np.sin(x))

Or, you could recast it to lines that do notuse tuple unpacking:

或者,您可以将其重铸为使用元组解包的行:

line = ax.plot(x, np.sin(x))[0]

or

或者

lines = ax.plot(x, np.sin(x))

def animate(i):
    lines[0].set_ydata(np.sin(x+i/10.0))  # update the data
    return lines

#Init only required for blitting to give a clean slate.
def init():
    lines[0].set_ydata(np.ma.array(x, mask=True))
    return lines

For full details on how assignments work with respect to unpacking, see the Assignment Statementsdocumentation.

有关赋值如何与解包相关的完整详细信息,请参阅赋值语句文档。

回答by Elmar Peise

If you have

如果你有

x, = y

you unpack a list or tuple of length one. e.g.

您解压缩长度为 1 的列表或元组。例如

x, = [1]

will result in x == 1, while

将导致x == 1,而

x = [1]

gives x == [1]

x == [1]