django 动态创建for循环以从字典创建列表[duplicate]

drnojrws  于 2022-12-05  发布在  Go
关注(0)|答案(1)|浏览(125)

此问题在此处已有答案

How to get the cartesian product of a series of lists(17个答案)
4天前关闭。

variations = {
    'size':{'small':'Small',
            'medium':'Medium', 
            'large':'Large'}, 
    'quantity':{'20l':'20l',
                '10l':'10l',
                '5l':'5l'},
    'color':{'red':'Red',
             'blue':'Blue',
             'green':'Green'}
                }

var_list = [[i,j,k] for i in variations['color'] for j in variations['size'] for k in variations['quantity']]

也可以将上面的代码编写为:

var_list = []

for i in variations['color']:
  for j in variations['size']:
    for k in variations['quantity']:
      comb = []
      comb.append(i)
      comb.append(j)
      comb.append(k)
      Var_list.append(comb)

两个var_list输出:

[['red', 'small', '20l'], ['red', 'small', '10l'], ['red', 'small', '5l'], ['red', 'medium', '20l'], ['red', 'medium', '10l'], ['red', 'medium', '5l'], ['red', 'large', '20l'], ['red', 'large', '10l'], ['red', 'large', '5l'], ['blue', 'small', '20l'], ['blue', 'small', '10l'], ['blue', 'small', '5l'], ['blue', 'medium', '20l'], ['blue', 'medium', '10l'], ['blue', 'medium', '5l'], ['blue', 'large', '20l'], ['blue', 'large', '10l'], ['blue', 'large', '5l'], ['green', 'small', '20l'], ['green', 'small', '10l'], ['green', 'small', '5l'], ['green', 'medium', '20l'], ['green', 'medium', '10l'], ['green', 'medium', '5l'], ['green', 'large', '20l'], ['green', 'large', '10l'], ['green', 'large', '5l']]

var_list包含了3个for循环,基于变体中的3个字典。如何编写上面的代码,以便var_list中的for循环可以根据变体中存在的字典数量增加或减少?
例如,如果“brand”也出现在变量中,则应在var_list中动态创建此“brand”的for循环,因此var_list变为

var_list = [[i,j,k,l] for i in variations['color'] for j in variations['size'] for k in variations['quantity'] for l in varistions['brands']
tkclm6bt

tkclm6bt1#

当我听到 *nested for循环 * 时,我认为 * 是..的乘积 *
得到variations的值的值的乘积。

variations = {
    'size':{'small':'Small',
            'medium':'Medium', 
            'large':'Large'}, 
    'quantity':{'20l':'20l',
                '10l':'10l',
                '5l':'5l'},
    'color':{'red':'Red',
             'blue':'Blue',
             'green':'Green'},
    'brand':{'one':'foo','two':'bar'}}

>>> a = [list(v.values()) for v in variations.values()]
>>> a
[['Small', 'Medium', 'Large'], ['20l', '10l', '5l'], ['Red', 'Blue', 'Green'], ['foo', 'bar']]
>>> import itertools
>>> for c in itertools.product(*a):      
...     print(c)
... 
('Small', '20l', 'Red', 'foo')
('Small', '20l', 'Red', 'bar')
('Small', '20l', 'Blue', 'foo')
('Small', '20l', 'Blue', 'bar')
('Small', '20l', 'Green', 'foo')
('Small', '20l', 'Green', 'bar')
('Small', '10l', 'Red', 'foo')
('Small', '10l', 'Red', 'bar')
('Small', '10l', 'Blue', 'foo')
('Small', '10l', 'Blue', 'bar')
('Small', '10l', 'Green', 'foo')
('Small', '10l', 'Green', 'bar')
('Small', '5l', 'Red', 'foo')
('Small', '5l', 'Red', 'bar')
('Small', '5l', 'Blue', 'foo')
('Small', '5l', 'Blue', 'bar')
('Small', '5l', 'Green', 'foo')
('Small', '5l', 'Green', 'bar')
('Medium', '20l', 'Red', 'foo')
('Medium', '20l', 'Red', 'bar')
('Medium', '20l', 'Blue', 'foo')
('Medium', '20l', 'Blue', 'bar')
('Medium', '20l', 'Green', 'foo')
('Medium', '20l', 'Green', 'bar')
('Medium', '10l', 'Red', 'foo')
('Medium', '10l', 'Red', 'bar')
('Medium', '10l', 'Blue', 'foo')
('Medium', '10l', 'Blue', 'bar')
('Medium', '10l', 'Green', 'foo')
('Medium', '10l', 'Green', 'bar')
('Medium', '5l', 'Red', 'foo')
('Medium', '5l', 'Red', 'bar')
('Medium', '5l', 'Blue', 'foo')
('Medium', '5l', 'Blue', 'bar')
('Medium', '5l', 'Green', 'foo')
('Medium', '5l', 'Green', 'bar')
('Large', '20l', 'Red', 'foo')
('Large', '20l', 'Red', 'bar')
('Large', '20l', 'Blue', 'foo')
('Large', '20l', 'Blue', 'bar')
('Large', '20l', 'Green', 'foo')
('Large', '20l', 'Green', 'bar')
('Large', '10l', 'Red', 'foo')
('Large', '10l', 'Red', 'bar')
('Large', '10l', 'Blue', 'foo')
('Large', '10l', 'Blue', 'bar')
('Large', '10l', 'Green', 'foo')
('Large', '10l', 'Green', 'bar')
('Large', '5l', 'Red', 'foo')
('Large', '5l', 'Red', 'bar')
('Large', '5l', 'Blue', 'foo')
('Large', '5l', 'Blue', 'bar')
('Large', '5l', 'Green', 'foo')
('Large', '5l', 'Green', 'bar')

>>>

或者只是

>>> a = [v.values() for v in variations.values()]
>>> for c in itertools.product(*a): print(c)

相关问题