首页 > 解决方案 > 将列表动态添加到 json-dict 作为浮点数

问题描述

我正在运行一个 python 3.7 脚本,我试图删除引号。我试图将动态列表(x 和 y 坐标)添加到 json 字典,但我不断收到列表周围的引号。我需要在没有这些引号的情况下插入列表。

这是我正在运行的代码:

#The dynamic list 
polygon = ['0.124912491249125', '0.683368336833683', '0.128112811281128', '0.472147214721472', '-0.096909690969097', '0.444344434443444', '-0.196919691969197', '-0.533353335333533', '-0.64996499649965', '-0.427742774277428', '-0.290529052905291', '-0.266726672667267', '-0.237523752375237', '0.64996499649965']
list_polygon = []

for i,k in zip(polygon[0::2], polygon[1::2]):
    data_polygon = ('['+str(i), str(k)+']')
    data_polygon = str(data_polygon)
    list_polygon.append(data_polygon)

list_polygon = str(list_polygon)
list_polygon = list_polygon.replace("'",'').replace("(",'').replace(")",'').replace("[[",'[').replace("]]",']')

cord_data = {'apiVersion': '1.3',
         'context': '<client context>',
         'params': {'cameras': [{'active': True, 'id': 1, 'rotation': 0}],
                  'configurationStatus': 5,
                  'profiles': [{'camera': 1,
                                'filters': [{'active': True,
                                             'data': 1,
                                             'type': 'timeShortLivedLimit'},
                                            {'active': True,
                                             'data': 5,
                                             'type': 'distanceSwayingObject'},
                                            {'active': True,
                                             'data': [5, 5],
                                             'type': 'sizePercentage'}],
                                'name': 'Profile 1',
                                'triggers': [{'data': [list_polygon],
                                              'type': 'includeArea'}],
                                'uid': 1}]},
         'method': 'setConfiguration'}

我得到了答案:

{'apiVersion': '1.3', 'context': '<client context>', 'params': {'cameras': [{'active': True, 'id': 1, 'rotation': 0}], 'configurationStatus': 5, 'profiles': [{'camera': 1, 'filters': [{'active': True, 'data': 1, 'type': 'timeShortLivedLimit'}, {'active': True, 'data': 5, 'type': 'distanceSwayingObject'}, {'active': True, 'data': [5, 5], 'type': 'sizePercentage'}], 'name': 'Profile 1', 'triggers': [{'data': ['[0.124912491249125, 0.683368336833683], [0.128112811281128, 0.472147214721472], [-0.096909690969097, 0.444344434443444], [-0.196919691969197, -0.533353335333533], [-0.64996499649965, -0.427742774277428], [-0.290529052905291, -0.266726672667267], [-0.237523752375237, 0.64996499649965]'], 'type': 'includeArea'}], 'uid': 1}]}, 'method': 'setConfiguration'}

如您所见,它增加了括号(在开始和结束时)“触发”:[{'data':[[ 0.124912491249125,0.683333683333683] -0.196919691969197, -0.533353335333533], [-0.64996499649965, -0.427742774277428], [-0.290529052905291, -0.266726672667267], [-0.237523752375237, 0.64996499649965 ]']

相反,我希望它看起来像这样:

'triggers': [{'data': [[ 0.124912491249125, 0.683368336833683], [0.128112811281128, 0.472147214721472], [-0.096909690969097, 0.444344434443444], [-0.196919691969197, -0.533353335333533], [-0.64996499649965, -0.427742774277428], [-0.290529052905291, -0.266726672667267],[-0.237523752375237,0.64996499649965 ]]

第一个和第二个必须配对。

我要发送的帖子应该是这样的:

cord_data = {'apiVersion': '1.3',
         'context': '<client context>',
         'params': {'cameras': [{'active': True, 'id': 1, 'rotation': 0}],
                  'configurationStatus': 5,
                  'profiles': [{'camera': 1,
                                'filters': [{'active': True,
                                             'data': 1,
                                             'type': 'timeShortLivedLimit'},
                                            {'active': True,
                                             'data': 5,
                                             'type': 'distanceSwayingObject'},
                                            {'active': True,
                                             'data': [5, 5],
                                             'type': 'sizePercentage'}],
                                'name': 'Profile 1',
                                'triggers': [{'data': [[0.124912491249125, 0.683368336833683],
                                                       [0.128112811281128, 0.472147214721472],
                                                       [-0.096909690969097, 0.444344434443444]
                                                        and so on],
                                              'type': 'includeArea'}],
                                'uid': 1}]},
         'method': 'setConfiguration'}

我做错了什么?

标签: pythonpython-3.x

解决方案


我想你会想要这样的东西:

from pprint import pprint

#The dynamic list
polygon = ['0.124912491249125', '0.683368336833683', '0.128112811281128', '0.472147214721472', '-0.096909690969097', '0.444344434443444', '-0.196919691969197', '-0.533353335333533', '-0.64996499649965', '-0.427742774277428', '-0.290529052905291', '-0.266726672667267', '-0.237523752375237', '0.64996499649965']
list_polygon = []

for i,k in zip(polygon[0::2], polygon[1::2]):
    # Don't duplicate convert to `str`, please. Data is already a string.
    data_polygon = ([i, k])
    # Remove this line, it messes it up if retained
    # data_polygon = str(data_polygon)
    list_polygon.append(data_polygon)

# Why are we converting all lists to string anyway??
# list_polygon = str(list_polygon)
# list_polygon = list_polygon.replace("'",'').replace("(",'').replace(")",'').replace("[[",'[').replace("]]",']')

# Convert all nested strings to floats
list_polygon = [list(map(float, data_polygon)) for data_polygon in list_polygon]

cord_data = {'apiVersion': '1.3',
         'context': '<client context>',
         'params': {'cameras': [{'active': True, 'id': 1, 'rotation': 0}],
                  'configurationStatus': 5,
                  'profiles': [{'camera': 1,
                                'filters': [{'active': True,
                                             'data': 1,
                                             'type': 'timeShortLivedLimit'},
                                            {'active': True,
                                             'data': 5,
                                             'type': 'distanceSwayingObject'},
                                            {'active': True,
                                             'data': [5, 5],
                                             'type': 'sizePercentage'}],
                                'name': 'Profile 1',
                                # Don't nest it within another list (unless needed)
                                # 'triggers': [{'data': [list_polygon],
                                'triggers': [{'data': list_polygon,
                                              'type': 'includeArea'}],
                                'uid': 1}]},
         'method': 'setConfiguration'}

pprint(cord_data)

推荐阅读