[英]How would I slice a value of a key in a dictionary
hello I'm new to coding and I have to Define a function named filterByMonth with two parameters.您好,我是编码新手,我必须使用两个参数定义一个名为 filterByMonth 的 function。 The first argument passed to the function should be a list of dictionaries (the data), the second a number (representing a month, 1 through 12).
传递给 function 的第一个参数应该是字典列表(数据),第二个参数是数字(代表一个月,从 1 到 12)。 This function must return a list of all the dictionaries from the input list whose 'issued' date is in the indicated month.
此 function 必须从输入列表中返回所有字典的列表,其“发布”日期在指定月份。 'slice' a string to more easily extract the relevant portion of a date from a string.
'slice' 一个字符串,以便更容易地从字符串中提取日期的相关部分。 Sample function call: filterByMonth(data,9)
示例 function 调用:filterByMonth(data,9)
With a list of dictionaries I need to slice the month which is a part of the value for the key 'issued' which is the third key in the dictionaries.使用字典列表,我需要对月份进行切片,该月份是键“已发布”的值的一部分,该键是字典中的第三个键。
data = [
{'apno': 'FEND19-9487084', 'aptype': 'FENDRIVE', 'issued': '2019-09-05T00:00:00.000', 'stname': '129 PARKSIDE CT', 'city': 'BUFFALO', 'state': 'NY', 'zip': '14214', 'applicant': 'PETER CICERO', 'fees': '150', 'value': '3500', 'plans': '0', 'sbl': '0795300004001000', 'landuse': '411', 'inspector': 'ANDREW BLERSCH', 'expdate': '2020-03-05T00:00:00.000', 'descofwork': 'REMOVE EXISTING DRIVEWAY AND REPLACE IN KIND WITH CONCRETE TO CODE ON SOUTH / RIGHT SIDE OF STRUCTURE TO CODE - SURVEY SCANNED', 'location_1': {'latitude': '42.95116080935555', 'longitude': '-78.83406536395538', 'human_address': '{"address": "", "city": "", "state": "", "zip": ""}'}, 'latitude': '42.95116080935555', 'longitude': '-78.83406536395538', 'council_district': 'UNIVERSITY', 'police_district': 'District D', 'census': '45', 'census_block_group': '1', 'census_block': '1010', 'neighborhood': 'UNKNOWN', ':@computed_region_fk4y_hpmh': '5', ':@computed_region_eziv_p4ck': '64', ':@computed_region_tmcg_v66k': '8', ':@computed_region_kwzn_pe6v': '18', ':@computed_region_uh5x_q5mi': '88', ':@computed_region_dwzh_dtk5': '1573', ':@computed_region_b3rm_3c8a': '37', ':@computed_region_xbxg_7ifr': '24', ':@computed_region_urdz_b6n8': '7'},
{'apno': 'SWIM19-9485898', 'aptype': 'SWIM POOL', 'issued': '2019-08-19T00:00:00.000', 'stname': '341 NORWALK', 'city': 'BUFFALO', 'state': 'NY', 'zip': '14216', 'applicant': 'MS CHRISTINE SALAMONE', 'fees': '75', 'value': '500', 'plans': '0', 'sbl': '0785000006033000', 'landuse': '210', 'inspector': 'ANDREW BLERSCH', 'expdate': '2020-02-19T00:00:00.000', 'descofwork': 'INSTALLATION OF AN ABOVE GROUND SWIMMING POOL SUBMITTED THROUGH IDT', 'location_1': {'latitude': '42.95333872723409', 'longitude': '-78.85429233887896', 'human_address': '{"address": "", "city": "", "state": "", "zip": ""}'}, 'latitude': '42.95333872723409', 'longitude': '-78.85429233887896', 'council_district': 'DELAWARE', 'police_district': 'District D', 'census': '49', 'census_block_group': '1', 'census_block': '1000', 'neighborhood': 'UNKNOWN', ':@computed_region_fk4y_hpmh': '5', ':@computed_region_eziv_p4ck': '51', ':@computed_region_tmcg_v66k': '7', ':@computed_region_kwzn_pe6v': '5', ':@computed_region_uh5x_q5mi': '190', ':@computed_region_dwzh_dtk5': '944', ':@computed_region_b3rm_3c8a': '28', ':@computed_region_xbxg_7ifr': '25', ':@computed_region_urdz_b6n8': '2'},
]
def filterByMonth(dta,month):
result=[]
for x in dta:
for y in x:
for x['issued'] in y
if month== x[6]:
result.append(x)
return result
print(filterByMonth(data,9))
You could do this more easily with the datetime module like so您可以像这样使用 datetime 模块更轻松地做到这一点
from datetime import datetime
data = [
{'apno': 'FEND19-9487084', 'aptype': 'FENDRIVE', 'issued': '2019-09-05T00:00:00.000', 'stname': '129 PARKSIDE CT', 'city': 'BUFFALO', 'state': 'NY', 'zip': '14214', 'applicant': 'PETER CICERO', 'fees': '150', 'value': '3500', 'plans': '0', 'sbl': '0795300004001000', 'landuse': '411', 'inspector': 'ANDREW BLERSCH', 'expdate': '2020-03-05T00:00:00.000', 'descofwork': 'REMOVE EXISTING DRIVEWAY AND REPLACE IN KIND WITH CONCRETE TO CODE ON SOUTH / RIGHT SIDE OF STRUCTURE TO CODE - SURVEY SCANNED', 'location_1': {'latitude': '42.95116080935555', 'longitude': '-78.83406536395538', 'human_address': '{"address": "", "city": "", "state": "", "zip": ""}'}, 'latitude': '42.95116080935555', 'longitude': '-78.83406536395538', 'council_district': 'UNIVERSITY', 'police_district': 'District D', 'census': '45', 'census_block_group': '1', 'census_block': '1010', 'neighborhood': 'UNKNOWN', ':@computed_region_fk4y_hpmh': '5', ':@computed_region_eziv_p4ck': '64', ':@computed_region_tmcg_v66k': '8', ':@computed_region_kwzn_pe6v': '18', ':@computed_region_uh5x_q5mi': '88', ':@computed_region_dwzh_dtk5': '1573', ':@computed_region_b3rm_3c8a': '37', ':@computed_region_xbxg_7ifr': '24', ':@computed_region_urdz_b6n8': '7'},
{'apno': 'SWIM19-9485898', 'aptype': 'SWIM POOL', 'issued': '2019-08-19T00:00:00.000', 'stname': '341 NORWALK', 'city': 'BUFFALO', 'state': 'NY', 'zip': '14216', 'applicant': 'MS CHRISTINE SALAMONE', 'fees': '75', 'value': '500', 'plans': '0', 'sbl': '0785000006033000', 'landuse': '210', 'inspector': 'ANDREW BLERSCH', 'expdate': '2020-02-19T00:00:00.000', 'descofwork': 'INSTALLATION OF AN ABOVE GROUND SWIMMING POOL SUBMITTED THROUGH IDT', 'location_1': {'latitude': '42.95333872723409', 'longitude': '-78.85429233887896', 'human_address': '{"address": "", "city": "", "state": "", "zip": ""}'}, 'latitude': '42.95333872723409', 'longitude': '-78.85429233887896', 'council_district': 'DELAWARE', 'police_district': 'District D', 'census': '49', 'census_block_group': '1', 'census_block': '1000', 'neighborhood': 'UNKNOWN', ':@computed_region_fk4y_hpmh': '5', ':@computed_region_eziv_p4ck': '51', ':@computed_region_tmcg_v66k': '7', ':@computed_region_kwzn_pe6v': '5', ':@computed_region_uh5x_q5mi': '190', ':@computed_region_dwzh_dtk5': '944', ':@computed_region_b3rm_3c8a': '28', ':@computed_region_xbxg_7ifr': '25', ':@computed_region_urdz_b6n8': '2'}
]
def filterByMonth(data, month):
result = []
for item in data:
datestring = item['issued']
dt = datetime.strptime(datestring, '%Y-%m-%dT%H:%M:%S.%f')
if dt.month == month:
result.append(item)
return result
print(filterByMonth(data, 9))
and a more pythonic way would be this一个更蟒蛇的方式就是这样
def filterByMonth(data, month):
return [item for item in data if datetime.strptime(item['issued'], '%Y-%m-%dT%H:%M:%S.%f').month == month]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.