您可以使用此作爲基礎(而不是把'11 0.24' 爲一個浮點數,我想你想適應幾小時/分鐘或分鐘/秒)...你可以使用defaultdict
有效地結合並減去匹配鍵。
只要你可以得到你的數據轉換成這樣的格式:
f1 = [
[11.24, 1],
[11.26, 2],
[11.27, 3],
[11.29, 5],
[11.30, 6]
]
f2 = [
[11.25, 1],
[11.26, 3],
[11.27, 4],
[11.31, 6],
[11.32, 7],
[11.33, 8]
]
然後:
from collections import defaultdict
from itertools import chain
dd = defaultdict(float)
for k, v in chain(
((b, a) for a, b in f1),
((b, -a) for a, b in f2)): # negate a
dd[k] += v
結果:
{1: -0.009999999999999787,
2: 11.26,
3: 0.009999999999999787,
4: -11.27,
5: 11.29,
6: -0.009999999999999787,
7: -11.32,
8: -11.33}
對於比賽只
個
matches = dict((k, v) for v, k in f1)
d2 = dict((k, v) for v, k in f2)
for k, v in matches.items():
try:
matches[k] = v - d2[k]
except KeyError as e:
del matches[k]
print matches
# {1: -0.009999999999999787, 3: 0.009999999999999787, 6: -0.009999999999999787}
這個特定情況下的輸出應該如何? – eumiro 2012-07-20 10:36:17