我正在寫的函數有一個列表列表。我想遍歷列表,獲取列表中的第一個元素和所有匹配的元素,並將其他數組中的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是我的測試數據。
你能表現出而不是試圖用言語解釋? – CoryKramer 2014-10-16 23:16:48
就拿這個名單:[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
請使用該信息編輯您的文章。如果將其粘貼到評論區域,所有格式都將丟失。 – CoryKramer 2014-10-16 23:48:08