我如何编写此python函数

问题描述 投票:-2回答:2

python:使用四个参数定义一个名为filterInRange的函数。传递给函数的第一个参数应该是字典(数据)列表,第二个参数是字符串(字典键),第三个和第四个参数是数字,代表范围的低值和高值。此函数必须从输入列表中返回所有字典的列表,这些字典包含key:value对,其中low <= value

示例函数调用:filterInRange(data,'value',0,8000)

data = [

{'apno': 'REP19-9496002', 'aptype': 'REPAIR', 'issued': '2019-09-26T00:00:00.000', 'stname': '473 COLVIN', 'city': 'BUFFALO', 'state': 'NY', 'zip': '14216', 'applicant': 'AVA ROOFING & SIDING', 'licno': 'LTC12-10021580', 'lictype': 'LIGHT COMMERCIAL CONTRACTOR', 'fees': '110', 'value': '16700', 'plans': '0', 'sbl': '0784900004036000', 'landuse': '220', 'inspector': 'ANDREW BLERSCH', 'expdate': '2020-03-26T00:00:00.000', 'descofwork': 'Complete tear off of existing roof, install 1/2 inch plywood, install 6 feet of ice shield, install synthetic paper, install 50 years of Architectural shingles Landmark Certainteed, install ridge vent and new soffits', 'location_1': {'latitude': '42.953667097354455', 'longitude': '-78.8588683262867', 'human_address': '{"address": "", "city": "", "state": "", "zip": ""}'}, 'latitude': '42.953667097354455', 'longitude': '-78.8588683262867', 'council_district': 'DELAWARE', 'police_district': 'District D', 'census': '49', 'census_block_group': '1', 'census_block': '1003', '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': '468', ':@computed_region_b3rm_3c8a': '28', ':@computed_region_xbxg_7ifr': '25', ':@computed_region_urdz_b6n8': '2'},

{'apno': 'REP19-9495998', 'aptype': 'REPAIR', 'issued': '2019-09-26T00:00:00.000', 'stname': '14 DUANE', 'city': 'BUFFALO', 'state': 'NY', 'zip': '14214', 'applicant': 'DALEX CONSTR. INC (HIC, LTC)', 'licno': 'LTC11-538569', 'lictype': 'LIGHT COMMERCIAL CONTRACTOR', 'fees': '75', 'value': '6502', 'plans': '0', 'sbl': '0892700005016000', 'landuse': '210', 'inspector': 'ANDREW BLERSCH', 'expdate': '2020-03-26T00:00:00.000', 'descofwork': 'tear off and replacement on right side of main frame only.', 'location_1': {'latitude': '42.940486277980945', 'longitude': '-78.84340571259821', 'human_address': '{"address": "", "city": "", "state": "", "zip": ""}'}, 'latitude': '42.940486277980945', 'longitude': '-78.84340571259821', 'council_district': 'DELAWARE', 'police_district': 'District D', 'census': '45', 'census_block_group': '4', 'census_block': '4017', 'neighborhood': 'UNKNOWN', ':@computed_region_fk4y_hpmh': '5', ':@computed_region_eziv_p4ck': '64', ':@computed_region_tmcg_v66k': '7', ':@computed_region_kwzn_pe6v': '18', ':@computed_region_uh5x_q5mi': '71', ':@computed_region_dwzh_dtk5': '3256', ':@computed_region_b3rm_3c8a': '37', ':@computed_region_xbxg_7ifr': '24', ':@computed_region_urdz_b6n8': '2'}

]

def filterInRange(dta,key,lval,hval):
  for x in dta:
     for key,x[key] in x.items():
         x[key]= int(float(x[key])
              if (x[key]>= lval) and (x[key]< hval)
    return element for element in dta
print(filterInRange(data,'value', 0, 8000))

python function dictionary key
2个回答
0
投票
def filterInRange(dta, key, lval, hval):
    return [d for d in dta if lval <= float(d[key]) < hval]

def filterInRange(dta, key, lval, hval):
    return list(filter(lambda d: lval <= float(d[key]) < hval, dval))

0
投票

您无需遍历x.items()。您只需访问x[key]即可获取所需的字典元素。

您可以使用内置的filter功能。

def filterInRange(dta,key,lval,hval):
    return filter(lambda item: lval <= float(item[key]) < hval, dta)

如果您需要编写自己的循环:

def filterInRange(dta, key, lval, hval):
    result = []
    for item in dta:
        if lval <= float(item[key]) < hval:
            result.push(item)
    return result
© www.soinside.com 2019 - 2024. All rights reserved.