assign a retrievable unique ID to a changing lambda list in python?
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: here, how to return a lambda list in simple line(s):
[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:
suppose I defined another function 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>)]
this combination with different a sequence compare with last one :
ab,ac,ad,bc,bd,cd
=================
ab,ac,bc
But I want to keep all possible item with an unque ID, it means no matter how the
l= [a,b,c,d]
or
l= [b,a,c,d]
pr
l= [a,b,e,d]
Take "ac" for example: the "ac" and other possible item always with an unique ID bind then I can access "ac" with that unique ID. I think it is like to create an extendable hash table for each item.
So, is it possible to assign an int ID or a "HASH" to the lambda item? I also want this mapping relationship should be able to store in disk as a file and can be retrieved later.
Thanks for any idea.
sample to explain 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
res开发者_Python百科ult2, 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:
just use set
to avoid dicts?
They are works for me.
精彩评论