tags:

views:

128

answers:

3

Hi,

From two unequal arrays, i need to compare & delete based on the last value of an array.

Example:

m[0] and n[0] are read form a text file & saved as a array, [0] - their column number in text file.

m[0] = [0.00, 1.15, 1.24, 1.35, 1.54, 2.32, 2.85, 3.10, 3.40, 3.80, 4.10, 4.21, 4.44]

n[0] = [0.00, 1.12, 1.34, 1.45, 2.54, 3.12, 3.57]

n[0] last value is 3.57, it lies between 3.40 and 3.80 of m[0] so I need to print till 3.40 in m[0]`

Required output:

p[0] = [0.00, 1.15, 1.24, 1.35, 1.54, 2.32, 2.85, 3.10, 3.40]
A: 

I haven't been able to test this but here you go...

p = []
for item in m[0]:
    if (item < n[0][-1]):
        p.append(item)
    else:
        break
sixfoottallrabbit
len(n) is completely unnecessary here.
hop
Right, thanks - I haven't used Python in a while.
sixfoottallrabbit
@hop: There is so much that is unnecessary here. When you start teasing it apart, it becomes Ned Batchelder's one-liner.
hughdbrown
+6  A: 

Some details are a little unclear, but this should do what you want:

p[0] = [x for x in m[0] if x < n[0][-1]]
Ned Batchelder
+1  A: 

if both lists are ordered, you can do:

import bisect
m[0][:bisect.bisect(m[0],n[0][-1])]
fortran