简体   繁体   English

我将如何切片字典中的键值

[英]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.

相关问题 如果按字典值对字典进行排序,我该如何按值对字典进行排序? - How would I sort this Dictionary by value then if the same sort it by key value? 如何从值中找到字典中的键? - How would I find the key in a dictionary from a value? 如何将dict {key:value}放入字典中的指定键中,以便在对value进行计数后成为{key:{key:value}} - How would I put a dict {key: value} in it's designated key in a dictionary so that it is {key: {key: value}} after counting value 如果字典键包含某个单词,我该如何删除它? - If a dictionary key contains a certain word, how would I remove it? 我将如何对字典的第二个键的值进行排序? - How would I sort the values of the dictionary the second key? 为什么将key:value对不添加到Dictionary? - Why would a key:value pair not be added to a Dictionary? 我想知道如何将 map 字典的值列表设为 dataframe - I would like to know how to map dictionary with list of value to dataframe 我如何将字典 append 作为现有字典键的另一个值 - How do I append a dictionary as another value to an existing dictionary key 我有一个值列表,我想将 append 键值对迭代到 python 字典 - i've a list of values and I would like to iteratively append key-value pair to a python dictionary 如果键是一个元组,如何在字典中访问键的值? - How can I access the value of a key in a dictionary if the key is a tuple?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM