行,= plot(x,sin(x))逗号代表什么? [英] line, = plot(x,sin(x)) what does comma stand for?

查看:128
本文介绍了行,= plot(x,sin(x))逗号代表什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作动画情节.这是示例代码:

I'm trying to make an animated plot. Here is an example code:

from pylab import *
import time

ion()

tstart = time.time()               # for profiling
x = arange(0,2*pi,0.01)            # x-array
line, = plot(x,sin(x))
for i in arange(1,200):
    line.set_ydata(sin(x+i/10.0))  # update the data
    draw()                         # redraw the canvas

print 'FPS:' , 200/(time.time()-tstart)

我不了解line,.没有逗号,代码将无法正常工作.

I don't understand the line,. Without comma, the code doesn't work.

推荐答案

逗号是Python语法,表示单元素元组.例如,

The comma is Python syntax that denotes either a single-element tuple. E.g.,

>>> tuple([1])
(1,)

在这种情况下,它用于参数解压:plot返回一个单元素列表,该列表被解压到line:

In this case, it is used for argument unpacking: plot returns a single-element list, which is unpacked into line:

>>> x, y = [1, 2]
>>> x
1
>>> y
2
>>> z, = [3]
>>> z
3

另一种可能更易读的方法是使用类似列表的语法:

An alternative, perhaps more readable way of doing this is to use list-like syntax:

>>> [z] = [4]
>>> z
4

尽管z, =在Python代码中更常见.

though the z, = is more common in Python code.

这篇关于行,= plot(x,sin(x))逗号代表什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆