2016-06-15 26 views
0

我正在尋找使用蒙特卡羅模擬的參數組合的可能性。 我有4個參數,每個可以有大約250個值。 我使用某種概率分佈函數爲每個參數隨機生成了250,000個場景。 我現在想找出哪些參數組合最有可能發生。 爲了實現這一點,我已經開始篩選出我的250,000個隨機生成的樣本中的任何重複項,以減少列表的長度。 然後我通過這個縮小列表迭代並檢查每個場景在原始250,000長列表中出現的次數。大型嵌套列表的快速排序

我有250,000項包含列表,這樣一個大名單:

a = [[1,2,5,8],[1,2,5,8],[3,4,5,6],[3,4,5,7],....,[3,4,5,7]]# len(a) is equal to 250,000 

我想找到有我的名單隻發生一次在每個列表的快捷和有效的方式。

最終目標是計算列表a中每個列表的出現次數。

到目前爲止我有:

'''Removing duplicates from list a and storing this as a new list temp''' 
b_set = set(tuple(x) for x in a) 
temp = [ list(x) for x in b_set ] 
temp.sort(key = lambda x: a.index(x))  

''' I then iterate through each of my possible lists (i.e. temp) and count how many times they occur in a''' 
most_likely_dict = {} 
for scenario in temp: 
    freq = list(scenario_list).count(scenario) 
    most_likely_dict[str(scenario)] = freq 

此刻它需要一個很好的15分鐘表演......如何把它轉換成一個幾秒鐘的任何建議,將不勝感激!

+0

什麼是你想用此方法解決實際問題呢?很有可能如果你每次做某些次優時都需要重新排列列表。你能提供一些背景嗎? – jonrsharpe

+0

我在問題開始時添加了一些上下文。我基本上想知道大列表中的每個列表會發生多少次。每個嵌套列表代表了我的問題可能的參數組合,而不是模擬我想要關注的4個最可能的組合。 – Sorade

+1

你爲什麼不做['Counter(map(tuple,a))'](https://docs.python.org/2/library/collections.html#collections.Counter)?這會給你例如'{(1,2,5,8):2,...}',而不需要排序。 – jonrsharpe

回答

1

你可以拿出排序部分,最後的結果是,這將是無序的在任何情況下字典,然後使用字典理解:

>>> a = [[1,2],[1,2],[3,4,5],[3,4,5], [3,4,5]] 
>>> a_tupled = [tuple(i) for i in a] 
>>> b_set = set(a_tupled) 
>>> {repr(i): a_tupled.count(i) for i in b_set} 
{'(1, 2)': 2, '(3, 4, 5)': 3} 

調用你的元組list將增加更多的開銷,但你可以,如果你想

>>> {repr(list(i)): a_tupled.count(i) for i in b_set} 
{'[3, 4, 5]': 3, '[1, 2]': 2} 

或者只是使用一個Counter

>>> from collections import Counter 
>>> Counter(tuple(i) for i in a) 
0
{str(item):a.count(item) for item in a} 

輸入:

a = [[1,2,5,8],[1,2,5,8],[3,4,5,6],[3,4,5,7],[3,4,5,7]] 

輸出:

{'[3, 4, 5, 6]': 1, '[1, 2, 5, 8]': 2, '[3, 4, 5, 7]': 2} 
+1

請注意,這是'O(n^2)',因爲'count'每次迭代整個列表。 – jonrsharpe