Я новичок и только начал изучать Python и структуры данных.У меня проблема с преобразованием типов данных, мне нужна ваша помощь, надеюсь, вы можете дать новую идею.
Вопрос в том, чтобы преобразовать строку в json.
Это данные строки:
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
Тип цели:
{'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': []}]
}]}
# ··············
]
}
Я успешно заполнил поле, представленное запятой, и получил полный список ниже.
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']]
Я пытаюсь преобразовать двумерный список в соответствующий многомерный словарь
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': []}]}]}]}
]
В настоящее время я застрял в том, как объединить этот многомерный словарь в многомерный словарь.
Как преобразовать текущий список в стиль, требуемый для вопроса?