[英]Iterating over a list of lists and checking for a condition Python
我有一个数据 object 是我通过操作一个更大的数据集创建的。 它是一个列表列表,其中最高级别是星期几(1-7,但为简洁起见,我只包括第一天),第二级别是每天的时间。 object 看起来像这样:
act_all_fun = [[[datetime.time(0, 0), 254, 'F37', 'd1'],
[datetime.time(2, 0), 254, 'F7', 'd1'],
[datetime.time(5, 0), 254, 'F17', 'd1'],
[datetime.time(6, 30), 254, 'F37', 'd1'],
[datetime.time(7, 0), 15, 'F37', 'd1'],
[datetime.time(10, 0), 17, 'F37', 'd0'],
[datetime.time(12, 0), 19, 'F37', 'd0'],
[datetime.time(21, 0), 15, 'F7', 'd1'],
[datetime.time(23, 0), 254, 'F37', 'd1']],
[[datetime.time(0, 0), 254, 'F37', 'd1'],
[datetime.time(1, 30), 254, 'F', 'd1'],
[datetime.time(5, 0), 2, 'F7', 'd1'],
[datetime.time(6, 30), 2, 'F37', 'd0'],
[datetime.time(8, 0), 7, 'F37', 'd0'],
[datetime.time(9, 15), 11, 'F', 'd0'],
[datetime.time(10, 15), 15, 'F', 'd0'],
[datetime.time(15, 30), 5, 'F37', 'd0'],
[datetime.time(16, 15), 6, 'F', 'd0'],
[datetime.time(19, 30), 9, 'F37', 'd0'],
[datetime.time(23, 0), 254, 'F37', 'd1']]]
我想遍历 act_all_fun 中的每个特定列表(在时间级别)并且:
例如,所需的 output 将是:
act_all_fun = [[[datetime.time(0, 0), 254, 'F37', 'd1'],
[datetime.time(2, 0), 253, 'F7', 'd1'],
[datetime.time(5, 0), 252, 'F17', 'd1'],
[datetime.time(6, 30), 254, 'F37', 'd1'],
[datetime.time(7, 0), 15, 'F37', 'd1'],
[datetime.time(10, 0), 17, 'F37', 'd0'],
[datetime.time(12, 0), 19, 'F37', 'd0'],
[datetime.time(21, 0), 45, 'F7', 'd1'],
[datetime.time(23, 0), 254, 'F37', 'd1']],
[[datetime.time(0, 0), 254, 'F37', 'd1'],
[datetime.time(1, 30), 251, 'F', 'd1'],
[datetime.time(5, 0), 2, 'F7', 'd1'],
[datetime.time(6, 30), 2, 'F37', 'd0'],
[datetime.time(8, 0), 7, 'F37', 'd0'],
[datetime.time(9, 15), 11, 'F', 'd0'],
[datetime.time(10, 15), 75, 'F', 'd0'],
[datetime.time(15, 30), 5, 'F37', 'd0'],
[datetime.time(16, 15), 6, 'F', 'd0'],
[datetime.time(19, 30), 9, 'F37', 'd0'],
[datetime.time(23, 0), 254, 'F37', 'd1']]]
为便于参考,查output at: a-day 1次:(0,0)/(2,0), (5,0)/(6,30)/(21,0) b-day 2次: (1,30)/(10,15)
我试过这样的事情:
for day in act_all_fun:
for act in day:
if act[1] in range(240,255) and act[2] not in [x[2] for x in day if x != act]:
act[1] = act[1] - 1
但这并没有产生我预期的结果。
我尝试了多种不同的方法,从将列表展平到将其放入 dataframe 但无济于事。 我也试图尽可能清楚,但意识到我可能没有做到。 如果您需要任何说明,请告诉我。
好吧,经过大量工作并尝试多次迭代,从概念上讲,这个问题可以通过以下方式解决:a- 扁平化列表 b- 创建我们想要比较的项目对 c- 索引我们得到的匹配项 d- 处理(即操纵我们需要的值)满足既定条件的项目。
这是代码,希望它能帮助其他人解决这个问题,或者这个问题的任何其他衍生问题:
#flattening the list for ease of operation
def flatten(input_list):
flat_list = []
for sublist in input_list:
for item in sublist:
flat_list.append(item)
return flat_list
act_flt = flatten(flatten(act_all_fun))
# Creating unique pattern-based actions
unq_act = []
idx_act = []
for i in range(len(act_flt)):
if isinstance(act_flt[i], int) and act_flt[i] != 254:
unq_act.append([act_flt[i], act_flt[i+1]])
idx_act.append(i)
unique_second_elements = []
for item in new_list:
if item[1] not in unique_second_elements:
unique_second_elements.append(item[1])
for second_element in unique_second_elements:
for item in unq_act:
if item[1] == second_element:
item[0] += (unique_second_elements.index(second_element) * 30)
# Creating unique free pattern-based actions
unq_free = []
idx_free = []
for i in range(len(act_flt)):
if isinstance(act_flt[i], int) and act_flt[i] == 254:
unq_free.append([act_flt[i], act_flt[i+1]])
idx_free.append(i)
unique_second_elements = []
for item in unq_free:
if item[1] not in unique_second_elements:
unique_second_elements.append(item[1])
for second_element in unique_second_elements:
for item in unq_free:
if item[1] == second_element:
item[0] += (unique_second_elements.index(second_element) - 2)
现在处理之前的输出:
dow1 = len(act_all_fun[0])*4 -1
dow2 = len(act_all_fun[1])*4
dow3 = len(act_all_fun[2])*4
dow4 = len(act_all_fun[3])*4
dow5 = len(act_all_fun[4])*4
dow6 = len(act_all_fun[5])*4
dow7 = len(act_all_fun[6])*4
for i in idx_act:
if 0<=i<=dow1:
day = 0
act_tmp = i//4 #finding the action entry
act_all_fun[day][act_tmp][1] = unq_act[idx_act.index(i)][0]
print(i)
elif dow1<i<=(dow1+dow2):
day = 1
act_tmp = (i-dow1)//4 #finding the action entry
act_all_fun[day][act_tmp][1] = unq_act[idx_act.index(i)][0]
print(i)
elif (dow1+dow2)<i<=(dow1+dow2+dow3):
day = 2
act_tmp = (i-(dow1+dow2))//4
act_all_fun[day][act_tmp][1] = unq_act[idx_act.index(i)][0]
print(i)
elif dow1+dow2+dow3<i<=(dow1+dow2+dow3+dow4):
day = 3
act_tmp = (i-(dow1+dow2+dow3))//4
act_all_fun[day][act_tmp][1] = unq_act[idx_act.index(i)][0]
print(i)
elif dow1+dow2+dow3+dow4<i<=(dow1+dow2+dow3+dow4+dow5):
day = 4
act_tmp = (i-(dow1+dow2+dow3+dow4))//4
act_all_fun[day][act_tmp][1] = unq_act[idx_act.index(i)][0]
print(i)
elif dow1+dow2+dow3+dow4+dow5<i<=(dow1+dow2+dow3+dow4+dow5+dow6):
day = 5
act_tmp = (i-(dow1+dow2+dow3+dow4+dow5))//4
act_all_fun[day][act_tmp][1] = unq_act[idx_act.index(i)][0]
print(i)
else:
day = 6
act_tmp = (i-(dow1+dow2+dow3+dow4+dow5+dow6))//4
act_all_fun[day][act_tmp][1] = unq_act[idx_act.index(i)][0]
print(i)
for j in idx_free:
if 0<=j<=dow1:
day = 0
act_tmp = j//4
act_all_fun[day][act_tmp][1] = unq_free[idx_free.index(j)][0]
print(j)
elif dow1<j<=(dow1+dow2):
day = 1
act_tmp = (j-dow1)//4
act_all_fun[day][act_tmp][1] = unq_free[idx_free.index(j)][0]
print(j)
elif dow1+dow2<j<=(dow1+dow2+dow3):
day = 2
act_tmp = (j-(dow1+dow2))//4
act_all_fun[day][act_tmp][1] = unq_free[idx_free.index(j)][0]
print(j)
elif dow1+dow2+dow3<j<=(dow1+dow2+dow3+dow4):
day = 3
act_tmp = (j-(dow1+dow2+dow3))//4
act_all_fun[day][act_tmp][1] = unq_free[idx_free.index(j)][0]
print(j)
elif dow1+dow2+dow3+dow4<j<=(dow1+dow2+dow3+dow4+dow5):
day = 4
act_tmp = (j-(dow1+dow2+dow3+dow4))//4
act_all_fun[day][act_tmp][1] = unq_free[idx_free.index(j)][0]
print(j)
elif dow1+dow2+dow3+dow4+dow5<j<=(dow1+dow2+dow3+dow4+dow5+dow6):
day = 5
act_tmp = (j-(dow1+dow2+dow3+dow4+dow5))//4
act_all_fun[day][act_tmp][1] = unq_free[idx_free.index(j)][0]
print(j)
else:
day = 6
act_tmp = (j-(dow1+dow2+dow3+dow4+dow5+dow6))//4
act_all_fun[day][act_tmp][1] = unq_free[idx_free.index(j)][0]
print(j)
随意从中提取您需要的内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.