简体   繁体   中英

How to convert List to Json in python3

I am a beginner and just started learning python and data structures. I have a problem with data type conversion, I need your help, I hope you can give a new idea.

The question is to convert the string to json.

This is row data:

machine learning,inear model,linear regression,least squares
,neural network,neuron model,activation function
,multi-layer network,perceptron
,,,connection right
,reinforcement learning,model learning,strategy evaluation
,,,strategy improvement
,,model-free learning,monte carlo method
,,,time series learning
,imitate learning,directly imitate learning
,,,inverse reinforcement learning

Target style:

{'machine learning':
    [{'inear model':
        [{'linear regression':
            [{'least squares': []}]
          }]},
    {'neural network':
        [{'neuron model':
              [{'activation function': []}]
          }]},
    {'multi-layer network':
         [{'perceptron':
               [{'connection right': []}]
           }]},
    {'reinforcement learning':
         [{'model learning':
               [{'strategy evaluation': []}]
           }]}
    # ··············
     ]
          }

I have successfully completed the field represented by the comma and got a complete list below.

with open('concept.txt', 'r') as f:
    contents = f.readlines()
concepts = []

for concept in contents:
    concept = concept.replace('\n', '')
    array = concept.split(',')
    concepts.append(array)

for i in range(len(concepts)):
    for j in range(len(concepts[i])):
        if concepts[i][j] == '':
            concepts[i][j] = concepts[i-1][j]
print(concepts)


>>> [['machine learning', ' linear model', ' linear regression', ' least squares'], 
    ['machine learning', ' neural network', ' neuron model', ' activation function'],
    ['machine learning', ' multi-layer network', ' perceptron'], 
    ['machine learning', ' multi-layer network', ' perceptron', ' connection right'], 
    ['machine learning', ' reinforcement learning', ' model learning', ' strategy evaluation'], 
    ['machine learning', ' reinforcement learning', ' model learning', ' strategy improvement'], 
    ['machine learning', ' reinforcement learning', ' model-free learning', ' Monte Carlo method'], 
    ['machine learning', ' reinforcement learning', ' model-free learning', 'time series learning'], 
    ['machine learning', ' imitate learning', ' directly imitate learning'], 
    ['machine learning', ' imitate learning', ' directly imitate learning', ' inverse reinforcement learning']] 

I try to convert two-dimensional list to the corresponding multidimensional dictionary

def dic(list):
    key = list[0]
    list.pop(0)
    if len(list) == 0:
        return {key: []}
    return {key: [dic(list)]}

def muilti_dic(mlist):
    muilti_list = []
    for i in range(len(mlist)):
        dic = dic(mlist[i])
        muilti_list.append(dic)
    return muilti_list

>>> [
     {'machine learning': 
         [{'inear model': 
          [{'linear regression': [{'least squares': []}]}]}]}, 
     {'machine learning': 
        [{'neural network': 
          [{'neuron model': [{'activation function': []}]}]}]}, 
     {'machine learning': 
        [{'multi-layer network': [{'perceptron': []}]}]}, 
     {'machine learning': 
        [{'multi-layer network': 
          [{'perceptron': [{'connection right': []}]}]}]}, 
     {'machine learning': 
        [{'reinforcement learning': 
          [{'model learning': [{'strategy evaluation': []}]}]}]}, 
     {'machine learning': 
        [{'reinforcement learning': 
          [{'model learning': [{'strategy improvement': []}]}]}]}, 
     {'machine learning': 
        [{'reinforcement learning': 
          [{'model-free learning': [{'Monte Carlo method': []}]}]}]}, 
     {'machine learning': 
        [{'reinforcement learning': 
          [{'model-free learning': [{'time series learning': []}]}]}]}, 
     {'machine learning': 
        [{'imitate learning': [{'directly imitate learning': []}]}]}, 
     {'machine learning': [{'imitate learning': [{'directly imitate learning': [{'inverse reinforcement learning': []}]}]}]}
    ]

At present, I am stuck in how to merge this multiple multidimensional dictionary into a multidimensional dictionary.

How do I convert the current list into the style required by the question?

Instead of creation of separate dictionaries and then merging them, try to create the final (joined) dictionary, without any intermediate step.

The fragment of your code creating concepts list is OK.

Then add import json at the start of your program, and at the end, add the following code:

res = []    # Result
for row in concepts:
    curr = res    # Current object
    for str in row:
        if len(curr) == 0:
            curr.append({})
        curr = curr[0]
        if str not in curr:
            curr[str] = []
        curr = curr[str]

print(json.dumps(res, indent=2))

As you can see, the idea is:

  1. The result ( res ) is a list contaning a single dictionary object.
  2. Processing of each row causes "going down the object tree", on each string - element of the current row.
  3. Initially added value to a dictionary (for some key) contains an empty list. If there is no "more embedded" element, this ends this "path".
  4. Before "going down the tree" the program adds an empty dictionary to this list, if it has not beed added earlier.
  5. The last step is to add an empty array, under the key equal to the current string.

The printed result (slightly reformatted to take less rows) is:

[{"machine learning": [{
    "inear model": [{
      "linear regression": [{
        "least squares": []}]}],
    "neural network": [{
      "neuron model": [{
        "activation function": []}]}],
    "multi-layer network": [{
      "perceptron": [{
        "connection right": []}]}],
    "reinforcement learning": [{
      "model learning": [{
        "strategy evaluation": [],
        "strategy improvement": []}],
      "model-free learning": [{
        "monte carlo method": [],
        "time series learning": []}]}],
    "imitate learning": [{
      "directly imitate learning": [{
        "inverse reinforcement learning": []}]}]
    }]
}]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM