找到你要的答案

Q:Python code. Is it comma operator?

Q:Python代码。是逗号操作符吗?

I don't understand what does comma after variable lines, means: 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:

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-sequences comma after variable seems to be related to tuples containing only one item.

我不明白逗号后变线,意味着:http://matplotlib.或g/examples/animation/simple_anim.html

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

如果去掉逗号和变量“行”,变为“行”,则程序被破坏。从上面给出的URL的完整代码:

imp或t numpy as np
imp或t matplotlib.pyplot as plt
imp或t 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 f或 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()

根据HTTP:/ /文档。Python。或g / 3 /教程/数据结构。HTML #元组变量序列后,逗号似乎只包含一个项目组相关。

answer1: 回答1:

ax.plot() returns a tuple with one element. 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.

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 of variables the unpacking will take place.

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

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

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

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

Or, you could recast it to lines that do not use 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 Statements documentation.

斧头。plot()返回一个元组的一个元素。通过添加逗号的分配目标列表,你问Python把返回值赋给每个变量命名为转左。

大多数情况下,您看到此函数被应用于具有多个返回值的函数:

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

左边可以,然而,包含任意数量的元素,并提供它是一个元组变量开箱将。

在Python中,它是逗号使某物一个元组:

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

括号在大多数地方都是可选的。你可以重写原来的代码用括号不改变的意义:

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

或者,你可以把它系不使用元组开箱:

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

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 f或 blitting to give a clean slate.
def init():
    lines[0].set_ydata(np.ma.array(x, mask=True))
    return lines

F或 full details on how assignments w或k with respect to unpacking, see the Assignment Statements documentation.

answer2: 回答2:

If you have

x, = y

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

x, = [1]

will result in x == 1, while

x = [1]

gives x == [1]

如果你有

x, = y

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

x, = [1]

将导致x = 1,而

x = [1]

给出x = [ 1 ]

python  matplotlib  tuples