如何在字典中切分键的值

2024-09-30 06:22:08 发布

您现在位置:Python中文网/ 问答频道 /正文

你好,我是新来编码,我必须定义一个函数名为filterByMonth与两个参数。传递给函数的第一个参数应该是字典列表(数据),第二个参数是数字(表示一个月,从1到12)。此函数必须返回输入列表中“发布”日期在指定月份的所有词典的列表切分一个字符串,以便更容易地从字符串中提取日期的相关部分。函数调用示例:filterByMonth(data,9)

有了字典列表,我需要对month进行切片,它是键'issued'值的一部分,该键是字典中的第三个键。你知道吗

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))

Tags: city列表addresszipblockregionstatecensus
1条回答
网友
1楼 · 发布于 2024-09-30 06:22:08

使用这样的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))

还有一个更像Python的方法就是这样

def filterByMonth(data, month):
    return [item for item in data if datetime.strptime(item['issued'], '%Y-%m-%dT%H:%M:%S.%f').month == month]

相关问题 更多 >

    热门问题