代码如下
根据left的从小到大进行排序,然后导出name。并把name变成对应的英文
即最后结果print("right","right","up","left")
代码如下
根据left的从小到大进行排序,然后导出name。并把name变成对应的英文
即最后结果print("right","right","up","left")
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
这么写怎么样?