2010-11-24 56 views
0
def a(p): return p + 1 

def b(p): return p + 2 

def c(p): return p + 3 

l= [a,b,c] 

import itertools 
ll = itertools.combinations(l, 2) 

[x for x in ll] 
[(<function a at 0x00CBD770>, <function b at 0x00CBD7F0>), 
(<function a at 0x00CBD770>, <function c at 0x00BB27F0>), 
(<function b at 0x00CBD7F0>, <function c at 0x00BB27F0>)] 

Q1:這裏,如何在簡單的線條(S)返回一個lambda列表:分配一個可檢索的唯一ID到Python中更改的lambda列表?

[a(b(1)), # not the result of a(b(1)), but just a lambda object 
a(c(1)), # also items may more than 2 here if itertools.combinations(l, 4) 
b(c(1))] 

Q2:

假設我定義的另一個功能d

def d(p): return p + 4 

l= [a,b,c,d] 
ll = itertools.combinations(l, 2) 

[(<function a at 0x00CBD770>, <function b at 0x00CBD7F0>), 
(<function a at 0x00CBD770>, <function c at 0x00BB27F0>), 
(<function a at 0x00CBD770>, <function d at 0x00CBDC70>), 
(<function b at 0x00CBD7F0>, <function c at 0x00BB27F0>), 
(<function b at 0x00CBD7F0>, <function d at 0x00CBDC70>), 
(<function c at 0x00BB27F0>, <function d at 0x00CBDC70>)] 

這個組合與不同的序列比較與最後一個:

ab,ac,ad,bc,bd,cd 
================= 
ab,ac,bc 

但我希望將所有可能的項目有unque ID,這意味着不管如何

l= [a,b,c,d] 

l= [b,a,c,d] 

PR

l= [a,b,e,d] 

「ac」 for ex充足:「ac」和其他可能的項目始終與唯一的ID綁定,然後我可以訪問「ac」與該唯一的ID。我想這就像爲每個項目創建一個可擴展的哈希表。

那麼,是否可以給lambda項分配一個int ID或「HASH」?我也希望這種映射關係應該能夠作爲文件存儲在磁盤中,並且可以在以後檢索。

謝謝你的任何想法。

樣本解釋Q2

===================== 
l= [a,b,c,d] 
func_combos = itertools.combinations(l, 2) 
compositions = [compose(f1, f2) for f1, f2 in func_combos] 

[compositions[x](100) for x in compositions] # take very long time to finish 
[result1, 
result2, 
result3, 
... 
] 

======== three days later on another machine ====== 
l= [a,c,b,e,f,g,h] 
[compositions[x](100) for x in compositions] # take very long time to finish 

[newresult1, 
newresult2, 
newresult3, 
... 
] 

but wait: here we can saving time: take "ac" for example: 

[result1, tag 
result2, tag_for_ac_aka_uniqueID_or_hash 
result3, tag 
... 
] 

we just need to check if the "ac" tag exists we can reduce the calculation: 
if hash_of(ac) in list(result.taglist): 
    copy result to new result: 
+1

當你開始談論哈希時,我不清楚你想要什麼。你能給出一個例子,說明你想要一個給定函數的唯一ID嗎? – aaronasterling 2010-11-24 06:54:21

+0

問題已更新,謝謝 – user478514 2010-11-24 07:29:35

回答

0

只是用set避免http://stardict.sourceforge.net/Dictionaries.php下載?

他們是我的作品。