在具有词典的python中对嵌套列表进行排序

时间:2018-02-23 18:30:39

标签: python python-3.x

我有以下给定格式的列表:

{'DetectedText': 'RAZZAQ', 'Type': 'WORD', 'Id': 11, 'ParentId': 1, 'Confidence': 98.4175796508789, 'Geometry': {'BoundingBox': {'Width': 0.13082271814346313, 'Height': 0.07997149974107742, 'Left': 0.05101994425058365, 'Top': 0.03755004703998566}, 'Polygon': [{'X': 0.050936006009578705, 'Y': 0.03812778368592262}, {'X': 0.1816418319940567, 'Y': 0.038123928010463715}, {'X': 0.18164214491844177, 'Y': 0.11644938588142395}, {'X': 0.05093633010983467, 'Y': 0.11645323783159256}]}}
{'DetectedText': '123456', 'Type': 'WORD', 'Id': 10, 'ParentId': 0, 'Confidence': 99.44573974609375, 'Geometry': {'BoundingBox': {'Width': 0.1333530694246292, 'Height': 0.0828823447227478, 'Left': 0.7199195027351379, 'Top': 0.028442291542887688}, 'Polygon': [{'X': 0.7201741337776184, 'Y': 0.028222769498825073}, {'X': 0.8533148169517517, 'Y': 0.03051863983273506}, {'X': 0.8531182408332825, 'Y': 0.11106494814157486}, {'X': 0.7199775576591492, 'Y': 0.10876907408237457}]}}
{'DetectedText': 'ABDUL', 'Type': 'WORD', 'Id': 12, 'ParentId': 2, 'Confidence': 98.67652893066406, 'Geometry': {'BoundingBox': {'Width': 0.11256816983222961, 'Height': 0.074801966547966, 'Left': 0.04738632217049599, 'Top': 0.14951731264591217}, 'Polygon': [{'X': 0.04746600240468979, 'Y': 0.1498059332370758}, {'X': 0.1596333384513855, 'Y': 0.15002737939357758}, {'X': 0.15961268544197083, 'Y': 0.2239050269126892}, {'X': 0.04744535684585571, 'Y': 0.22368358075618744}]}}
{'DetectedText': 'BED:', 'Type': 'WORD', 'Id': 13, 'ParentId': 2, 'Confidence': 98.10992431640625, 'Geometry': {'BoundingBox': {'Width': 0.08710028976202011, 'Height': 0.07596106827259064, 'Left': 0.6397669911384583, 'Top': 0.14786238968372345}, 'Polygon': [{'X': 0.6399385929107666, 'Y': 0.14720818400382996}, {'X': 0.7270201444625854, 'Y': 0.14760403335094452}, {'X': 0.7269713878631592, 'Y': 0.22334450483322144}, {'X': 0.6398898363113403, 'Y': 0.22294865548610687}]}}
{'DetectedText': 'G3/26', 'Type': 'WORD', 'Id': 14, 'ParentId': 2, 'Confidence': 99.2603759765625, 'Geometry': {'BoundingBox': {'Width': 0.11345399171113968, 'Height': 0.08469167351722717, 'Left': 0.7426034212112427, 'Top': 0.14282099902629852}, 'Polygon': [{'X': 0.7428475022315979, 'Y': 0.14198169112205505}, {'X': 0.8559787273406982, 'Y': 0.14304040372371674}, {'X': 0.8558661937713623, 'Y': 0.2279675006866455}, {'X': 0.7427350282669067, 'Y': 0.22690877318382263}]}}

我想根据以下内容对列表进行排序:

(text['Geometry']['Polygon'][0]['Y']) and if both are same , then for 
(text['Geometry']['Polygon'][0]['X'])

我尝试的脚本是:

textL.sort(key = lambda x: x['Geometry']['Polygon']['0']['Y'])

假设列表存储在textL中,但它给了我错误。

  

文件" detect-text.py",第20行,in       textD.sort(key = lambda x:x [' Geometry'] [' Polygon'] [' 0'] [' Y'] )TypeError:list indices必须是整数或切片,而不是str

任何帮助都将受到高度赞赏。

1 个答案:

答案 0 :(得分:3)

而不是整数0,而是传递"0",这会因列表崩溃而崩溃,因为__getitem__期望索引的整数:

textL.sort(key = lambda x: (x['Geometry']['Polygon'][0]['Y'], x['Geometry']['Polygon'][0]['X']))