0

I have a list of 3 item tuples i,j,k, where i is my output variable, j is the time (thus sets the sequence of the dataset) and k is the input variable. The first "hit" needs to stay at posn 1 in the results set as they are later enumerated and the order is key.

I want to extract the [i,j] for:

  1. k = 0.1 (rounded)
  2. Every 5th k = 0.1, but not every 10th
  3. Only extract a value if there are exactly ten k = 0.1 in a row and disregard if there are more or less (note that k = 0.1 can occur at any point so I can't just index slice)

I can do 1. and 2. but have just got a new large dataset where 3. is now a problem.

values = []
count = 0
    for i,j,k in zip(a, b, c):
        if float('%.1g' % k) == 0.1:
            count += 1
            if (count % 5) == 0 and (count % 10 != 0):               
                values.append([i,j])

Example (sorry for length)

a, b, c
33, 1, 0
33, 2, 0
30, 3, 0
33, 4, 0
33, 5, 0.1
33, 6, 0.1 
31, 7, 0.1 
33, 8, 0.1 
37, 9, 0.1 
33, 10, 0.1 
33, 11, 0.1 
36, 12, 0.1 
33, 13, 0.1 
33, 14, 0.1
33, 15, 5
39, 16, 5
33, 17, 0.1 
33, 18, 0.1 
36, 19, 0.1 
33, 20, 0.1 
32, 21, 0.1
33, 22, 0.1 

All I want from that list is [37,9] as that is the 5th entry of a block of ten k = 0.1.

MattCryer
  • 135
  • 1
  • 8
  • I can't follow what you are trying to do, can you give an example input and output. BTW `float('%.1g' % k)` is just the same as `float(k)` the string formatting is unnecessary. Comparing floats for equality is problematic just leave it as a string `k == '0.1'`. – AChampion Feb 16 '17 at 03:12
  • Please don't post links, just update your question with sample data and expected output. – AChampion Feb 16 '17 at 03:26
  • The k value is not exactly 0.1, if k = 0.10367, float(k) not equal to float('%.1g' % k). The k values are not identical as they are the measured value of an applied voltage that is trying to be 0.1 – MattCryer Feb 16 '17 at 03:49
  • Ok, still better to use `format(float(k), '.1g') == '0.1', comparing floats is problematic, see https://stackoverflow.com/questions/5595425/what-is-the-best-way-to-compare-floats-for-almost-equality-in-python – AChampion Feb 16 '17 at 05:03

1 Answers1

1

If you just want the fifth entry of every block of 10, then using a simple generator function to return only chunks of 10 that meet c == 0.1:

def gen(a, b, c):
    g = []
    for i, j, k in zip(a, b, c):
        if format(float(k), '.1g') == '0.1':
            g.append((i,j))
        else:
            g = []
        if len(g) == 10:
            yield g

>>> [list(e)[4] for e in gen(a, b, c)]
[('37', '9')]
AChampion
  • 29,683
  • 4
  • 59
  • 75