1
我需要根據鍵是否大於或小於給定值將浮點列表或不同長度的(命名)元組列表分組。擴展分組代碼以處理更通用的輸入
例如,給定的2小於1的權力的列表,和截斷的列表:
twos = [2**(-(i+1)) for i in range(0,10)]
cutoffs = [0.5, 0.125, 0.03125]
然後起作用
split_into_groups(twos, cutoffs)
應返回
[[0.5], [0.25, 0.125], [0.0625, 0.03125], [0.015625, 0.0078125, 0.00390625, 0.001953125, 0.0009765625]]
I」已經實現了這樣的功能:
def split_by_prob(items, cutoff, groups, key=None):
for k,g in groupby(enumerate(items), lambda (j,x): x<cutoff):
groups.append((map(itemgetter(1),g)))
return groups
def split_into_groups(items, cutoffs, key=None):
groups = items
final = []
for i in cutoffs:
groups = split_by_prob(groups,i,[],key)
if len(groups) > 1:
final.append(groups[0])
groups = groups.pop()
else:
final.append(groups[0])
return final
final.append(groups)
return final
,這些目前通過測試的是:
>>> split_by_prob(twos, 0.5, [])
[[0.5], [0.25, 0.125, 0.0625, 0.03125, 0.015625, 0.0078125, 0.00390625, 0.001953125, 0.0009765625]]
>>> split_into_groups(twos, cutoffs)
[[0.5], [0.25, 0.125], [0.0625, 0.03125], [0.015625, 0.0078125, 0.00390625, 0.001953125, 0.0009765625]]
>>> split_into_groups(twos, cutoffs_p10)
[[0.5, 0.25, 0.125], [0.0625, 0.03125, 0.015625], [0.0078125, 0.00390625, 0.001953125], [0.0009765625]]
凡cutoffs_p10 = [10**(-(i+1)) for i in range(0,5)]
我可以直截了當地通過改變
這個延伸到形式items = zip(range(0,10), twos)
的元組的列表
def split_by_prob(items, cutoff, groups, key=None):
for k,g in groupby(enumerate(items), lambda (j,x): x<cutoff):
groups.append((map(itemgetter(1),g)))
return groups
到
def split_by_prob(items, cutoff, groups, key=None):
for k,g in groupby(enumerate(items), lambda (j,x): x[1]<cutoff):
groups.append((map(itemgetter(1),g)))
return groups
如何去通過添加一個關鍵擴展原始方法默認爲浮動(或者整形等),但一個可以處理的元組和namedtuples的名單?
例如像:
split_into_groups(items, cutoffs, key=items[0])
將返回
[[(0,0.5)], [(1,0.25), (2,0.125)], [(3,0.0625), (4,0.03125)], [(5,0.015625), (6,0.0078125), (7,0.00390625), (8,0.001953125), (9,0.0009765625)]]
@TomKealy最後,我在你的'key'問題中包含了'key'回答。 –
感謝你的回答!你能解釋一下你爲什麼選擇了你所做的路線,也許爲什麼我寫的代碼不能擴展到做我需要的東西? –
@TomKealy我這樣做是因爲貧窮的原因:我喜歡我的方式,我沒有正確閱讀你真正要求的是什麼('鑰匙'部分),並開始重寫。出於實際的原因,它允許我清楚地分割找到時隙的概念,以及在分組數據中查找元素以用於決定要使用的時隙的概念。無論如何,你的代碼看起來質量很好,我喜歡它,但我懶得鑽研所有的細節。 –