简体   繁体   English

使用python遍历列表列表

[英]Looping through a list of lists using python

I have a function that creates a list containing 3 elements and appends this list to a master list called peaks: 我有一个函数,该函数创建一个包含3个元素的列表,并将此列表追加到称为Peaks的主列表中:

peak = (x_data point, y_data point and area)   
peaks = (peak, peak, peak etc)

I am trying to write a loop that cycles through the peak[area] elements and returns the areas that are greater than 100000 and then using this in a matplotlib 'text' to graph input. 我试图编写一个循环,循环遍历peak [area]元素并返回大于100000的区域,然后在matplotlib'text'中使用此循环以图形化输入。 Here is my code: 这是我的代码:

plot(x_data, y_data)                          
for peak in peaks:
    if peak[2] >= 100000:
        text(peak[0], 1.02*peak[1], '%d'%int(peak[2]))
grid(True)                 
show()

The output is just the x_data vs y_data plot with no text annotations...any ideas why? 输出只是x_data vs y_data图,没有文本注释...为什么有任何想法? I can't figure it out. 我不知道。

This loop works if i exclude the if statement. 如果我排除if语句,则此循环有效。 However, I want to select the peaks with an area greater than 100000. 但是,我要选择面积大于100000的峰。

It appears that the peak areas are reported as negative. 看来峰面积报告为负。

The solution was to change 解决的办法是改变

for peak in peaks:
    if peak[2] >= 100000:
        text(peak[0], 1.02*peak[1], '%d'%int(peak[2]))

to

for peak in peaks:
    if peak[2] <= -100000:
        text(peak[0], 1.02*peak[1], '%d'%int(peak[2]))

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

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