在python中为可变的lambda列表分配一个可检索的唯一ID?

时间:2010-11-24 06:47:19

标签: python lambda uniqueidentifier

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:这里,如何在简单的行中返回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”为例:“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 个答案:

答案 0 :(得分:0)

只需使用set来避免双语吗?

它们适合我。