【问题标题】:How to create one dictionary from for loop如何从 for 循环创建一个字典
【发布时间】:2021-09-21 23:35:06
【问题描述】:

我对编码很陌生,我正在尝试遍历一个列表并创建一个具有相同键的字典。我的代码目前正在使用相同的键创建大约 1000 个格式正确的字典,但我不知道如何将它们变成一个字典。

for salaries, match in matching_salaries_non_employees.items():

   final_match.update(matches)

   Power_match = (match['Status']['Power'])+ ": " + (salaries)
    
   Length_match = (match['Status']['Length'])+ ": " + (match['Break']['Temp'])
        
   duration_Status = (match ['Status'][str('duration')])
   duration_Break = (match ['Break']['duration'])
   duration_match = (str(duration_Status) + ': ' + str(duration_Break))
    
   freq_Status = (match ['Status'][str('freq')])
   freq_Break = (match ['Break']['freq'])
   freq_match = (str(freq_Status) + ": " + str(freq_Break))
    
   start_times = (match['Status']['start time']) + ": " + (match['Break']['start time'])

   matches = {'Power': [power_match], 'Length': [Length_match], 'Duration': [duration_match], 'Freq': [freq_match], 'Start Times': [start_times]}

我的部分输出示例如下所示;

{'Power': ['high: high'], 'Length': ['Long: Short'], 'Duration': ['2.22: 1.01'], 'Freq': ['0.081229: 0.079503'], 'Start Times': ['10.10.08: 11.09.16']}
{'Power': ['med-high: med-high'], 'Length': ['Long: Long'], 'Duration': ['1.16: 0.81'], 'Freq': ['0.0988: 0.565'], 'Start Times': ['17.08.98: 31:01:03']}

而我希望将它合并到一个字典中,这样我就可以将它保存到单个 csv 文件中

【问题讨论】:

    标签: python dictionary for-loop


    【解决方案1】:

    最简单直接的解决方案是使用defaultdict

    from collections import defaultdict
    
    matches = defaultdict(list)
    
    for salaries, match in matching_salaries_non_employees.items():
    
       Power_match = (match['Status']['Power'])+ ": " + (salaries)
        
       Length_match = (match['Status']['Length'])+ ": " + (match['Break']['Temp'])
            
       duration_Status = (match ['Status'][str('duration')])
       duration_Break = (match ['Break']['duration'])
       duration_match = (str(duration_Status) + ': ' + str(duration_Break))
        
       freq_Status = (match ['Status'][str('freq')])
       freq_Break = (match ['Break']['freq'])
       freq_match = (str(freq_Status) + ": " + str(freq_Break))
        
       start_times = (match['Status']['start time']) + ": " + (match['Break']['start time'])
    
       matches['Power'].append(Power_match)
       matches['Length'].append(Length_match)
       matches['Duration'].append(duration_match)
       matches['Freq'].append(freq_match)
       matches['Start Times'].append(start_times)
    

    【讨论】:

      【解决方案2】:

      @Matias Cicero 的回答很棒,但是显式初始化也很简单:

      keys = ['Power', 'Length', 'Duration', 'Freq', 'Start Times']
      matches = {k: [] for k in keys}
      

      将替换行

      from collections import defaultdict
      matches = defaultdict(list)
      

      【讨论】:

        猜你喜欢
        • 2022-10-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-01-17
        • 2021-12-04
        • 1970-01-01
        • 2020-06-20
        相关资源
        最近更新 更多