2014-07-23 82 views
1

我有一個兩個列表,其中包含真值和其他包含實驗結果及其相關分數的列表。對齊python中的兩個列表

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庫中,但並沒有多少了解。 我應該怎麼做?

+2

我明白你要找的第一個結果,但不是第二個結果。你需要給我們更多的細節,甚至可能是一些示例代碼。 – KronoS

+0

@KronoS我正在考慮使用set操作,但我不確定這是否是正確的方法 –

+0

我在這裏從根本上談論。你如何得到'[6,11,7,0]'?我瞭解'[6,NaN,7,NaN]',儘管我認爲這並不能說明問題。你在這裏的總體目標是什麼? – KronoS

回答

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] 
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] 

...或者,如果你喜歡拉姆達功能:

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) 
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] 
+0

實際上,每個旁邊都可能缺少值。我大概可以嘗試從實驗中隨機選擇數值。不管怎麼說,還是要謝謝你 :) –