简体   繁体   English

循环遍历python默认字典

[英]looping through python default dict

I create a default dict in my code something like below:我在我的代码中创建了一个默认的字典,如下所示:

defaultdict( <class 'list'> , {'month': ['JAN', 'FEB'], 'car': ['baleno', 'santro'], 'measure': ['sales', 'expense']}) defaultdict( <class 'list'> , {'month': ['JAN', 'FEB'], 'car': ['baleno', 'santro'], 'measure': ['sales', 'expense' ]})

cube = 'test'

Now I would like to print above dict in the below format by adding variable cube :现在我想通过添加变量cube以下面的格式在 dict 上面打印:

['month', 'JAN', 'car', 'baleno', 'measure', 'sales', 'test'] ['month', 'JAN', 'car', 'baleno', 'measure', 'sales', 'test']

['month', 'JAN', 'car', 'baleno', 'measure','expense', 'test'] ['month', 'JAN', 'car', 'baleno', 'measure', 'expense', 'test']

['month', 'JAN', 'car', 'santro', 'measure', 'sales', 'test'] ['month', 'JAN', 'car', 'santro', 'measure', 'sales', 'test']

['month', 'JAN', 'car', 'santro', 'measure', 'expense', 'test'] ['month', 'JAN', 'car', 'santro', 'measure', 'expense', 'test']

['month', 'FEB', 'car', 'baleno', 'measure','sales', 'test'] ['month', 'FEB', 'car', 'baleno', 'measure','sales', 'test']

['month', 'FEB', 'car', 'baleno', 'measure','expense', 'test'] ['month', 'FEB', 'car', 'baleno', 'measure', 'expense', 'test']

['month', 'FEB', 'car', 'santro', 'measure','sales', 'test'] ['month', 'FEB', 'car', 'santro', 'measure','sales', 'test']

['month', 'FEB', 'car', 'santro', 'measure','expense', 'test'] ['month', 'FEB', 'car', 'santro', 'measure', 'expense', 'test']

I'm actually using three loops to achieve the above output, but would like to get a neat one.我实际上使用三个循环来实现上述输出,但想要得到一个整洁的循环。

dim=['month','car','measure']
cube='test'
for b in itertools.product(*(k.values())):                                                  
        list1 = list()                                      
        for (f, c) in zip(b, dim):                                                         
            list1.append(c)                                 
            list1.append(f)                                 
        list1.append(cube)                             
        print(list1) 

k is the default dict k是默认字典

PS: I'm new to PYTHON. PS:我是 PYTHON 的新手。 Just using it for the couple of months.只是使用它几个月。

Given the input is a dictionary, I don't think you can get much more efficient than nested for loops (note: itertools.product is equivalent to a for loop).鉴于输入是一个字典,我认为你不会比嵌套 for 循环更高效(注意:itertools.product 相当于一个 for 循环)。 You could possibly do it as a one liner using list comprehension, but this won't be more efficient and may be less readable.您可以使用列表理解将其作为单行代码来完成,但这不会更有效率并且可读性可能较低。

Your implementation looks fine, here is a slightly more streamlined write up:您的实现看起来不错,这里有一个稍微简化的写法:

k = {'month': ['JAN', 'FEB'], 
     'car': ['baleno', 'santro'], 
     'measure': ['sales', 'expense']}

# Grab the keys from the dictionary as opposed to hard-coding them
dim=k.keys()
cube='test'

# cartesian product of each key's set of values
for b in itertools.product(*k.values()):                                                
    list1 = list()
    # extending empty list by (key, value) for specific values in product b                         
    for pair in zip(dim, b):                                                         
        list1.extend(pair)                                 
    list1.append(cube)                             
    print(list1) 

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

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