我正在尝试编写以这种格式接收数据的代码
数据示例:
[['12319825', '39274', {'pH': 8.1}], ['12319825', '39610', {'pH': 7.27}],
['12319825', '39638', {'pH': 7.87, 'Escherichia coli': 25.0}],
['12319825', '39770', {'pH': 7.47, 'Escherichia coli': 27.0}],
['12319825', '39967', {'pH': 8.36}], ['12319825', '39972', {'pH': 8.42}],
['12319825', '39987', {'pH': 8.12, 'Escherichia coli': 8.0}],
['12319825', '40014', {'pH': 8.12}], ['12319825', '40329',{'pH': 8.45}],
['12319825', '40658', {'pH': 8.35, 'Escherichia coli': 6.3}],
['12319825', '40686', {'pH': 8.17}],
['12319825', '40714', {'pH': 8.13}], ['12319825', '40732', {'pH': 8.4}],
['12319825', '40809', {'pH': 8.42}],
['12319825', '40827', {'pH': 8.46}],
['12319825', '41043', {'pH': 8.42, 'Escherichia coli': 170.0}],
['12319825', '41071', {'pH': 8.24, 'Escherichia coli': 92.0}],
['12319825', '41080', {'pH': 8.4}],
['12319825', '41101', {'pH': 8.36, 'Escherichia coli': 560.0}], ['12319825', '41134', {'pH': 8.67}]]
并将返回一个字典,其中键是污染物(在本例中,pH或大肠杆菌),值是我所称的DateList。日期列表将是每个数据点的列表元组,格式为(日期,T/F)。如果值超出给定范围或超过给定值(取决于条件的类型),则布尔值将为真
rangeCriteria={'pH':(5.0,9.0)}
convCriteria={'Echerichia coli':320)
现在,当我运行这个代码时,每个字典都有
def testLocationForConv(DataFromLocation):
#checks if a pollutant is outside of acceptable values.
#A dictionary is created where each pollutant has a cooresponding list of tuples
#with the date and a corresponding boolean to say if it is in or out of
#the criteria (true if out false if in)
#It handles when the criteria is a minimum or range rather than a
#maximum
dateList=[]
impairedList=[]
overDict=dict()
for date in DataFromLocation:
for pollutant in date[2]:
if pollutant in conventionalCriteriaList:
dateList.append((date[1],date[2][pollutant]>convCriteria[pollutant]))
overDict[pollutant]=dateList
if pollutant in rangeCriteria:
overDict[pollutant]=dateList
dateList.append((date[1], (not (float(date[2][pollutant])>rangeCriteria[pollutant][0] and float(date[2][pollutant])<rangeCriteria[pollutant][1])) ))
#if pollutant in minCriteriaList:
# overDict[pollutant]=dateList
# dateList.append((date[1],date[2][pollutant]<minCriteria[pollutant])
else:
pass
print overDict
现在,这两种污染物的数据点被添加到字典中,得到以下结果。你知道吗
{'pH': [('39274', False), ('39610', False), ('39638', False),
('39638', False), ('39770', False), ('39770', False), ('39967', False),
('39972', False), ('39987', False), ('39987', False), ('40014', False),
('40329', False), ('40658', False), ('40658', False), ('40686', False),
('40714', False), ('40732', False), ('40809', False), ('40827', False),
('41043', False), ('41043', False), ('41071', False), ('41071', False),
('41080', False), ('41101', False), ('41101', True), ('41134', False)],
'Escherichia coli': [('39274', False), ('39610', False), ('39638', False),
('39638', False), ('39770', False), ('39770', False), ('39967', False),
('39972', False), ('39987', False), ('39987', False), ('40014', False),
('40329', False), ('40658', False), ('40658', False), ('40686', False),
('40714', False), ('40732', False), ('40809', False), ('40827', False),
('41043', False), ('41043', False), ('41071', False), ('41071', False),
('41080', False), ('41101', False), ('41101', True), ('41134', False)]}
现在我输入了这个问题,我意识到问题是我在迭代日期,然后是污染物,但是我想要一个编译日期的列表,但是对于污染物是独立的。我该如何列一个这样的单子并把它加到字典里去呢?你知道吗
我会退一步想想你的方法。你让事情变得更难了。首先,数据:
当你的布尔条件有点复杂的时候,你应该给它们自己的函数,如果只是为了可读性。在这里,我会更进一步,还将它们添加到一本字典中,其中的关键是相应的污染物,这将使你的生活非常轻松!你知道吗
关键的问题是你使用的是一个单列表,但是你真的需要两个。使用两个空列表初始化字典,因为您知道您将附加到它们。你知道吗
最后,对数据进行迭代:
最后,输出:
相关问题 更多 >
编程相关推荐