【问题标题】:How would I slice a value of a key in a dictionary我将如何切片字典中的键值
【发布时间】:2020-02-08 11:57:35
【问题描述】:

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

使用字典列表,我需要对月份进行切片,该月份是键“已发布”的值的一部分,该键是字典中的第三个键。

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

【问题讨论】:

  • 如果您在浏览器中搜索“Python 字符串切片教程”,您会发现可以比我们在此处管理的更好地解释这一点的参考资料。 Stack Overflow 并非旨在替代常规研究。当您诚实地尝试并发布您的代码时——Minimal, complete, verifiable example 在这里适用——那么我们随时准备提供帮助。

标签: python function dictionary key slice


【解决方案1】:

你可以像这样使用 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))

还有一种更 Pythonic 的方式

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

【讨论】:

  • 你是如何得到'%Y-%m-%dT%H:%M:%S.%f'的
  • 可以阅读strptime文档here%Y-%m-%dT%H:%M:%S.%f Year-month-dayTHour:Minute:Second.Millisecond
猜你喜欢
  • 2018-12-31
  • 2017-03-19
  • 1970-01-01
  • 2016-11-02
  • 2021-04-12
  • 2021-09-14
  • 2015-05-26
  • 2021-06-04
相关资源
最近更新 更多