【发布时间】:2019-08-05 22:04:12
【问题描述】:
maxScore = functools.reduce(lambda x, y: max(x['score'], y['score']), data)
print(maxScore)
TypeError Traceback (most recent call last)
<ipython-input-110-01bfe99b000b> in <module>()
----> 1 maxScore = functools.reduce(lambda x, y: max(x['score'], y['score']), data)
2 print(maxScore)
<ipython-input-110-01bfe99b000b> in <lambda>(x, y)
----> 1 maxScore = functools.reduce(lambda x, y: max(x['score'], y['score']), data)
2 print(maxScore)
TypeError: string indices must be integers
数据是:
{'action': 'Violations were cited in the following area(s).',
'bbl': '4113240001',
'bin': '4244445',
'boro': 'Queens',
'building': '22315',
'camis': '41699792',
'census_tract': '059600',
'community_board': '413',
'council_district': '27',
'critical_flag': 'Y',
'cuisine_description': 'Caribbean',
'dba': "MERITTA'S JAMAICAN AND AMERICAN RESTAURANT",
'grade': 'A',
'grade_date': '2019-04-29T00:00:00.000',
'inspection_date': '2019-04-29T00:00:00.000',
'inspection_type': 'Cycle Inspection / Re-inspection',
'latitude': '40.694248351255',
'longitude': '-73.737295424635',
'nta': 'QN33',
'phone': '7185270136',
'record_date': '2019-08-04T06:01:16.000',
'score': '10',
'street': 'LINDEN BOULEVARD',
'violation_code': '04L',
'violation_description': "Evidence of mice or live mice present in facility's food and/or non-food areas.",
'zipcode': '11411'},
{'action': 'Violations were cited in the following area(s).',
'bbl': '4007000047',
'bin': '4012143',
'boro': 'Queens',
'building': '44-01',
'camis': '50001785',
'census_tract': '014700',
'community_board': '401',
'council_district': '22',
'critical_flag': 'N',
'cuisine_description': 'Delicatessen',
'dba': 'LA LUNA CAFE',
'grade': 'A',
'grade_date': '2018-12-03T00:00:00.000',
'inspection_date': '2018-12-03T00:00:00.000',
'inspection_type': 'Cycle Inspection / Re-inspection',
'latitude': '40.762333015514',
'longitude': '-73.911670635234',
'nta': 'QN70',
'phone': '7186060094',
'record_date': '2019-08-04T06:01:16.000',
'score': '13',
'street': '30TH AVE',
'violation_code': '08C',
'violation_description': 'Pesticide use not in accordance with label or applicable laws. Prohibited chemical used/stored. Open bait station used.',
'zipcode': '11103'},
{'action': 'Violations were cited in the following area(s).',
'bbl': '5036170001',
'bin': '5052234',
'boro': 'Staten Island',
'building': '2333',
'camis': '50048016',
'census_tract': '012200',
'community_board': '502',
'council_district': '50',
'critical_flag': 'Y',
'cuisine_description': 'Asian',
'dba': 'SIMPLE ASIA',
'grade': 'A',
'grade_date': '2016-11-01T00:00:00.000',
'inspection_date': '2016-11-01T00:00:00.000',
'inspection_type': 'Pre-permit (Operational) / Re-inspection',
'latitude': '40.573990387537',
'longitude': '-74.106221480135',
'nta': 'SI45',
'phone': '9178252981',
'record_date': '2019-08-04T06:01:16.000',
'score': '10',
'street': 'HYLAN BLVD',
'violation_code': '06F',
'violation_description': 'Wiping cloths soiled or not stored in sanitizing solution.',
'zipcode': '10306'},
{'action': 'Establishment Closed by DOHMH. Violations were cited in the following area(s) and those requiring immediate action were addressed.',
'bbl': '2057060001',
'bin': '2083167',
'boro': 'Bronx',
'building': '249',
'camis': '41690532',
'census_tract': '028900',
'community_board': '208',
'council_district': '11',
'critical_flag': 'Y',
'cuisine_description': 'American',
'dba': 'KINGSBRIDGE DONUT SHOP',
'inspection_date': '2019-07-25T00:00:00.000',
'inspection_type': 'Cycle Inspection / Re-inspection',
'latitude': '40.879751741296',
'longitude': '-73.906454907184',
'nta': 'BX29',
'phone': '3473464171',
'record_date': '2019-08-04T06:01:16.000',
'score': '68',
'street': 'WEST 231 STREET',
'violation_code': '05F',
'violation_description': 'Insufficient or no refrigerated or hot holding equipment to keep potentially hazardous foods at required temperatures.',
'zipcode': '10463'}
【问题讨论】:
-
什么是
data?显示minimal reproducible example。它似乎是字符串列表,而不是字典。此外,这看起来不像您期望的那样工作。max将返回一个数字,这将使减少的累加器成为一个数字,如果x是一个数字,x['score']没有意义。你不应该尝试索引累加器。 -
数据来自这个链接:data.cityofnewyork.us/Health/…
标签: python json python-3.x