2014-10-16 91 views
0

我正在寫的函數有一個列表列表。我想遍歷列表,獲取列表中的第一個元素和所有匹配的元素,並將其他數組中的0賦給相同的位置。然後爲1,依此類推,直到分配所有元素。爲每個相同的列表項分配一個值

如何在python中執行此操作?

就拿這個名單:

[[0.39999999999999997], 
[2.145454545454545], 
[0.39999999999999997], 
[0.39999999999999997], 
[0.39999999999999997], 
[2.145454545454545], 
[2.145454545454545], 
[2.145454545454545], 
[2.145454545454545], 
[2.145454545454545], 
[2.145454545454545], 
[2.145454545454545], 
[0.39999999999999997], 
[0.39999999999999997], 
[0.39999999999999997], 
[2.145454545454545], 
[2.145454545454545], 
[2.145454545454545]] 

和輸出[0,1,0,0,0,1,1,1,1,1,1,1,0,0,0,1,1,1]

或者

closestCluster labels: 

[[0.18, 0.31, 0.32, 0.42, 0.28], [0.18, 0.31, 0.32, 0.42, 0.28], [0.18, 0.31, 0.32, 0.42, 0.28], 
[0.18, 0.31, 0.32, 0.42, 0.28], [0.18, 0.31, 0.32, 0.42, 0.28], [0.18, 0.31, 0.32, 0.42, 0.28], [0.18, 0.31, 0.32, 0.42, 
0.28], [0.18, 0.31, 0.32, 0.42, 0.28], [0.18, 0.31, 0.32, 0.42, 0.28], [0.18, 0.31, 0.32, 0.42, 0.28], [0.18, 0.31, 0.3 
2, 0.42, 0.28], [0.18, 0.31, 0.32, 0.42, 0.28], [0.18, 0.31, 0.32, 0.42, 0.28], [0.18, 0.31, 0.32, 0.42, 0.28], [0.18, 0 
.31, 0.32, 0.42, 0.28], [0.18, 0.31, 0.32, 0.42, 0.28], [0.18, 0.31, 0.32, 0.42, 0.28], [0.18, 0.31, 0.32, 0.42, 0.28], 
[0.18, 0.31, 0.32, 0.42, 0.28], [0.18, 0.31, 0.32, 0.42, 0.28], [0.18, 0.31, 0.32, 0.42, 0.28], [0.18, 0.31, 0.32, 0.42, 
0.28], [0.18, 0.31, 0.32, 0.42, 0.28], [0.18, 0.31, 0.32, 0.42, 0.28], [0.18, 0.31, 0.32, 0.42, 0.28], [0.18, 0.31, 0.3 
2, 0.42, 0.28], [0.18, 0.31, 0.32, 0.42, 0.28], [0.18, 0.31, 0.32, 0.42, 0.28], [0.18, 0.31, 0.32, 0.42, 0.28], [0.18, 0 
.31, 0.32, 0.42, 0.28], [0.18, 0.31, 0.32, 0.42, 0.28], [0.18, 0.31, 0.32, 0.42, 0.28], [0.18, 0.31, 0.32, 0.42, 0.28], 
[0.18, 0.31, 0.32, 0.42, 0.28], [0.18, 0.31, 0.32, 0.42, 0.28], [0.18, 0.31, 0.32, 0.42, 0.28], [0.18, 0.31, 0.32, 0.42, 
0.28], [0.18, 0.31, 0.32, 0.42, 0.28], [0.18, 0.31, 0.32, 0.42, 0.28], [0.18, 0.31, 0.32, 0.42, 0.28], [0.18, 0.31, 0.3 
2, 0.42, 0.28], [0.18, 0.31, 0.32, 0.42, 0.28], [0.18, 0.31, 0.32, 0.42, 0.28], [0.18, 0.31, 0.32, 0.42, 0.28], [0.18, 0 
.31, 0.32, 0.42, 0.28], [0.18, 0.31, 0.32, 0.42, 0.28], [0.18, 0.31, 0.32, 0.42, 0.28], [0.18, 0.31, 0.32, 0.42, 0.28], 
[0.18, 0.34, 0.71, 0.71, 0.9]] 

進入一個類似的列表。我希望這爲R^n工作,但1到4是我的測試數據。

+2

你能表現出而不是試圖用言語解釋? – CoryKramer 2014-10-16 23:16:48

+0

就拿這個名單:[0.39999999999999997],[2.145454545454545],[0.39999999999999997],[0.39999999999999997],[0.399 99999999999997],[2.145454545454545],[2.145454545454545],[2.145454545454545],[2.145454545454545],[2.145454545454545] ,[ 2.145454545454545],[2.145454545454545],[0.39999999999999997],[0.39999999999999997],[0.39999999999999997],[2.1454 54545454545],[2.145454545454545],[2.145454545454545]]並輸出[0,1,0,0,0,1,1, 1,1,1,1,1,0,0,0,1,1,1] – khaannn 2014-10-16 23:47:20

+0

請使用該信息編輯您的文章。如果將其粘貼到評論區域,所有格式都將丟失。 – CoryKramer 2014-10-16 23:48:08

回答

1

如果你想要做什麼,我覺得你想要做的事:

lst = ["A","A","B","C","A"] 
values = {} 

i = 0 
for value in lst: 
    if value[0] in values: continue 
    values[value[0]] = i 
    i += 1 

lst2 = [values[n[0]] for n in lst] 

輸出:

>>> print(lst2) 
[0, 0, 1, 2, 0] 

*編輯爲每個操作的編輯

+0

這適用於我發佈的1維數據,但是有沒有辦法可以推廣到n維?我寫了一個k-means聚類算法(對於一個類),它輸出分配給匹配輸入向量位置的聚類。 – khaannn 2014-10-17 00:07:38

+0

@khaannn這是超級不清楚你想在這裏完成什麼。您確實需要爲您的輸入提供一組預期的輸出。你試過什麼了?你期待'[[1,2,3],[1,2,3]]'返回'[0,0]'還是'[[0,1,2],[0,1,2]] '等等? – 2014-10-17 16:13:22

相關問題