将Python字典转换为元组列表

将Python字典转换为元组列表,python,Python,我正在进行一项小任务,包括: my_filters={'cat':'apple,orange','day':'202001012020002'} 它应该将其转换为元组列表,如下所示: [ [[('cat', '=', 'apple'), ('day', '=', '20200101')]] [[('cat', '=', 'apple'), ('day', '=', '20200102')]] [[('cat', '=', 'orange'), ('day', '=', '20200101')]

我正在进行一项小任务,包括:

my_filters={'cat':'apple,orange','day':'202001012020002'}

它应该将其转换为元组列表,如下所示:

[

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

]
[

[('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')]


]
我的代码在两个键上运行良好:

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)
应该是这样的:

[

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

]
[

[('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')]


]

所以,当我的字典里有更多的键时,我很挣扎。谢谢

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

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')
输出

> 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]]
>

希望本例能为您提供一些思路,让您了解如何为自己的问题找到更干净的解决方案。

这是一种痛苦的处理方法。你需要做更多关于概括问题的练习。帮助此特定应用程序的一个工具是
itertools
包,尤其是
产品
方法。我不太愿意“帮助”您,因为我看不出您给定的输出格式有任何合理的应用程序:一个奇怪字符串的元组列表?我担心您需要备份并学习足够的数据结构,以便正确应用您试图实现的任何目标。@Prune老实说,这是一项学校作业,这是使用说明,请参见。您需要澄清您的限制:“this”没有明确的引用。字典中的键对定义是否正确?因为在Python3.7之前,字典是没有顺序的,所以您可能会将
“cat”
“other”
配对,将
“day”
“bis”
配对,除非您对那些键有一些特殊的了解,可以使用指定的方式将它们配对。如果按照它们在
dict
中出现的顺序进行排序,由于散列随机化,在不同的运行中会得到不同的结果。从Python3.7开始,
dict
行为发生了变化(插入顺序被保留),尽管向后兼容的代码可能还不想利用这一事实。