我正在尝试找到执行以下循环语句的最佳方法
我有下面的代码,在我检查一列长度并相应地执行操作的地方,它工作得很好。但是我无法优化代码,而不得不创建那么长的if语句
下面是我的代码:
if len(name) == 1:
table.cell(1, 0).text = str(type_1_1)
table.cell(1, 1).text = str(type_1_2)
table.cell(1, 2).text = str(type_1_3)
table.cell(1, 3).text = str(type_1_4)
elif len(name) == 2:
table.cell(1, 0).text = str(type_1_1)
table.cell(1, 1).text = str(type_1_2)
table.cell(1, 2).text = str(type_1_3)
table.cell(1, 3).text = str(type_1_4)
table.cell(2, 0).text = str(type_2_1)
table.cell(2, 1).text = str(type_2_2)
table.cell(2, 2).text = str(type_2_3)
table.cell(2, 3).text = str(type_2_4)
elif len(name) == 3:
table.cell(1, 0).text = str(type_1_1)
table.cell(1, 1).text = str(type_1_2)
table.cell(1, 2).text = str(type_1_3)
table.cell(1, 3).text = str(type_1_4)
table.cell(2, 0).text = str(type_2_1)
table.cell(2, 1).text = str(type_2_2)
table.cell(2, 2).text = str(type_2_3)
table.cell(2, 3).text = str(type_2_4)
table.cell(3, 0).text = str(type_3_1)
table.cell(3, 1).text = str(type_3_2)
table.cell(3, 2).text = str(type_3_3)
table.cell(3, 3).text = str(type_3_4)
elif len(name) == 4:
table.cell(1, 0).text = str(type_1_1)
table.cell(1, 1).text = str(type_1_2)
table.cell(1, 2).text = str(type_1_3)
table.cell(1, 3).text = str(type_1_4)
table.cell(2, 0).text = str(type_2_1)
table.cell(2, 1).text = str(type_2_2)
table.cell(2, 2).text = str(type_2_3)
table.cell(2, 3).text = str(type_2_4)
table.cell(3, 0).text = str(type_3_1)
table.cell(3, 1).text = str(type_3_2)
table.cell(3, 2).text = str(type_3_3)
table.cell(3, 3).text = str(type_3_4)
table.cell(4, 0).text = str(type_4_1)
table.cell(4, 1).text = str(type_4_2)
table.cell(4, 2).text = str(type_4_3)
table.cell(4, 3).text = str(type_4_4)
elif len(name) == 5:
table.cell(1, 0).text = str(type_1_1)
table.cell(1, 1).text = str(type_1_2)
table.cell(1, 2).text = str(type_1_3)
table.cell(1, 3).text = str(type_1_4)
table.cell(2, 0).text = str(type_2_1)
table.cell(2, 1).text = str(type_2_2)
table.cell(2, 2).text = str(type_2_3)
table.cell(2, 3).text = str(type_2_4)
table.cell(3, 0).text = str(type_3_1)
table.cell(3, 1).text = str(type_3_2)
table.cell(3, 2).text = str(type_3_3)
table.cell(3, 3).text = str(type_3_4)
table.cell(4, 0).text = str(type_4_1)
table.cell(4, 1).text = str(type_4_2)
table.cell(4, 2).text = str(type_4_3)
table.cell(4, 3).text = str(type_4_4)
table.cell(5, 0).text = str(type_5_1)
table.cell(5, 1).text = str(type_5_2)
table.cell(5, 2).text = str(type_5_3)
table.cell(5, 3).text = str(type_5_4)
elif len(name) == 6:
table.cell(1, 0).text = str(type_1_1)
table.cell(1, 1).text = str(type_1_2)
table.cell(1, 2).text = str(type_1_3)
table.cell(1, 3).text = str(type_1_4)
table.cell(2, 0).text = str(type_2_1)
table.cell(2, 1).text = str(type_2_2)
table.cell(2, 2).text = str(type_2_3)
table.cell(2, 3).text = str(type_2_4)
table.cell(3, 0).text = str(type_3_1)
table.cell(3, 1).text = str(type_3_2)
table.cell(3, 2).text = str(type_3_3)
table.cell(3, 3).text = str(type_3_4)
table.cell(4, 0).text = str(type_4_1)
table.cell(4, 1).text = str(type_4_2)
table.cell(4, 2).text = str(type_4_3)
table.cell(4, 3).text = str(type_4_4)
table.cell(5, 0).text = str(type_5_1)
table.cell(5, 1).text = str(type_5_2)
table.cell(5, 2).text = str(type_5_3)
table.cell(5, 3).text = str(type_5_4)
table.cell(6, 0).text = str(type_6_1)
table.cell(6, 1).text = str(type_6_2)
table.cell(6, 2).text = str(type_6_3)
table.cell(6, 3).text = str(type_6_4)
elif len(name) == 7:
table.cell(1, 0).text = str(type_1_1)
table.cell(1, 1).text = str(type_1_2)
table.cell(1, 2).text = str(type_1_3)
table.cell(1, 3).text = str(type_1_4)
table.cell(2, 0).text = str(type_2_1)
table.cell(2, 1).text = str(type_2_2)
table.cell(2, 2).text = str(type_2_3)
table.cell(2, 3).text = str(type_2_4)
table.cell(3, 0).text = str(type_3_1)
table.cell(3, 1).text = str(type_3_2)
table.cell(3, 2).text = str(type_3_3)
table.cell(3, 3).text = str(type_3_4)
table.cell(4, 0).text = str(type_4_1)
table.cell(4, 1).text = str(type_4_2)
table.cell(4, 2).text = str(type_4_3)
table.cell(4, 3).text = str(type_4_4)
table.cell(5, 0).text = str(type_5_1)
table.cell(5, 1).text = str(type_5_2)
table.cell(5, 2).text = str(type_5_3)
table.cell(5, 3).text = str(type_5_4)
table.cell(6, 0).text = str(type_6_1)
table.cell(6, 1).text = str(type_6_2)
table.cell(6, 2).text = str(type_6_3)
table.cell(6, 3).text = str(type_6_4)
table.cell(7, 0).text = str(type_7_1)
table.cell(7, 1).text = str(type_7_2)
table.cell(7, 2).text = str(type_7_3)
table.cell(7, 3).text = str(type_7_4)
elif len(name) == 8:
table.cell(1, 0).text = str(type_1_1)
table.cell(1, 1).text = str(type_1_2)
table.cell(1, 2).text = str(type_1_3)
table.cell(1, 3).text = str(type_1_4)
table.cell(2, 0).text = str(type_2_1)
table.cell(2, 1).text = str(type_2_2)
table.cell(2, 2).text = str(type_2_3)
table.cell(2, 3).text = str(type_2_4)
table.cell(3, 0).text = str(type_3_1)
table.cell(3, 1).text = str(type_3_2)
table.cell(3, 2).text = str(type_3_3)
table.cell(3, 3).text = str(type_3_4)
table.cell(4, 0).text = str(type_4_1)
table.cell(4, 1).text = str(type_4_2)
table.cell(4, 2).text = str(type_4_3)
table.cell(4, 3).text = str(type_4_4)
table.cell(5, 0).text = str(type_5_1)
table.cell(5, 1).text = str(type_5_2)
table.cell(5, 2).text = str(type_5_3)
table.cell(5, 3).text = str(type_5_4)
table.cell(6, 0).text = str(type_6_1)
table.cell(6, 1).text = str(type_6_2)
table.cell(6, 2).text = str(type_6_3)
table.cell(6, 3).text = str(type_6_4)
table.cell(7, 0).text = str(type_7_1)
table.cell(7, 1).text = str(type_7_2)
table.cell(7, 2).text = str(type_7_3)
table.cell(7, 3).text = str(type_7_4)
table.cell(8, 0).text = str(type_8_1)
table.cell(8, 1).text = str(type_8_2)
table.cell(8, 2).text = str(type_8_3)
table.cell(8, 3).text = str(type_8_4)
elif len(name) == 9:
table.cell(1, 0).text = str(type_1_1)
table.cell(1, 1).text = str(type_1_2)
table.cell(1, 2).text = str(type_1_3)
table.cell(1, 3).text = str(type_1_4)
table.cell(2, 0).text = str(type_2_1)
table.cell(2, 1).text = str(type_2_2)
table.cell(2, 2).text = str(type_2_3)
table.cell(2, 3).text = str(type_2_4)
table.cell(3, 0).text = str(type_3_1)
table.cell(3, 1).text = str(type_3_2)
table.cell(3, 2).text = str(type_3_3)
table.cell(3, 3).text = str(type_3_4)
table.cell(4, 0).text = str(type_4_1)
table.cell(4, 1).text = str(type_4_2)
table.cell(4, 2).text = str(type_4_3)
table.cell(4, 3).text = str(type_4_4)
table.cell(5, 0).text = str(type_5_1)
table.cell(5, 1).text = str(type_5_2)
table.cell(5, 2).text = str(type_5_3)
table.cell(5, 3).text = str(type_5_4)
table.cell(6, 0).text = str(type_6_1)
table.cell(6, 1).text = str(type_6_2)
table.cell(6, 2).text = str(type_6_3)
table.cell(6, 3).text = str(type_6_4)
table.cell(7, 0).text = str(type_7_1)
table.cell(7, 1).text = str(type_7_2)
table.cell(7, 2).text = str(type_7_3)
table.cell(7, 3).text = str(type_7_4)
table.cell(8, 0).text = str(type_8_1)
table.cell(8, 1).text = str(type_8_2)
table.cell(8, 2).text = str(type_8_3)
table.cell(8, 3).text = str(type_8_4)
table.cell(9, 0).text = str(type_9_1)
table.cell(9, 1).text = str(type_9_2)
table.cell(9, 2).text = str(type_9_3)
table.cell(9, 3).text = str(type_9_4)
elif len(name) == 10:
table.cell(1, 0).text = str(type_1_1)
table.cell(1, 1).text = str(type_1_2)
table.cell(1, 2).text = str(type_1_3)
table.cell(1, 3).text = str(type_1_4)
table.cell(2, 0).text = str(type_2_1)
table.cell(2, 1).text = str(type_2_2)
table.cell(2, 2).text = str(type_2_3)
table.cell(2, 3).text = str(type_2_4)
table.cell(3, 0).text = str(type_3_1)
table.cell(3, 1).text = str(type_3_2)
table.cell(3, 2).text = str(type_3_3)
table.cell(3, 3).text = str(type_3_4)
table.cell(4, 0).text = str(type_4_1)
table.cell(4, 1).text = str(type_4_2)
table.cell(4, 2).text = str(type_4_3)
table.cell(4, 3).text = str(type_4_4)
table.cell(5, 0).text = str(type_5_1)
table.cell(5, 1).text = str(type_5_2)
table.cell(5, 2).text = str(type_5_3)
table.cell(5, 3).text = str(type_5_4)
table.cell(6, 0).text = str(type_6_1)
table.cell(6, 1).text = str(type_6_2)
table.cell(6, 2).text = str(type_6_3)
table.cell(6, 3).text = str(type_6_4)
table.cell(7, 0).text = str(type_7_1)
table.cell(7, 1).text = str(type_7_2)
table.cell(7, 2).text = str(type_7_3)
table.cell(7, 3).text = str(type_7_4)
table.cell(8, 0).text = str(type_8_1)
table.cell(8, 1).text = str(type_8_2)
table.cell(8, 2).text = str(type_8_3)
table.cell(8, 3).text = str(type_8_4)
table.cell(9, 0).text = str(type_9_1)
table.cell(9, 1).text = str(type_9_2)
table.cell(9, 2).text = str(type_9_3)
table.cell(9, 3).text = str(type_9_4)
table.cell(10, 0).text = str(type_10_1)
table.cell(10, 1).text = str(type_10_2)
table.cell(10, 2).text = str(type_10_3)
table.cell(10, 3).text = str(type_10_4)
elif len(name) == 11:
table.cell(1, 0).text = str(type_1_1)
table.cell(1, 1).text = str(type_1_2)
table.cell(1, 2).text = str(type_1_3)
table.cell(1, 3).text = str(type_1_4)
table.cell(2, 0).text = str(type_2_1)
table.cell(2, 1).text = str(type_2_2)
table.cell(2, 2).text = str(type_2_3)
table.cell(2, 3).text = str(type_2_4)
table.cell(3, 0).text = str(type_3_1)
table.cell(3, 1).text = str(type_3_2)
table.cell(3, 2).text = str(type_3_3)
table.cell(3, 3).text = str(type_3_4)
table.cell(4, 0).text = str(type_4_1)
table.cell(4, 1).text = str(type_4_2)
table.cell(4, 2).text = str(type_4_3)
table.cell(4, 3).text = str(type_4_4)
table.cell(5, 0).text = str(type_5_1)
table.cell(5, 1).text = str(type_5_2)
table.cell(5, 2).text = str(type_5_3)
table.cell(5, 3).text = str(type_5_4)
table.cell(6, 0).text = str(type_6_1)
table.cell(6, 1).text = str(type_6_2)
table.cell(6, 2).text = str(type_6_3)
table.cell(6, 3).text = str(type_6_4)
table.cell(7, 0).text = str(type_7_1)
table.cell(7, 1).text = str(type_7_2)
table.cell(7, 2).text = str(type_7_3)
table.cell(7, 3).text = str(type_7_4)
table.cell(8, 0).text = str(type_8_1)
table.cell(8, 1).text = str(type_8_2)
table.cell(8, 2).text = str(type_8_3)
table.cell(8, 3).text = str(type_8_4)
table.cell(9, 0).text = str(type_9_1)
table.cell(9, 1).text = str(type_9_2)
table.cell(9, 2).text = str(type_9_3)
table.cell(9, 3).text = str(type_9_4)
table.cell(10, 0).text = str(type_10_1)
table.cell(10, 1).text = str(type_10_2)
table.cell(10, 2).text = str(type_10_3)
table.cell(10, 3).text = str(type_10_4)
table.cell(11, 0).text = str(type_11_1)
table.cell(11, 1).text = str(type_11_2)
table.cell(11, 2).text = str(type_11_3)
table.cell(11, 3).text = str(type_11_4)
elif len(name) == 12:
table.cell(1, 0).text = str(type_1_1)
table.cell(1, 1).text = str(type_1_2)
table.cell(1, 2).text = str(type_1_3)
table.cell(1, 3).text = str(type_1_4)
table.cell(2, 0).text = str(type_2_1)
table.cell(2, 1).text = str(type_2_2)
table.cell(2, 2).text = str(type_2_3)
table.cell(2, 3).text = str(type_2_4)
table.cell(3, 0).text = str(type_3_1)
table.cell(3, 1).text = str(type_3_2)
table.cell(3, 2).text = str(type_3_3)
table.cell(3, 3).text = str(type_3_4)
table.cell(4, 0).text = str(type_4_1)
table.cell(4, 1).text = str(type_4_2)
table.cell(4, 2).text = str(type_4_3)
table.cell(4, 3).text = str(type_4_4)
table.cell(5, 0).text = str(type_5_1)
table.cell(5, 1).text = str(type_5_2)
table.cell(5, 2).text = str(type_5_3)
table.cell(5, 3).text = str(type_5_4)
table.cell(6, 0).text = str(type_6_1)
table.cell(6, 1).text = str(type_6_2)
table.cell(6, 2).text = str(type_6_3)
table.cell(6, 3).text = str(type_6_4)
table.cell(7, 0).text = str(type_7_1)
table.cell(7, 1).text = str(type_7_2)
table.cell(7, 2).text = str(type_7_3)
table.cell(7, 3).text = str(type_7_4)
table.cell(8, 0).text = str(type_8_1)
table.cell(8, 1).text = str(type_8_2)
table.cell(8, 2).text = str(type_8_3)
table.cell(8, 3).text = str(type_8_4)
table.cell(9, 0).text = str(type_9_1)
table.cell(9, 1).text = str(type_9_2)
table.cell(9, 2).text = str(type_9_3)
table.cell(9, 3).text = str(type_9_4)
table.cell(10, 0).text = str(type_10_1)
table.cell(10, 1).text = str(type_10_2)
table.cell(10, 2).text = str(type_10_3)
table.cell(10, 3).text = str(type_10_4)
table.cell(11, 0).text = str(type_11_1)
table.cell(11, 1).text = str(type_11_2)
table.cell(11, 2).text = str(type_11_3)
table.cell(11, 3).text = str(type_11_4)
table.cell(12, 0).text = str(type_12_1)
table.cell(12, 1).text = str(type_12_2)
table.cell(12, 2).text = str(type_12_3)
table.cell(12, 3).text = str(type_12_4)
elif len(name) == 13:
table.cell(1, 0).text = str(type_1_1)
table.cell(1, 1).text = str(type_1_2)
table.cell(1, 2).text = str(type_1_3)
table.cell(1, 3).text = str(type_1_4)
table.cell(2, 0).text = str(type_2_1)
table.cell(2, 1).text = str(type_2_2)
table.cell(2, 2).text = str(type_2_3)
table.cell(2, 3).text = str(type_2_4)
table.cell(3, 0).text = str(type_3_1)
table.cell(3, 1).text = str(type_3_2)
table.cell(3, 2).text = str(type_3_3)
table.cell(3, 3).text = str(type_3_4)
table.cell(4, 0).text = str(type_4_1)
table.cell(4, 1).text = str(type_4_2)
table.cell(4, 2).text = str(type_4_3)
table.cell(4, 3).text = str(type_4_4)
table.cell(5, 0).text = str(type_5_1)
table.cell(5, 1).text = str(type_5_2)
table.cell(5, 2).text = str(type_5_3)
table.cell(5, 3).text = str(type_5_4)
table.cell(6, 0).text = str(type_6_1)
table.cell(6, 1).text = str(type_6_2)
table.cell(6, 2).text = str(type_6_3)
table.cell(6, 3).text = str(type_6_4)
table.cell(7, 0).text = str(type_7_1)
table.cell(7, 1).text = str(type_7_2)
table.cell(7, 2).text = str(type_7_3)
table.cell(7, 3).text = str(type_7_4)
table.cell(8, 0).text = str(type_8_1)
table.cell(8, 1).text = str(type_8_2)
table.cell(8, 2).text = str(type_8_3)
table.cell(8, 3).text = str(type_8_4)
table.cell(9, 0).text = str(type_9_1)
table.cell(9, 1).text = str(type_9_2)
table.cell(9, 2).text = str(type_9_3)
table.cell(9, 3).text = str(type_9_4)
table.cell(10, 0).text = str(type_10_1)
table.cell(10, 1).text = str(type_10_2)
table.cell(10, 2).text = str(type_10_3)
table.cell(10, 3).text = str(type_10_4)
table.cell(11, 0).text = str(type_11_1)
table.cell(11, 1).text = str(type_11_2)
table.cell(11, 2).text = str(type_11_3)
table.cell(11, 3).text = str(type_11_4)
table.cell(12, 0).text = str(type_12_1)
table.cell(12, 1).text = str(type_12_2)
table.cell(12, 2).text = str(type_12_3)
table.cell(12, 3).text = str(type_12_4)
table.cell(13, 0).text = str(type_13_1)
table.cell(13, 1).text = str(type_13_2)
table.cell(13, 2).text = str(type_13_3)
table.cell(13, 3).text = str(type_13_4)
elif len(name) == 14:
table.cell(1, 0).text = str(type_1_1)
table.cell(1, 1).text = str(type_1_2)
table.cell(1, 2).text = str(type_1_3)
table.cell(1, 3).text = str(type_1_4)
table.cell(2, 0).text = str(type_2_1)
table.cell(2, 1).text = str(type_2_2)
table.cell(2, 2).text = str(type_2_3)
table.cell(2, 3).text = str(type_2_4)
table.cell(3, 0).text = str(type_3_1)
table.cell(3, 1).text = str(type_3_2)
table.cell(3, 2).text = str(type_3_3)
table.cell(3, 3).text = str(type_3_4)
table.cell(4, 0).text = str(type_4_1)
table.cell(4, 1).text = str(type_4_2)
table.cell(4, 2).text = str(type_4_3)
table.cell(4, 3).text = str(type_4_4)
table.cell(5, 0).text = str(type_5_1)
table.cell(5, 1).text = str(type_5_2)
table.cell(5, 2).text = str(type_5_3)
table.cell(5, 3).text = str(type_5_4)
table.cell(6, 0).text = str(type_6_1)
table.cell(6, 1).text = str(type_6_2)
table.cell(6, 2).text = str(type_6_3)
table.cell(6, 3).text = str(type_6_4)
table.cell(7, 0).text = str(type_7_1)
table.cell(7, 1).text = str(type_7_2)
table.cell(7, 2).text = str(type_7_3)
table.cell(7, 3).text = str(type_7_4)
table.cell(8, 0).text = str(type_8_1)
table.cell(8, 1).text = str(type_8_2)
table.cell(8, 2).text = str(type_8_3)
table.cell(8, 3).text = str(type_8_4)
table.cell(9, 0).text = str(type_9_1)
table.cell(9, 1).text = str(type_9_2)
table.cell(9, 2).text = str(type_9_3)
table.cell(9, 3).text = str(type_9_4)
table.cell(10, 0).text = str(type_10_1)
table.cell(10, 1).text = str(type_10_2)
table.cell(10, 2).text = str(type_10_3)
table.cell(10, 3).text = str(type_10_4)
table.cell(11, 0).text = str(type_11_1)
table.cell(11, 1).text = str(type_11_2)
table.cell(11, 2).text = str(type_11_3)
table.cell(11, 3).text = str(type_11_4)
table.cell(12, 0).text = str(type_12_1)
table.cell(12, 1).text = str(type_12_2)
table.cell(12, 2).text = str(type_12_3)
table.cell(12, 3).text = str(type_12_4)
table.cell(13, 0).text = str(type_13_1)
table.cell(13, 1).text = str(type_13_2)
table.cell(13, 2).text = str(type_13_3)
table.cell(13, 3).text = str(type_13_4)
table.cell(14, 0).text = str(type_14_1)
table.cell(14, 1).text = str(type_14_2)
table.cell(14, 2).text = str(type_14_3)
table.cell(14, 3).text = str(type_14_4)
答案 0 :(得分:1)
看来,您的所有价值观都是相互依存的。也就是说,长度为N的功等于长度为N-1的功,再加上一点功。考虑到这一点,请考虑使您的if
语句非排他性,例如:
if len(name) >= 1:
table.cell(1, 0).text = str(type_1_1)
table.cell(1, 1).text = str(type_1_2)
table.cell(1, 2).text = str(type_1_3)
table.cell(1, 3).text = str(type_1_4)
# Note: NOT elif, just if
if len(name) >= 2:
table.cell(2, 0).text = str(type_2_1)
table.cell(2, 1).text = str(type_2_2)
table.cell(2, 2).text = str(type_2_3)
table.cell(2, 3).text = str(type_2_4)
if len(name) == 3:
table.cell(3, 0).text = str(type_3_1)
table.cell(3, 1).text = str(type_3_2)
table.cell(3, 2).text = str(type_3_3)
table.cell(3, 3).text = str(type_3_4)
现在有一个很好的重复模式。下一个问题是各种type_X_Y
变量被存储为单独的名称,而不是使用列表或字典。可以做几件事,但在提供更多数据之前,我们可以忽略它。相反,让我们创建一个设置一行表格单元格数据的函数:
def set_table_row(table, rownum, *values):
assert len(values) == 4, "4 values are required per row"
for col, value in enumerate(values):
table.cell(rownum, col) = str(value)
现在,您可以将每个块中的4条语句折叠为一个辅助函数调用:
if len(name) >= 1:
set_table_row(table, 1, type_1_1, type_1_2, type_1_3, type_1_4)
if len(name) >= 2:
set_table_row(table, 2, type_2_1, type_2_2, type_2_3, type_2_4)
if len(name) == 3:
set_table_row(table, 3, type_3_1, type_3_2, type_3_3, type_3_4)
这对您来说足够了。如果不是,则需要提供有关各种type_X_Y
值是什么的更多信息:它们是全局变量还是局部变量,对象的属性,从元组提取的值?
答案 1 :(得分:0)
考虑将多维数组用于值或字典而不是自定义变量,但是您可以这样做:
for x in range(len(name)):
table.cell(x, 0).text = str(locals()["type_" + x + "_1"])
table.cell(x, 1).text = str(locals()["type_" + x + "_2"])
table.cell(x, 2).text = str(locals()["type_" + x + "_3"])
table.cell(x, 3).text = str(locals()["type_" + x + "_4"])
或另一个循环:
for i in range(len(name)):
for j in range(4):
table.cell(i, j).text = str(locals()["type_" + i + "_" + j])
多维数组可以这样完成:
types = [[]]
types[0][0] = "foo"
types[0][1] = "bar"
# ...
types[1][0] = ".."
for i in range(len(name)):
for j in range(4):
table.cell(i, j).text = types[i][j]
答案 2 :(得分:0)
您可以使用迭代器:
from itertools import cycle, count
types_ = ((k, v) for k, v in dict(locals()).items() if k.startswith('type_'))
types = [type for _, type in sorted(types_)]
### Alternatively you can define the list with types manually
### types = [type_1_1, type_1_2, type_1_3, type_1_4...]
def func(name):
# assert len(name) <= len(types)
r = (i for i in count(start=1) for _ in range(4))
c = cycle(range(4))
t = iter(types)
for _ in range(len(name) * 4):
table.cell(next(r), next(c)).text = str(next(t))
types = [f'type_{i}_{j}' for i in range(1, 100) for j in range(1, 5)]
# ['type_1_1', 'type_1_2', 'type_1_3', 'type_1_4', 'type_2_1', 'type_2_2', 'type_2_3', 'type_2_4'...]
def func(name):
r = (i for i in count(start=1) for _ in range(4))
c = cycle(range(4))
t = iter(types)
for _ in range(len(name) * 4):
print(f'table.cell({next(r)}, {next(c)}).text = str({next(t)})')
func('AB')
table.cell(1, 0).text = str(type_1_1)
table.cell(1, 1).text = str(type_1_2)
table.cell(1, 2).text = str(type_1_3)
table.cell(1, 3).text = str(type_1_4)
table.cell(2, 0).text = str(type_2_1)
table.cell(2, 1).text = str(type_2_2)
table.cell(2, 2).text = str(type_2_3)
table.cell(2, 3).text = str(type_2_4)