在python中对齐两个列表

时间:2014-07-23 20:29:24

标签: python list

我有两个列表,其中包含真值,其他包含实验结果及其相关分数。

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

我想调整我的实验和真值,使最大真值对齐

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

现在我想为未对齐的子序列中的缺失分配值。

这里我们从(4, 3), (2, 4), (11, 6)中选择了11,因为它得分最高。 最后一个缺失值被指定为0,因为没有元素超出(7, 4)

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

我在查看difflib图书馆,但不太了解。 我该怎么办呢?

3 个答案:

答案 0 :(得分:0)

对于第一部分,你可以这样做:

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]

答案 1 :(得分:0)

第一部分可以通过简单的列表理解来完成:

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]

...或者如果您更喜欢lambda函数:

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)

答案 2 :(得分:0)

假设您永远不会有两个或多个缺失值彼此相邻,您可以使用:

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]