Python字典合并 多个字典的之合并为列表示例

# -*- coding: utf-8 -*-
"""
字典合并
    场景说明

    输入: 多个 \t分割的文本文件   每个文本文件的两列数据分别代表 x轴数值 y轴数值
            多个文件的x轴的数值不连续
    输出: 合并的 \t分割的文本文件

"""


def text_file_write(file_name: str, content_list: list, mode='w+', is_flush=False) -> None:
    """

    :param file_name:
    :param content_list:
    :param mode:
    :param is_flush:
    :return:
    :return:
    """
    with open(file_name, mode, encoding='utf-8') as f:
        f.write('\n'.join(content_list))
        if is_flush:
            f.flush()


def text_file_read(file_name: str, mode='r') -> list:
    """

    :param file_name:
    :param mode:
    :return:
    """
    with open(file_name, mode, encoding='utf-8') as f:
        return [l.strip() for l in f.readlines()]


def main(out_file: str, test_list: list) -> None:
    """

    :param out_file:
    :param test_list: [
        [['1', '2'], ['22', '2'], ['23', '4'], ['5', '5']],
        ...
    ]
    :return:
    """
    update_key = []
    for tl in test_list:
        update_key += list(zip(*tl))[0]
    update_key = list(set(update_key))
    print(update_key)
    #
    update_dict = dict()
    for tl in test_list:
        test_dict = dict(tl)
        for i in update_key:
            update_dict.setdefault(i, []).append(test_dict.get(i, '0'))

    print(list(update_dict.items()))
    update_list = sorted(update_dict.items(), key=lambda x: int(x[0]))

    # 下行代码 当键len >=2 时,会被拆分
    # update_list = ['\t'.join([i for it in item for i in it]) for item in update_list]
    update_list_rst = []
    for item in update_list:
        temp = []
        for it in item:
            if isinstance(it, str):
                temp.append(it)
            else:
                temp += [i for i in it]
        update_list_rst.append('\t'.join(temp))
    text_file_write(out_file, update_list_rst)


if __name__ == '__main__':
    # 数据存入文本
    test1 = [[1, 2], [22, 2], [23, 4], [5, 5]]
    test2 = [[1, 2], [22, 2], [32, 4], [104, 1], [25, 5]]
    test3 = [[22, 2], [23, 4], [24, 1], [25, 5], [26, 2]]

    for index, test_demo in enumerate([test1, test2, test3]):
        tmp = ['\t'.join([str(j) for j in item]) for item in test_demo]
        text_file_write('test%s' % (index + 1), tmp)

    # 文本读取数据
    test1 = text_file_read('test1')
    test2 = text_file_read('test2')
    test3 = text_file_read('test3')
    test1 = [i.split('\t') for i in test1]
    test2 = [i.split('\t') for i in test2]
    test3 = [i.split('\t') for i in test3]
    print(test1)
    main('out', [test1, test2, test3])

输出

[['1', '2'], ['22', '2'], ['23', '4'], ['5', '5']]
['104', '23', '26', '5', '32', '24', '25', '1', '22']
[('104', ['0', '1', '0']), ('23', ['4', '0', '4']), ('26', ['0', '0', '2']), ('5', ['5', '0', '0']), ('32', ['0', '4', '0']), ('24', ['0', '0', '1']), ('25', ['0', '5', '5']), ('1', ['2', '2', '0']), ('22', ['2', '2', '2'])]
发布了76 篇原创文章 · 获赞 221 · 访问量 22万+

猜你喜欢

转载自blog.csdn.net/chichu261/article/details/104687000