python - 只返回列表的最后一个元素?
问题描述
我正在使用一个 JSON 文件,我必须对其进行解析以获得“点”,如下所示(JSON 文件的片段)
test_json = [{u'imagePath': u'..\\jpg_images\\20200205-095417-373.jpg', u'shapes': [{u'shape_type': u'polygon', u'points': [[69.2162162162162, 314.5945945945946], [63.27027027027026, 324.3243243243243], [66.51351351351352, 328.10810810810807], [73.54054054054052, 318.9189189189189], [81.64864864864865, 321.08108108108104], [88.13513513513513, 314.5945945945946], [88.13513513513513, 299.4594594594594], [82.1891891891892, 290.8108108108108], [61.648648648648646, 292.4324324324324], [57.86486486486487, 303.2432432432432], [58.94594594594594, 314.05405405405406]],
u'flags': {}, u'group_id': None, u'label': u'anchor'}, {u'shape_type': u'polygon', u'points': [[244.35135135135135, 145.4054054054054], [247.05405405405406, 155.13513513513513], [252.45945945945942, 154.05405405405403], [249.75675675675672, 144.86486486486487], [258.94594594594594, 145.94594594594594], [261.64864864864865, 135.67567567567568], [258.94594594594594, 127.02702702702702], [249.2162162162162, 125.4054054054054], [235.7027027027027, 128.1081081081081], [229.2162162162162, 139.45945945945945], [233.54054054054052, 147.02702702702703]],
u'flags': {}, u'group_id': None, u'label': u'anchor'}, {u'shape_type': u'polygon', u'points': [[28.13513513513513, 1.6216216216216215], [28.13513513513513, 10.27027027027027], [40.02702702702703, 16.216216216216214], [53.0, 15.135135135135135], [60.56756756756755, 9.18918918918919], [61.1081081081081, 0.0]],
u'flags': {}, u'group_id': None, u'label': u'anchor'}], u'version': u'4.5.6', u'flags': {}, u'imageHeight': 512, u'imageWidth': 640}, {u'imagePath': u'..\\jpg_images\\20200205-095439-351.jpg', u'shapes': [{u'shape_type': u'polygon',
u'points': [[72.45945945945945, 348.10810810810807], [66.51351351351352, 356.7567567567567], [70.83783783783784, 359.4594594594594], [74.08108108108107, 354.5945945945946], [81.64864864864865, 355.6756756756757], [86.51351351351352, 347.02702702702703], [84.89189189189187, 335.1351351351351], [74.62162162162161, 329.72972972972974], [65.43243243243242, 331.35135135135135], [65.43243243243242, 344.86486486486484]],
u'flags': {}, u'group_id': None, u'label': u'anchor'}, {u'shape_type': u'polygon',
u'points': [[243.27027027027026, 184.32432432432432], [245.97297297297297, 192.97297297297297], [251.9189189189189, 191.35135135135135], [248.67567567567568, 183.24324324324323], [256.78378378378375, 183.78378378378378], [260.02702702702703, 175.13513513513513], [258.94594594594594, 165.4054054054054], [248.1351351351351, 163.24324324324323], [232.45945945945942, 167.56756756756755], [232.45945945945942, 183.24324324324323]],
u'flags': {}, u'group_id': None, u'label': u'anchor'}, {u'shape_type': u'polygon', u'points': [[41.648648648648646, 39.45945945945946], [31.918918918918905, 43.24324324324324], [34.621621621621614, 50.27027027027027], [44.89189189189187, 45.4054054054054], [49.756756756756744, 55.67567567567567], [61.1081081081081, 51.89189189189189], [67.59459459459458, 41.62162162162162], [66.51351351351352, 28.108108108108105], [58.40540540540539, 23.243243243243242], [43.81081081081081, 23.243243243243242], [36.24324324324323, 29.18918918918919]],
u'flags': {}, u'group_id': None, u'label': u'anchor'},
{u'shape_type': u'polygon', u'points': [[523.8108108108108, 110.8108108108108], [543.2702702702702, 113.5135135135135], [544.3513513513514, 107.02702702702702], [527.5945945945946, 104.32432432432432], [531.3783783783783, 95.67567567567568], [525.4324324324324, 93.51351351351352], [511.91891891891885, 96.75675675675676], [512.4594594594595, 105.94594594594594], [516.7837837837837, 116.21621621621621]], u'flags': {}, u'group_id': None, u'label': u'anchor'}], u'version': u'4.5.6', u'flags': {}, u'imageHeight': 512, u'imageWidth': 640}]
编码:
import json
print(len(test_json))
# Using simple for loop
for index in range(len(test_json)):
shapes = test_json[index][u'shapes']
print(shapes)
但这仅返回列表最后一个元素的点。我如何从这个列表中获得所有分数?我想从这个列表中提取所有点(它以 u'points' 给出),然后将这些点存储在单独的列表中,稍后我将对其进行展平。
我尝试了很多事情,比如使用 While 循环,将字典与列表(test_json)分开,但我未能达到预期的结果。
我将衷心感谢您的帮助。如果问题似乎不符合标准,我深表歉意。
问候,
亚什·伦沃尔。
解决方案
推荐阅读
- r - 如何删除按特征排序的重复项?
- email - 如何使用 postfix 在无效电子邮件进入 smtp 中继之前过滤掉它们
- python - “which conda”命令返回了意想不到的东西
- html - Bootstrap 4 Scrollspy 无法正确更改 IE 中的活动列表项
- search - solr search 如何处理 contains +phone
- junit - 基于 ActivitiRule 的单元测试中未调用 TaskListener
- javascript - laravel form bootstrap-select 无法正常工作
- c# - 防伪令牌无法解密?[无法摆脱这个奇怪的错误]
- python - 使用python登录booking.com
- php - 如何在 PHP 中获取关联数组的索引?