Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is the best way to sort a sequence in Python?
    primarykey
    data
    text
    <p>I am trying to sort the table based on certain conditions that need to happen in a row. Simplified version of a table:</p> <pre><code>Number Time 1 23 2 45 3 67 4 23 5 11 6 45 7 123 8 34 </code></pre> <p>...</p> <p>I need to check if time was &lt;40 5 times in a row. Like I need to check rows 1-5, then 2-6 etc... And then print and save to a file the first and last time. Like, if the condition is met for rows 2-6 I will need to print time for Number 2 and Number 6. The checking should stop after condition has been met. No need to check other rows. I implemented a counter with two temp variables to check for 3 items in a row so far. It works fine. But, if I want to check for the condition that happened 30 times in a row, I can not just create 30 temp variables manually. What is the best way to achieve that? I guess I will just need some kind of a loop. Thanks!</p> <p>Here is part of my code:</p> <pre><code>reader = csv.reader(open(filename)) counter, temp1, temp2, numrow = 0, 0, 0, 0 for row in reader: numrow+=1 if numrow &lt;5: col0, col1, col4, col5, col6, col23, col24, col25 = float(row[0]), float(row[1]), float(row[4]), float(row[5]),float(row[6]), float(row[23]), float(row[24]), float(row[25]) if col1 &lt;= 40: list1=(col1, col3, col4, col5, col6, col23, col24, col25) counter += 1 if counter == 3: print("Cell# %s" %filename[-10:-5]) print LAYOUT.format(*headers_short) print LAYOUT.format(*temp1) print LAYOUT.format(*temp2) print LAYOUT.format(*list1) print "" elif counter == 1: temp1=list1 elif counter == 2: temp2=list1 else: counter = 0 </code></pre> <p>I implemented solution suggested by Bakuriu and it seems to be working. But what will be the best way to combine numerous testing? Like I need to check for several conditions. Lets say: v</p> <ul> <li>efficiency for less than 40 in 10 cycles in a row, </li> <li>capacity for less than 40 in 5 cycles in row </li> <li>time for less than 40 for 25 cycles in a row</li> <li>and some others...</li> </ul> <p>Right now I just open csv.reader for every testing and run the function. I guess it is not the most efficient way, although it works. Sorry, I am just a complete noob.</p> <pre><code>csvfiles = glob.glob('processed_data/*.stat') for filename in csvfiles: flag=[] flag.append(filename[-12:-5]) reader = csv.reader(open(filename)) for a, row_group in enumerate(row_grouper(reader,10)): if all(float(row[1]) &lt; 40 for row in row_group): str1= "Efficiency is less than 40 in cycles "+ str(a+1)+'-'+str(a+10) #i is the index of the first row in the group. flag.append(str1) break #stop processing other rows. reader = csv.reader(open(filename)) for b, row_group in enumerate(row_grouper(reader,5)): if all(float(row[3]) &lt; 40 for row in row_group): str1= "Capacity is less than 40 minutes in cycles "+ str(a+1)+'-'+str(a+5) flag.append(str1) break #stop processing other rows. reader = csv.reader(open(filename)) for b, row_group in enumerate(row_grouper(reader,25)): if all(float(row[3]) &lt; 40 for row in row_group): str1= "Time is less than &lt; 40 in cycles "+ str(a+1)+'-'+str(a+25) flag.append(str1) break #stop processing other rows. if len(flag)&gt;1: for i in flag: print i print '\n' </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload