下面是我的代码,需要很长时间才能执行。如何在Python的列表理解中实现它以提高速度和效率?
buildings=[]
for bi in range(1449):
for si in range (16):
for m in range(3):
a= train[(train['building_id']==bi)&(train['site_id']==si)&(train['meter']==m)]
if not a.empty:
buildings.append(a.values)
答案 0 :(得分:1)
在没有样本数据的情况下很难说出这是否正确,但这在理论上应该可行:
buildings = [
x for x in
[
train[(train['building_id']==bi)&(train['site_id']==si)&(train['meter']==m)].values
for bi in range(1449)
for si in range(16)
for m in range(3)
]
if not x.empty
]