简体   繁体   English

如何检查列表中的数字是否大于它之前的数字-python

[英]How to check if a number in a list is greater than the number before it - python

I have 3 sets of data zipped together (date,somenumber,price).我有 3 组数据压缩在一起(日期、某个数字、价格)。 I want to iterated through the table and whenever somenumber is less than somenumber before it in the list I want to pull the date,somenumber,price for when that happens.我想遍历表,每当某个数字小于列表中的某个数字之前,我想提取日期、某个数字、发生这种情况时的价格。 Currently I just have:目前我只有:

for a,b,c in zip(date,somenumber,price):
    print(a,b,c)

and it prints something like:它打印出类似的东西:

2018-01-30 18:42:00 859235 6.95
2018-02-01 09:08:00 323405 7.43
2018-02-02 15:16:00 528963 6.4
2018-02-05 18:48:00 808739 7.91
2018-02-07 14:42:00 462541 7.33
2018-02-10 18:48:00 598001 6.21
2018-02-11 03:32:00 650558 7.31
2018-02-11 11:28:00 670392 6.21

When somenumber went from 808739 to 462541 I want to return the data for that lower number: 2018-02-07 14:42:00 462541 7.33当某个数字从 808739 变为 462541 时,我想返回该较低数字的数据:2018-02-07 14:42:00 462541 7.33

Thanks!谢谢!

This will compare somenumber+1 to somenumber, and if somenumber + 1 (the next number in the sequence is less than the current) it will print all data for that row.这会将 somenumber+1 与 somenumber 进行比较,如果 somenumber + 1(序列中的下一个数字小于当前数字),它将打印该行的所有数据。 If it is not less it will not print anything.如果不小于,则不会打印任何内容。

for a,b,c,d in zip(date, somenumber, somenumber[1:], price):
    if c < b:
        print(a, b, c, d)

What I understood from the question is every time, the value of somenumber decreases in the next row, it should print the row.我从问题中了解到的是,每次somenumber的值在下一行减少时,它应该打印该行。 It's very easy to implement.这很容易实现。 Just store the previous value in another variable ( prev_number ).只需将先前的值存储在另一个变量 ( prev_number ) 中。 For the first row, though, we need to initialize prev_number to the minimum number possible.但是,对于第一行,我们需要将prev_number初始化为可能的最小数字。 Following is the solution:以下是解决方案:

import sys
prev_number = -sys.maxint - 1    #initialize prev_number to lowest possible number in python
for a,b,c in zip(date,somenumber,price):
    if b < prev_number:
        print(a,b,c)
    prev_number = d

Let me know if it works for you.请让我知道这对你有没有用。

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

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