使用字典将 "sequential" 信息添加到 python 列表

Adding "sequential" information to python list using dictionaries

问题

我想从我拥有的平面列表中创建字典,以便添加“顺序性”信息,但我在寻找解决方案时遇到了一些问题。

列表类似于

a = ['Q=123', 'W=456', 'E=789', 'Q=753', 'W=159', 'E=888']

我正在拍摄 dict 像:

dictionary = {
    'Step_1': {
        'Q=123', 
        'W=456', 
        'E=789'
    },
    'Step_2': {
        'Q=753', 
        'W=159', 
        'E=888'
    }
}

我想以一个具有任意数量 Steps 的函数结束,以便将其应用于我的数据集。假设在数据集中有像 a 这样的列表,每个列表有 1 <= n <6 Steps

我的想法

到目前为止,我想到了这个:

nsteps = a.count("Q")
data = {}
for i in range(nsteps):
    stepi = {}
    for element in a:
            new = element.split("=")
            if new[0] not in stepi:
                stepi[new[0]] = new[1]
            else:
                pass
    data[f"Step_{i}"] = stepi

但它没有按预期工作:最终字典中的两个步骤都包含 Step_1 的数据。 知道如何解决这个问题吗?

一种方法是:

a = ['Q=123', 'W=456', 'E=789', 'Q=753', 'W=159', 'E=888']

indices = [i for i, v in enumerate(a) if v[0:2] == 'Q=']

dictionary = {f'Step_{idx+1}': {k: v for k, v in (el.split('=') for el in a[s:e])} 
              for idx, (s, e) in enumerate(zip(indices, indices[1:] + [len(a)]))}

print(dictionary)
{'Step_1': {'Q': '123', 'W': '456', 'E': '789'}, 
'Step_2': {'Q': '753', 'W': '159', 'E': '888'}}

详情:

a = ['Q=123', 'W=456', 'E=789', 'Q=753', 'W=159', 'E=888']

# Get indices where a step starts. 
# This could handle also steps with variable amount of elements and keys starting with 'Q' that are not exactly 'Q'.
indices = [i for i, v in enumerate(a) if v[0:2] == 'Q=']

# Get the slices of the list starting at Q and ending before the next Q.
slices = list(zip(indices, indices[1:] + [len(a)]))
print(slices)
# [(0, 3), (3, 6)]

# Get step index and (start, end) pair for each slice.
idx_slices = list(enumerate(slices))
print(idx_slices)
# [(0, (0, 3)), (1, (3, 6))]

# Split the strings in the list slices and use the result as key-value pair for a given start:end.
# Here an example for step 1:
step1 = idx_slices[0][1] # This is (0, 3).
dict_step1 = {k: v for k, v in (el.split('=') for el in a[step1[0]:step1[1]])}
print(dict_step1)
# {'Q': '123', 'W': '456', 'E': '789'}

# Do the same for each slice.
step_dicts = {f'Step_{idx+1}': {k: v for k, v in (el.split('=') for el in a[s:e])}
              for idx, (s, e) in idx_slices}
print(step_dicts)
# {'Step_1': {'Q': '123', 'W': '456', 'E': '789'}, 'Step_2': {'Q': '753', 'W': '159', 'E': '888'}}

根据你的问题我了解到:

我们可以对列表中的项目进行分组,在本例中,一组三个元素,并一次循环三个。

在这个 answer 的帮助下:

from itertools import zip_longest

a = ['Q=123', 'W=456', 'E=789', 'Q=753', 'W=159', 'E=888']

def grouper(n, iterable):
    args = [iter(iterable)] * n
    return zip_longest(*args)

result = dict()

for i, d in enumerate(grouper(3, a), start=1):
    dict.update({f"Step_{i}": set(d)})

print(result)
{
    'Step_1': {'E=789', 'Q=123', 'W=456'}, 
    'Step_2': {'E=888', 'Q=753', 'W=159'}
}

首先按这样的项目分组:

a = ['Q=123', 'W=456', 'E=789', 'Q=753', 'W=159', 'E=888']
o = groupby(sorted(a, key=lambda x: x[0]), key=lambda x: x[0])

然后像这样创建一个字典:

d = {i: [j[1] for j in g] for i, g in o}

然后遍历它们并得到你的结果:

result = {f"step_{i+1}": [v[i] for v in r.items()] for i in range(len(max(r.values(), key=len)))}

结果将是:

Out[47]: {'step_1': ['E=789', 'Q=123', 'W=456'], 'step_2': ['E=888', 'Q=753', 'W=159']}

你快到了。您计算“Q”数量的方式有误,并且某些代码行的缩进有误(例如 data[f"Step_{i}"] = stepi

a = ['Q=123', 'W=456', 'E=789', 'Q=753', 'W=159', 'E=888']


def main():
    nsteps = len([s for s in a if "Q" in s])
    data = {}
    for i in range(nsteps):
        stepi = {}
        for element in a:
            new = element.split("=")
            if new[0] not in stepi:
                stepi[new[0]] = new[1]
            data[f"Step_{i}"] = stepi

    return data

if __name__ == "__main__":
    data = main()