我有一个执行以下操作的脚本
问题是源数据中的某些值为null,这会抛出TypeError
。我试图抛出一个条件,但它仍然试图减去Nones。
以下是包含一些示例数据的代码:
eLinks = {'726122193.0': [1310, 1315, 1320, 1325, 1330, 1335, 1340, 1310, 1315, 1320, 1325, 1330, 1335, 1340, 1310, 1315, 1320, 1325, 1330, 1335, 1340, 1310, 1315, 1320, 1325, 1330, 1335, 1340, 1310, 1315, 1320, 1325, 1330, 1335, 1340, 1310, 1315, 1320, 1325, 1330, 1335, 1340, 1310, 1315, 1320, 1325, 1330, 1335, 1340, 1310, 1315, 1320, 1325, 1330, 1335, 1340, 1310, 1315, 1320, 1325, 1330, 1335, 1340, 1310, 1315, 1320, 1325, 1330, 1335, 1340, 1310, 1315, 1320, 1325, 1330, 1335, 1340, 1310, 1315, 1320, 1325, 1330, 1335, 1340, 1310, 1315, 1320, 1325, 1330, 1335, 1340], '23607015.0': [None, None, None, 90, 95, 130, 2070, None, None, None, 580, 585, 610, 615, 2355, 2360, 1945, 1950, 125, 130, None, None, None, None, None, None, 90, 95, 130, 2070, None, None, None, 580, 585, 610, 615, 2355, 2360, 1945, 1950, 125, 130, None, None, None, None, None, None, 90, 95, 130, 2070, None, None, None, 580, 585, 610, 615, 2355, 2360, 1945, 1950, 125, 130, None, None, None, None, None, None, 90, 95, 130, 2070, None, None, None, 580, 585, 610, 615, 2355, 2360, 1945, 1950, 125, 130, None, None, None, None, None, None, 90, 95, 130, 2070, None, None, None, 580, 585, 610, 615, 2355, 2360, 1945, 1950, 125, 130, None, None, None, None, None, None, 90, 95, 130, 2070, None, None, None, 580, 585, 610, 615, 2355, 2360, 1945, 1950, 125, 130, None, None, None, None, None, None, 90, 95, 130, 2070, None, None, None, 580, 585, 610, 615, 2355, 2360, 1945, 1950, 125, 130, None, None, None, None, None, None, 90, 95, 130, 2070, None, None, None, 580, 585, 610, 615, 2355, 2360, 1945, 1950, 125, 130, None, None, None, None, None, None, 90, 95, 130, 2070, None, None, None, 580, 585, 610, 615, 2355, 2360, 1945, 1950, 125, 130, None, None, None, None, None, None, 90, 95, 130, 2070, None, None, None, 580, 585, 610, 615, 2355, 2360, 1945, 1950, 125, 130, None, None, None, None, None, None, 90, 95, 130, 2070, None, None, None, 580, 585, 610, 615, 2355, 2360, 1945, 1950, 125, 130, None, None, None, None, None, None, 90, 95, 130, 2070, None, None, None, 580, 585, 610, 615, 2355, 2360, 1945, 1950, 125, 130, None, None, None, None, None, None, 90, 95, 130, 2070, None, None, None, 580, 585, 610, 615, 2355, 2360, 1945, 1950, 125, 130, None, None, None]}
eOut = {}
for key, lis in eLinks.iteritems():
eCheck = []
sLis = sorted(lis)
for i, _ in enumerate(sLis[:-1]):
if i is not None:
dif = sLis[i+1] - sLis[i]
if dif > 20:
eCheck.append(dif)
eOut[key] = eCheck
答案 0 :(得分:1)
你显然不是要检查
if i is not None:
i
永远不会None
(它来自enumerate
),而您要确保sLis[i+1]
和sLis[i]
都不是{{1} }}
立即修正错误是用以下内容替换上述行:
None
更清洁的版本是这样的:
if sLis[i+1] is not None and sLis[i] is not None:
答案 1 :(得分:1)
您可以在排序后切除None
值,然后不必担心在内循环中识别None
:
eOut = {}
for key, lis in eLinks.iteritems():
eCheck = []
sLis = sorted(lis)
sLis = sLis[sLis.count(None):]
for i, _ in enumerate(sLis[:-1]):
dif = sLis[i+1] - sLis[i]
if dif > 20:
eCheck.append(dif)
eOut[key] = eCheck
根据您的示例数据,eOut
变为:
{'23607015.0': [30, 450, 25, 1330, 120, 285]}