在数据结构

变换列表我想知道它有可能创建一个转换此列表的功能:在数据结构

['music\n', ' extension=mp3\n', '\n', 'reports/INFOB131\n', ' extension=doc,docx,pdf\n', ' name_contains=INFOB131\n', ' max_size=100000\n', '\n', 'reports/INFOB132\n', ' extension=doc,docx,pdf\n', ' name_contains=INFOB132\n', ' max_size=100000\n', '\n', 'games\n', ' name_contains=SC2,Wesnoth\n', '\n', 'pictures/Namur\n', ' extension=jpeg\n', ' min_size=5000000\n', ' name_contains=cercle\n', '\n', 'pictures/autres\n', ' extension=jpeg\n', ' min_size=5000000'] 

已经从使用readlines方法()函数的文本文件得到。

到数据结构像这样的:

data_config = [{'music' : {'extension':'mp3'}}, {'reports/INFOB131': {'extension': ['doc', 'docx','pdf'], 'name_contains':'INFOB131', 'max_size':100000}}, {'reports/INFOB132': {'extension': ['doc', 'docx','pdf'], 'name_contains':'INFOB132', 'max_size':100000}}] 

我开始通过这些过程,以清理名单:

new_lines = [] 

for i in lines :

new_lines.append(str.strip(i, '\n'))

for i in new_lines:

if i == '':

new_lines.remove(i)

我得到这样的:

['music', ' extension=mp3', 'reports/INFOB131', ' extension=doc,docx,pdf', ' name_contains=INFOB131', ' max_size=100000', 'reports/INFOB132', ' extension=doc,docx,pdf', ' name_contains=INFOB132', ' max_size=100000', 'games', ' name_contains=SC2,Wesnoth', 'pictures/Namur', ' extension=jpeg', ' min_size=5000000', ' name_contains=cercle', 'pictures/autres', ' extension=jpeg', ' min_size=5000000'] 

接下来,理论上我有这样的想法:

创建一个将在列表中浏览的函数,如果“=”不在列表的元素中,则将其定义为变量k。否则,该列表的元素将被添加到列表名为pre_list和直到另外一个元素没有“=”,在没有遇到过,这pre_list进行更新,然后当新元素“=”,在它遇到一个字典containig变量键作为列表,pre_list是分配给该键的元素。

def my_function(list_in_question, my_config_list =[]): 

""" """

for i in list_in_question :

#it misses the first if for the situation in which the function stops

if not '=' in i:

my_config_list.append({key:pre_list})

i = key

pre_dict = {key:{}}

else:

pre_dict[key][i.split('=')[0]] = i.split('=')[1]

return myfunction(list_in_question[list_in_question.index(i):])

我想以递归的方式创建它。我有问题。其中之一是,我不知道我将如何阻止它...

回答:

问题比我预期的更复杂。
现在很难解释所有的代码。
它适用于当前数据,但我不确定它是否可以与其他数据一起使用。

data = ['music\n', ' extension=mp3\n', '\n', 'reports/INFOB131\n', ' extension=doc,docx,pdf\n', ' name_contains=INFOB131\n', ' max_size=100000\n', '\n', 'reports/INFOB132\n', ' extension=doc,docx,pdf\n', ' name_contains=INFOB132\n', ' max_size=100000\n', '\n', 'games\n', ' name_contains=SC2,Wesnoth\n', '\n', 'pictures/Namur\n', ' extension=jpeg\n', ' min_size=5000000\n', ' name_contains=cercle\n', '\n', 'pictures/autres\n', ' extension=jpeg\n', ' min_size=5000000'] 

config_list_2 = []

# variable for dictionary

prev_ext = None

curr_ext = ''

prev_list = list()

prev_dict = dict()

for item in data:

# remove spaces, tabs and '\n'

item = item.strip()

# create dictionary and sometimes new sublist

if not item:

if (prev_ext is not None) and curr_ext != prev_ext:

config_list_2.append(prev_list)

prev_list = list()

prev_list.append(prev_dict)

prev_dict = dict()

prev_ext = curr_ext

curr_ext = ''

# work only with elements `key=value`

if '=' in item:

# split `key=value`

key, value = item.split('=')

# if value has `,` then it is list of values

if ',' in value:

value = value.split(',')

# if you get `extension then you have start new dictionary

if key == 'extension':

curr_ext = value

# some values have to be converted to int()

if key in ('max_size', 'min_size'):

value = int(value)

# add to current dictionary

prev_dict[key] = value

# add last dictionary

if (prev_ext is not None) and curr_ext != prev_ext:

config_list_2.append(prev_list)

prev_list = list()

prev_list.append(prev_dict)

# add last sublist

config_list_2.append(prev_list)

# --- print result ---

print(config_list_2)

# --- compare with expected result --

expected = [[{'extension' : 'mp3'}], [{'extension':['doc','docx', 'pdf'], 'name_contains':'INFOB131', 'max_size':100000}, {'extension':['doc','docx', 'pdf'], 'name_contains':'INFOB132', 'max_size':100000}],[{'name_contains':['SC2','Wesnoth']}],[{'extension':'jpeg', 'min_size':5000000, 'name_contains':'cercle'}, {'extension':'jpeg', 'min_size':5000000}]]

print(config_list_2 == expected) # True

以上是 在数据结构 的全部内容, 来源链接: utcz.com/qa/258137.html

回到顶部