桔子 皮 2022-08-19 23:26 采纳率: 40%
浏览 25
已结题

python对大括号里面的大括号的数据排序后按顺序导出大括号后面的name怎么操作呢

代码如下

img

根据left的从小到大进行排序,然后导出name。并把name变成对应的英文
即最后结果print("right","right","up","left")

  • 写回答

1条回答 默认 最新

  • 梦里逆天 2022-08-22 13:51
    关注
    
    dict_1 = {
        "log_id": 1,
        "results": [
            {
                "location": {
                    "height": 29,
                    "left": 360,
                    "top": 76,
                    "width": 32
                },
                "name": "上",
                "score": 0.98
            },
            {
                "location": {
                    "height": 26,
                    "left": 471,
                    "top": 68,
                    "width": 27
                },
                "name": "左",
                "score": 0.92
            },
            {
                "location": {
                    "height": 28,
                    "left": 268,
                    "top": 79,
                    "width": 30
                },
                "name": "右",
                "score": 0.89
            },
            {
                "location": {
                    "height": 31,
                    "left": 200,
                    "top": 73,
                    "width": 29
                },
                "name": "右",
                "score": 0.88
            }
        ]
    }
    
    
    def translate(word):
        dict_word = {'right': '右', 'left': '左', 'top': '上'}
        # 获取value的key
        name = list(dict_word.keys())[list(dict_word.values()).index(word)]
        return name
    
    
    list_1 = dict_1['results']
    list_len = len(list_1)
    # 冒泡排序
    for i in range(list_len):
        for j in range(0, list_len - i - 1):
            # 比较location中的left的值
            if list_1[j]['location']['left'] > list_1[j + 1]['location']['left']:
                # 交换值
                list_1[j], list_1[j + 1] = list_1[j + 1], list_1[j]
    print(list_1)
    # [{'location': {'height': 31, 'left': 200, 'top': 73, 'width': 29}, 'name': '右', 'score': 0.88}, {'location': {
    # 'height': 28, 'left': 268, 'top': 79, 'width': 30}, 'name': '右', 'score': 0.89}, {'location': {'height': 29, 
    # 'left': 360, 'top': 76, 'width': 32}, 'name': '上', 'score': 0.98}, {'location': {'height': 26, 'left': 471, 
    # 'top': 68, 'width': 27}, 'name': '左', 'score': 0.92}] 
    for i in range(list_len):
        print(translate(list_1[i]['name']), end=' ')
    # right right top left 
    

    这么写怎么样?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 8月31日
  • 已采纳回答 8月23日
  • 创建了问题 8月19日

悬赏问题

  • ¥30 vmware exsi重置后的密码
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 UE5#if WITH_EDITOR导致打包的功能不可用
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题