使用其他2个嵌套列表中的项目填充嵌套列表|蟒蛇

时间:2019-03-10 19:14:08

标签: python list indexing iterator iteration

编辑

我有2个嵌套列表,例如:

babel-plugin-istanbul

它们具有精确的形状,这意味着:两个主列表中给定索引中的列表具有相同数量的元素。

我想创建一个嵌套的“主列表”,其中所有列表将具有相同数量的元素,并且这些元素将从list_of_start_times和list_of_temperatures中填充:

cluster_list_all_thermostat_starts = [[0, 1000, 1200, 1500, 7000], [2,300,400],[4,500,765,100,2500,3000]] list_all_thermostat_set_points = [[20, 20, 20.5, 19.5, 19], [20,23.5,19.5],[20,20,19.5,20,25,21.5]]

master_list =[[temperature,temperature,temperature,...,temperature], [temperature,temperature,temperature,...,temperature], [temperature,temperature,temperature,...,temperature]]应该有3个列表,每个列表具有相同数量的元素。

我现在是这样的:

master_list

全部:master_list_of_quartiles = [] for domek in range(len(list_of_start_times)): master_list = [0]*24*365*4 temperatures = list_all_thermostat_set_points[domek] house_list_start_charge = cluster_list_all_thermostat_starts[domek] house_list_end_charge = cluster_list_all_thermostat_starts[domek] #the same list is used because the thermostat only has start times #print(house_list_start_charge) for kwadrans in range(len(house_list_start_charge)): print(kwadrans) start_charge = int(house_list_start_charge[kwadrans]) print(start_charge) try: end_charge = int(house_list_end_charge[kwadrans+1]) - 1 except IndexError: end_charge = 4*24*365 print(end_charge) this_temp = temperatures[kwadrans] print(this_temp) for j in range(start_charge , end_charge): master_list[j]=this_temp master_list_of_quartiles.append(master_list) 显示出良好的价值,但填写master_list无效,因为它用最后一个迭代的项目填充了它。

目标是稍后将print(start_charge), print(this_temp), print(end_charge)转换为3列和24 * 365 * 4索引的数据框。

如何在保持正确索引的同时进行修复?

1 个答案:

答案 0 :(得分:0)

我设法解决了这个问题!

for domek in range(len(house_has_thermostat)):

master_list = []
temperatures = list_all_thermostat_set_points[domek]

house_list_start_charge = cluster_list_all_thermostat_starts[domek]
house_list_end_charge = cluster_list_all_thermostat_starts[domek] #the same list is used because the thermostat only has start times


for kwadrans in range(len(house_list_start_charge)):
    print(kwadrans)
    start_charge = int(house_list_start_charge[kwadrans])
    print(start_charge)
    try:
        end_charge = int(house_list_end_charge[kwadrans+1]) - 1
    except IndexError:
        end_charge = 4*24*365   
    print(end_charge)
    this_temp = temperatures[kwadrans]
    print(this_temp)


    for j in range(start_charge , end_charge):
        master_list.extend([this_temp])
while len(master_list)<4*24*365:
    master_list.extend([this_temp])
master_list_of_quartiles.append(master_list)