繁体   English   中英

将 Python 字典转换为元组列表

[英]Convert Python dictionary to list of tuples

我正在做一项小任务,其中包括:

my_filters = {'cat': 'apple,orange', 'day': '20200101,20200102'}

它应该将它转换成这样的元组列表:

[

[[('cat', '=', 'apple'), ('day', '=', '20200101')]]
[[('cat', '=', 'apple'), ('day', '=', '20200102')]]
[[('cat', '=', 'orange'), ('day', '=', '20200101')]]
[[('cat', '=', 'orange'), ('day', '=', '20200102')]]

]

我的代码适用于 2 个键:

def my_recursive_fun(i):
    results = []

    if i == 5:
        return ""
    if i <= 2:
        if i % 2 == 0:
            results.append([(list(my_filters.keys())[0], '=', my_filters['cat'].split(",")[0]),
                            (list(my_filters.keys())[1], '=', my_filters['day'].split(",")[1])])

        else:
            print("[\n")
            results.append([(list(my_filters.keys())[0], '=', my_filters['cat'].split(",")[0]),
                            (list(my_filters.keys())[1], '=', my_filters['day'].split(",")[0])])

    elif i <= 4:
        if i % 2 == 0:
            results.append([(list(my_filters.keys())[0], '=', my_filters['cat'].split(",")[1]),
                            (list(my_filters.keys())[1], '=', my_filters['day'].split(",")[1])])

            print(results)
            print("\n]")
            return ""
        else:
            results.append([(list(my_filters.keys())[0], '=', my_filters['cat'].split(",")[1]),
                            (list(my_filters.keys())[1], '=', my_filters['day'].split(",")[0])])

    print(results)
    my_recursive_fun(i + 1)


if __name__ == '__main__':
    my_recursive_fun(1)

但是当字典中有 4 个键时

my_filters = {'cat': 'apple,orange', 'day': '20200101,20200102', 'other': 'abc,cde', 'bis': '123,345'}

它应该是这样的:

[

[('cat','=','apple'),('day','=','20200101')],

[('cat','=','apple'),('day','=','20200102')],

[('cat','=','orange'),('day','=','20200101')],

[('cat','=','orange'),('day','=','20200102')],

[('other','=','abc'),('bis','=','123')],

[('other','=','abc'),('bis','=','345')],

[('other','=','cde'),('bis','=','123')],

[('other','=','cde'),('bis','=','345')]


]

因此,当我的字典中有更多键时,我很挣扎。 谢谢

下面是我对您的问题的一个变体的解决方案,它可能会给您一些关于如何 go 的想法。 我的不同之处包括: 我对my_filters使用真实的数据结构,即列表而不是字符串中的逗号分隔项; 此解决方案不保证结果的顺序; 此解决方案对my_filters具有破坏性:

my_filters = {
    'cat': ['apple', 'orange'],
    'day': ['20200101', '20200102'],
    'other': ['abc', 'cde'],
    'bis': [123, 345],
}

def recursive_filters(filters):
    my_results = []

    if filters:
        key, values = filters.popitem()

        results = recursive_filters(filters)

        for value in values:
            item = [key, '=', value]

            if results:
                for result in results:
                    my_results.append([*result, item])
            else:
                my_results.append([item])

    return my_results

print(*recursive_filters(my_filters), sep='\n')

OUTPUT

> python3 test.py
[['cat', '=', 'apple'], ['day', '=', '20200101'], ['other', '=', 'abc'], ['bis', '=', 123]]
[['cat', '=', 'orange'], ['day', '=', '20200101'], ['other', '=', 'abc'], ['bis', '=', 123]]
[['cat', '=', 'apple'], ['day', '=', '20200102'], ['other', '=', 'abc'], ['bis', '=', 123]]
[['cat', '=', 'orange'], ['day', '=', '20200102'], ['other', '=', 'abc'], ['bis', '=', 123]]
[['cat', '=', 'apple'], ['day', '=', '20200101'], ['other', '=', 'cde'], ['bis', '=', 123]]
[['cat', '=', 'orange'], ['day', '=', '20200101'], ['other', '=', 'cde'], ['bis', '=', 123]]
[['cat', '=', 'apple'], ['day', '=', '20200102'], ['other', '=', 'cde'], ['bis', '=', 123]]
[['cat', '=', 'orange'], ['day', '=', '20200102'], ['other', '=', 'cde'], ['bis', '=', 123]]
[['cat', '=', 'apple'], ['day', '=', '20200101'], ['other', '=', 'abc'], ['bis', '=', 345]]
[['cat', '=', 'orange'], ['day', '=', '20200101'], ['other', '=', 'abc'], ['bis', '=', 345]]
[['cat', '=', 'apple'], ['day', '=', '20200102'], ['other', '=', 'abc'], ['bis', '=', 345]]
[['cat', '=', 'orange'], ['day', '=', '20200102'], ['other', '=', 'abc'], ['bis', '=', 345]]
[['cat', '=', 'apple'], ['day', '=', '20200101'], ['other', '=', 'cde'], ['bis', '=', 345]]
[['cat', '=', 'orange'], ['day', '=', '20200101'], ['other', '=', 'cde'], ['bis', '=', 345]]
[['cat', '=', 'apple'], ['day', '=', '20200102'], ['other', '=', 'cde'], ['bis', '=', 345]]
[['cat', '=', 'orange'], ['day', '=', '20200102'], ['other', '=', 'cde'], ['bis', '=', 345]]
>

希望这个例子能给你一些关于如何为你自己的问题找到更清洁的解决方案的想法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM