如何将字典转换为嵌套字典?

时间:2019-11-24 19:00:50

标签: python python-2.7 dictionary

我有如下所示的字典:

{%- include 'check_not_feature_img' -%}
<table class="variations{%- if settings.swatch_design == '2' %} variant_square{%- elsif settings.swatch_design == '3' %} variant_simple{%- endif -%}" cellspacing="0">
   {%- assign option_index = 0 -%}
   {%- if settings.color_name_check != blank -%}{%- assign _gl_color_name = settings.color_name_check | replace: ' ,', ',' | replace: ', ', ',' | split: ',' -%}{%- assign gl_color_name = _gl_color_name | downcase -%}{%-else-%}{%- assign gl_color_name = '\nathan_dt\' -%}{%-endif-%}
   <tbody>
   {%- for option in product.options_with_values -%}
      {%- assign option_index = forloop.index0 -%}
      {%- assign downcased_option = option.name | downcase -%}
      {%- assign downcased_option_check = option.name | downcase | prepend: '"' | append: '"' | strip -%} 
      {%- if downcased_option == 'color' or downcased_option == 'colour' or gl_color_name contains downcased_option_check -%}
            <tr data-option-index="{{ option_index }}" id="gl_select_{{ option_index }}">
               <td class="label"><label for="{{ option_index }}">{{ option.name }}</label></td>
               <td class="value with-swatches">
                  <div class="swatches-select" data-id="{{ option_index }}" data-option-index="{{ option_index }}">
                     {%-assign index = 0 %}
                     {%- for value in option.values -%}
                        <div class="basel-swatch bg_color basel-tooltip swatch-size-{{settings.swatch_size}}{%- if settings.swatch_style == '1' %} colored-swatch{%- else %} image-swatch{%- endif %}{%- if first_available_variant and option.selected_value == value %} active-swatch{%- elsif forloop.first == true and product.selected_variant == blank and first_available_variant == false %} active-swatch{%- elsif option.selected_value == value and product.selected_variant != blank and first_available_variant == false %} active-swatch{%- endif %} bg_color_{{ value  | handleize  }} bg_{{ value  | handleize  }} swatch-enabled" data-value='{{ value | handleize }}' data-image-id="{{ featured_image_id[index] }}">
                           <span class="basel-tooltip-label">{{ value }}</span>{{ value }}
                        </div>
                        {%-assign index = index | plus: 1 %}
                     {%- endfor -%}
                  </div>
               </td>
            </tr>
      {%- else- %}
         <tr data-option-index="{{ option_index }}" id="gl_select_{{ option_index }}">
            <td class="label"><label for="{{ option_index }}">{{ option.name }}</label></td>
            <td class="value with-swatches">
               <div class="swatches-select" data-id="{{ option_index }}" data-option-index="{{ option_index }}">
                 {%- for value in option.values -%}
                     <div class="basel-swatch basel-tooltip text-only swatch-size-{{settings.swatch_size}}{%- if first_available_variant and option.selected_value == value %} active-swatch{%- elsif forloop.first == true and product.selected_variant == blank and first_available_variant == false %} active-swatch{%- elsif option.selected_value == value and product.selected_variant != blank and first_available_variant == false %} active-swatch{%- endif %} bg_{{ value | handleize  }} swatch-enabled" data-value='{{ value | handleize }}'>
                     <span class="basel-tooltip-label">{{ value }}</span>{{ value }}
                     </div>
                 {%- endfor -%}
               </div>
            </td>
         </tr>
      {%- endif -%}
   {%- endfor -%}
   </tbody>
</table>

我正在尝试制作嵌套字典,如下所示:

dict1 = {'K7': ['k07 = v07', ''], 'K2': ['k02 = v02', ''], 'K01': ['k2 = v2', 'k02 = v2', ''], 'K3': ['k1 = v1', ''], 'K02': ['k2 = v2', '']}

这是我尝试过的代码:

dict1 = [{'K7': {'k07' : 'v07'}}, {'K2': {'k02' : 'v02'}}, {'K01': {'k2' : 'v2','k02' : 'v2'}, {'K3': {'k1' : 'v1'}}, {'K02': {'k2' : 'v2'}}]

但是,我的输出如下:

 dict2 = []
 for ks,vs in list(dict1.iteritems()):

     vs = filter(None, vs)
     vi1 = [i.split('=', 1)[0] for i in vs]
     vi2 = [i.split('=', 1)[1] for i in vs] 

     for v1, v2 in zip(vi1,vi2):
         dict2.append({ks : {v1 : v2}})

很高兴听到一些建议。提前谢谢。

1 个答案:

答案 0 :(得分:2)

您可以将IFERROR(..., "")用于列表理解:

dict

输出:

dict1 = {'K7': ['k07 = v07', ''], 'K2': ['k02 = v02', ''], 'K01': ['k2 = v2', 'k02 = v2', ''], 'K3': ['k1 = v1', ''], 'K02': ['k2 = v2', '']}
new_d = [{a:dict(i.split(' = ') for i in b if i)} for a, b in dict1.items()]