优化这段代码col = [8, 9, 10] name_list1 = ['bags', 'laundry'] name_list2 = ['outdoor_garden', 'toys'] name_list3 = ['media', 'monitors'] name_list4 = ['fashion', 'lighting'] if cate in name_list1: copydata(24, 3, worksheet, new_worksheet) elif cate in name_list2: copydata(25, 3, worksheet, new_worksheet) elif cate in name_list3: copydata(34, 3, worksheet, new_worksheet) elif cate in name_list4: copydata(57, 3, worksheet, new_worksheet) elif cate == "data_storage": copydata(10, 3, worksheet, new_worksheet) elif cate == "health": copydata(20, 3, worksheet, new_worksheet) elif cate == "sports_shoes": copydata(21, 3, worksheet, new_worksheet)
时间: 2023-04-07 14:05:14 浏览: 100
这是一个关于代码优化的问题,可以回答。可以将name_list1到name_list4合并成一个列表,然后使用index()方法查找cate在列表中的位置,再根据位置调用相应的copydata()函数。代码如下:
col = [8, 9, 10]
name_list = [['bags', 'laundry'], ['outdoor_garden', 'toys'], ['media', 'monitors'], ['fashion', 'lighting']]
cate_index = -1
for i in range(len(name_list)):
if cate in name_list[i]:
cate_index = i
break
if cate_index == 0:
copydata(24, 3, worksheet, new_worksheet)
elif cate_index == 1:
copydata(25, 3, worksheet, new_worksheet)
elif cate_index == 2:
copydata(34, 3, worksheet, new_worksheet)
elif cate_index == 3:
copydata(57, 3, worksheet, new_worksheet)
elif cate == "data_storage":
copydata(10, 3, worksheet, new_worksheet)
elif cate == "health":
copydata(20, 3, worksheet, new_worksheet)
elif cate == "sports_shoes":
copydata(21, 3, worksheet, new_worksheet)
阅读全文