简体   繁体   English

在Python中开发更有效的数组和for循环脚本

[英]Developing a more efficient array and for loop script in Python

I'm working in python to iterate through some arrays and then fill cells in rows after each array data point has been written from the array into excel. 我正在python中进行迭代,以遍历某些数组,然后在将每个数组数据点从数组写入excel后,将行中的单元格填充。

Currently, my script works fine, but it looks terrible and am wondering a more efficient way I could code this. 目前,我的脚本可以正常运行,但是看起来很糟糕,并且想知道我可以使用哪种更有效的方法进行编码。 Should I put all the lists into an ordered dictionary? 我应该将所有列表放入有序词典中吗? I'm mainly looking to get rid of the crazy amount of for loops. 我主要是想摆脱疯狂的for循环。

Array Definitions 数组定义

delay_time_data             = []
step_size_data              = [1,1,1,1,1]
first_uncorrectable_data    = []
second_uncorrectable_data   = []
erasure_decode_data         = []
ecc_hibit_data              = []
bits_corrected_data         = []
bits_read_data              = []
avg_rber_data               = []
avg_rber_ed_data            = []
ed_success_data             = []

Loops 循环

#Fill in the logged data from the test ouput under each delay_time column
    for each_item in step_size_data:
        current_cell_iteration = '%s%d'%(xlsx_cols[current_letter], column_iterator) #STARTING AT CURRENT CELL OF 
        overall_stats_sheet[current_cell_iteration] = each_item
        current_letter+=1

    current_letter      = 1
    column_iterator     +=1

    for each_item in first_uncorrectable_data:
        current_cell_iteration = '%s%d'%(xlsx_cols[current_letter], column_iterator) #STARTING AT CURRENT CELL OF 
        overall_stats_sheet[current_cell_iteration] = each_item
        current_letter+=1

    current_letter      = 1
    column_iterator     +=1

    for each_item in second_uncorrectable_data:
        current_cell_iteration = '%s%d'%(xlsx_cols[current_letter], column_iterator) #STARTING AT CURRENT CELL OF 
        overall_stats_sheet[current_cell_iteration] = each_item
        current_letter+=1

    current_letter      = 1
    column_iterator     +=1

    for each_item in erasure_decode_data:
        current_cell_iteration = '%s%d'%(xlsx_cols[current_letter], column_iterator) #STARTING AT CURRENT CELL OF 
        overall_stats_sheet[current_cell_iteration] = each_item
        current_letter+=1

    current_letter      = 1
    column_iterator     +=1

    for each_item in ecc_hibit_data:
        current_cell_iteration = '%s%d'%(xlsx_cols[current_letter], column_iterator) #STARTING AT CURRENT CELL OF 
        overall_stats_sheet[current_cell_iteration] = each_item
        current_letter+=1

    current_letter      = 1
    column_iterator     +=1

    for each_item in bits_corrected_data:
        current_cell_iteration = '%s%d'%(xlsx_cols[current_letter], column_iterator) #STARTING AT CURRENT CELL OF 
        overall_stats_sheet[current_cell_iteration] = each_item
        current_letter+=1

    current_letter      = 1
    column_iterator     +=1

    for each_item in bits_read_data:
        current_cell_iteration = '%s%d'%(xlsx_cols[current_letter], column_iterator) #STARTING AT CURRENT CELL OF 
        overall_stats_sheet[current_cell_iteration] = each_item
        current_letter+=1

    current_letter      = 1
    column_iterator     +=1

    for each_item in avg_rber_data:
        current_cell_iteration = '%s%d'%(xlsx_cols[current_letter], column_iterator) #STARTING AT CURRENT CELL OF 
        overall_stats_sheet[current_cell_iteration] = each_item
        current_letter+=1

    current_letter      = 1
    column_iterator     +=1

    for each_item in avg_rber_ed_data:
        current_cell_iteration = '%s%d'%(xlsx_cols[current_letter], column_iterator) #STARTING AT CURRENT CELL OF 
        overall_stats_sheet[current_cell_iteration] = each_item
        current_letter+=1

As I said in the comments, "Make a dict of the lists, iterate through it with a for loop and then nest the for loop you are currently using." 正如我在评论中所说:“对列表进行排序,使用for循环遍历列表,然后嵌套当前使用的for循环。”

data = {
    'delay_time': [],
    'step_size': [1,1,1,1,1],
    'first_uncorrectable': [],
    'second_uncorrectable': [],
    'erasure_decode': [],
    'ecc_hibit': [],
    'bits_corrected': [],
    'bits_read': [],
    'avg_rber': [],
    'avg_rber_ed': [],
    'ed_success': []
}

for data_set in data:
    for each_item in data[data_set]:
        current_cell_iteration = '%s%d'%(xlsx_cols[current_letter], column_iterator) #STARTING AT CURRENT CELL OF
        overall_stats_sheet[current_cell_iteration] = each_item
        current_letter += 1
    current_letter = 1
    column_iterator += 1

Hope this helps! 希望这可以帮助!

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

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