1

I have a two Lists one with truth values and other containing experimental results and their associated scores.

truth = [6, 8, 7, 10]
experiment = [(6, 5), (4, 3), (2, 4), (11, 6), (7, 4)]

I want to align my experiment and truth values such that maximum truth value align

        [6, 8, 7, 10]
        [6, missing, 7, missing]

Now I'd like to assign value to missing from the from among subsequences which are not aligned.

Here we chose 11 from among (4, 3), (2, 4), (11, 6) since it has the highest score. The last missing value is assigned 0 because no element lies beyond (7, 4)

truth = [6, 8, 7, 10]
exp  =  [6, 11, 7, 0]   # Zero because 7 is the last element of experiment subsequence.

I was looking in difflib library but didn't understand much. How should I go about this?

Ada Xu
  • 953
  • 4
  • 14
  • 31
  • 2
    I understand the first result you're looking for but not the second. You need to give us a little more detail, perhaps even some example code. – James Mertz Jul 23 '14 at 20:31
  • @KronoS I was thinking about using set operations for this but I am not sure if that's the correct approach – Ada Xu Jul 23 '14 at 20:35
  • I'm talking about fundamentally here. How do you get the `[6, 11, 7, 0]`? I understand the `[6, NaN, 7, NaN]` though I don't think that tells you much. What is your overall end goal here? – James Mertz Jul 23 '14 at 20:37
  • I mean for the missing values I'd like to substitute the subsequence with the value that has the highest likelihood. Here a value is missing between 6 and 7. I'll substitute 11 because it has the the maximum score among the values between 6 and 7 – Ada Xu Jul 23 '14 at 20:38
  • @KronoS How should I solve the first part of problem? Could you give me some pointers? – Ada Xu Jul 23 '14 at 20:55

3 Answers3

0

For the first part, you can do something like this:

truth = [6, 8, 7, 10]
experiment = [(6, 5), (4, 3), (2, 4), (11, 6), (7, 4)]

def prepare(exp):
    keys = [i for i, j in exp]
    def select(v):
        return v if v in keys else None
    return select

select = prepare(experiment)

exp = [select(i) for i in truth]

# exp will be [6, None, 7, None]
user3557327
  • 1,109
  • 13
  • 22
0

The first part could be done with a simple list comprehension:

truth = [6, 8, 7, 10]
experiment = [(6, 5), (4, 3), (2, 4), (11, 6), (7, 4)]
res1 = [x if x in dict(experiment) else None for x in truth]

... or if you prefer lambda functions:

truth = [6, 8, 7, 10]
experiment = [(6, 5), (4, 3), (2, 4), (11, 6), (7, 4)]
res1 = map(lambda x: x if x in dict(experiment) else None, truth)
Delimitry
  • 2,987
  • 4
  • 30
  • 39
st0ne
  • 4,165
  • 2
  • 23
  • 24
0

Assuming that you'll never have two or more missing values next to each other, you can use:

truth = [6, 8, 7, 10]
experiment = [(6, 5), (4, 3), (2, 4), (11, 6), (7, 4)]

exp_list = [x[0] for x in experiment] #[6, 4, 2, 11, 7]

# Part one.
exp = [t if t in exp_list else None for t in truth] #[6, None, 7, None]

# Part two.
for i, t in enumerate(exp):
    if t == None:
        # Get index of previous and next truth values for missing values.
        i_prev = exp_list.index(exp[i-1]) if i else 0
        i_next = exp_list.index(exp[i+1]) if i < len(exp)-1 else len(exp_list)
        # Pick the largest value between them or 0 if no values are there.
        values_between = exp_list[slice(i_prev + 1, i_next)]
        exp[i] = max(values_between) if values_between else 0

print exp #[6, 11, 7, 0]
dwitvliet
  • 7,242
  • 7
  • 36
  • 62
  • Well actually there could be missing values next to each. I could probably experiment with randomly selecting values from experiment. Thanks anyway :) – Ada Xu Jul 23 '14 at 22:03