转换数据结构中的列表

时间:2017-12-02 20:12:43

标签: python-3.x

我想知道可以创建一个转换此列表的函数:

['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被更新,然后当一个新元素与" ="在它中遇到一个字典包含变量键作为列表,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):])

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

1 个答案:

答案 0 :(得分:0)

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

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