Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将元组转换为字典列表_Python_Json - Fatal编程技术网

Python 将元组转换为字典列表

Python 将元组转换为字典列表,python,json,Python,Json,我无法将两个元组转换为字典列表。结构如下: train_detail = (Counter({2: 50, 0: 62, 1: 38}), {2: 0.3333333333333333, 0: 0.41333333333333333, 1: 0.25333333333333335}) test_detail = (Counter({2: 6, 0: 49, 1: 4}), {2: 0.1016949152542373, 0: 0.8305084745762712, 1: 0.0677966

我无法将两个元组转换为字典列表。结构如下:

train_detail = (Counter({2: 50, 0: 62, 1: 38}),
 {2: 0.3333333333333333, 0: 0.41333333333333333, 1: 0.25333333333333335})

test_detail = (Counter({2: 6, 0: 49, 1: 4}),
 {2: 0.1016949152542373, 0: 0.8305084745762712, 1: 0.06779661016949153})
现在,我想将这两个转换为如下结构:

 [
        {
            "label": "0",
            "trainPercent": 0.41333333333333333,
            "trainNumber": 62,
            "testPercent": 0.8305084745762712,
            "testNumber": 49,
        },
        {
            "label": "1",
            "trainPercent": 0.25333333333333335,
            "trainNumber": 38,
            "testPercent": 0.06779661016949153,
            "testNumber": 4,
        },
        {
            "label": "2",
            "trainPercent": 0.3333333333333333,
            "trainNumber": 50,
            "testPercent": 0.1016949152542373,
            "testNumber": 6,
        },      
    ]
使用最小循环的有效方法是什么?非常感谢。注Counter是dict的一个子类,因此继承了常规dict的每个方法

from pprint import pprint
from collections import Counter


train_detail = (Counter({2: 50, 0: 62, 1: 38}),
 {2: 0.3333333333333333, 0: 0.41333333333333333, 1: 0.25333333333333335})

test_detail = (Counter({2: 6, 0: 49, 1: 4}),
 {2: 0.1016949152542373, 0: 0.8305084745762712, 1: 0.06779661016949153})

out = []
for t in train_detail[0]:
    out.append({
        'label': str(t),
        'trainNumber': train_detail[0][t],
        'trainPercent': train_detail[1][t],
        'testPercent': test_detail[1][t],
        'testNumber': test_detail[0][t]
    })

# pretty print to screen:
pprint(out)
印刷品:

[{'label': '2',
  'testNumber': 6,
  'testPercent': 0.1016949152542373,
  'trainNumber': 50,
  'trainPercent': 0.3333333333333333},
 {'label': '0',
  'testNumber': 49,
  'testPercent': 0.8305084745762712,
  'trainNumber': 62,
  'trainPercent': 0.41333333333333333},
 {'label': '1',
  'testNumber': 4,
  'testPercent': 0.06779661016949153,
  'trainNumber': 38,
  'trainPercent': 0.25333333333333335}]
印刷品:

[{'label': '2',
  'testNumber': 6,
  'testPercent': 0.1016949152542373,
  'trainNumber': 50,
  'trainPercent': 0.3333333333333333},
 {'label': '0',
  'testNumber': 49,
  'testPercent': 0.8305084745762712,
  'trainNumber': 62,
  'trainPercent': 0.41333333333333333},
 {'label': '1',
  'testNumber': 4,
  'testPercent': 0.06779661016949153,
  'trainNumber': 38,
  'trainPercent': 0.25333333333333335}]

使用lambda函数。lambda用于解压元组。

使用lambda函数。lambda用于解压元组。

感谢@Andrej Kesely和@Björn Marscholek

结合他们的答案:

labels_detail_list = [{'label': str(i), 
                       'trainNumber': train_detail[0][i], 
                       'trainPercent': train_detail[1][i],
                       'testNumber': test_detail[0][i],
                       'testPercent': test_detail[1][i]
                      } for i in train_detail[0]]

sorted(labels_detail_list, key=lambda x: int(x['label']))

应该是更简洁的版本。

谢谢@Andrej Kesely和@Björn Marschollek

结合他们的答案:

labels_detail_list = [{'label': str(i), 
                       'trainNumber': train_detail[0][i], 
                       'trainPercent': train_detail[1][i],
                       'testNumber': test_detail[0][i],
                       'testPercent': test_detail[1][i]
                      } for i in train_detail[0]]

sorted(labels_detail_list, key=lambda x: int(x['label']))

应该是更简洁的版本。

列表理解也可以做到这一点,并且可能更具可读性。如果需要对结果进行排序,您可能希望在迭代之前对键进行排序,或者添加
sorted(res,key=lambda x:int(x[“label”])
列表理解也会起到作用,并且可能更具可读性。如果需要对结果进行排序,您可能希望在迭代之前对键进行排序,或者添加
sorted(res,key=lambda x:int(x[“label”])