简体   繁体   English

与单元素元组混淆

[英]confused with single element tuple

here below the codes i tried to locate the relative maxima and minima of stock prices and make a plot with matplotlib 在代码下方,我试图找到股价的相对最大值和最小值,并使用matplotlib进行绘制

stockewma=pd.ewma(stock,span=3)
stocka=np.array(stockewma.values)
mx=argrelextrema(stocka,np.greater)
mn=argrelextrema(stocka,np.less)
mxx=[stock.index[i] for i in mx]
mxy=[stock.ix[i] for i in mx]
mnx=[stock.index[i] for i in mn]
mny=[stock.ix[i] for i in mn]

the code works alright, but then i found out the argrelextrema actually returns a tuple of SINGLE np.array of a SINGLE list. 该代码可以正常工作,但是后来我发现argrelextrema实际上返回一个单列表的单np.array的元组。

why should it wrap the data as clumsy as that? 为什么要像这样笨拙地包装数据?

for that i have to access the index value with mx[0] #cuz it is a single element tuple. 为此,我必须使用mx [0] #cuz访问索引值,它是单个元素元组。

and i don't understand why the following code could work, index with nesting a array of single list?! 我不明白为什么下面的代码可以工作,嵌套单个列表的数组索引?

mxx=[stock.index[i] for i in mx]
mxy=[stock.ix[i] for i in mx]
mnx=[stock.index[i] for i in mn]
mny=[stock.ix[i] for i in mn]

argrelextrema returns a single element tuple so that the following mxx and mxy statements work. argrelextrema返回单个元素元组,以便以下mxxmxy语句起作用。 These are list comprehensions 这些是列表理解

The statement 该声明

mxx=[stock.index[i] for i in mx]

returns a list which could be created by the following equivalent code 返回可以由以下等效代码创建的列表

mxx = []
for i in mx:
    value = stock.index[i]
    mxx.append(value)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM