为什么我的列表形状在并发的期货循环中发生变化



有人能告诉我为什么第二次循环时我可以格式化我的numpy列表吗?这就是我犯的错误。

(venv) C:UsersENG-DESKTOP-4PycharmProjectsDelay Tracker>python -m flight_weather_1
Shape of list is : (500, 1, 10) 
shape of unformatted numpy is (500, 1, 10)
shape of formatted numpy is (500, 10)
Shape of list is : (500,)
shape of unformatted numpy is (500,)

Traceback(最后一次调用(:文件";C: \Program Files\Python38\lib\runpy.py";,第194行,在_run_module_as_main中return _run_code(code,main_globals,None,文件";C: \Program Files\Python38\lib\runpy.py";,第87行,在_run_code中exec(代码,run_globals(文件";C: \Users\ENG-DESKTOP-4\PycharmProjects\Delay Tracker\flight_weather_1.py";,第121行,inresults_formatt=results_houu.整形(500,10(

ValueError:无法将大小为500的数组整形为形状(500,10(

def index_weather(index):
Iata_ = (first_mil.iloc[index]['ORIGIN'])
flight_time_ = (first_mil.iloc[index]['FL_DATE'])
weather_data = weather_from_IATA(Iata_, flight_time_)
return weather_data

for i in range(2):
index_ = list(range(1 + (i * 500), (500 + 1) + (i * 500)))
with concurrent.futures.ThreadPoolExecutor() as executor:
secs = index_
results = executor.map(index_weather, secs)
results_list = list(results)
print("Shape of list is : " + str(np.shape(results_list)))
results_thou = np.array(results_list)
results_list.clear()
print("shape of unformatted numpy is", np.shape(results_thou))
results_formatted = results_thou.reshape(500, 10)
print("shape of formatted numpy is", np.shape(results_formatted))
results_pd = pd.DataFrame(results_formatted)
pd.concat((all2, results_pd), axis=0)

我想这里可能已经有答案了。

来自Kellem Negasi的回答:

"当您重塑numpy数组时,总数元素不应该改变。例如a=[2,3,4,5,1,7]如果你想将其重塑为2D阵列,那么乘以的维度应该等于原始阵列a中的元素总数;

根据你的输出,我猜你已经试图分配未格式化的数字从您的错误输出:

ValueError: cannot reshape array of size 500 into shape (500,10), 

一直返回到长度为500个元素的index_变量希望这对有帮助

即使没有concurrent的东西:

In [32]: for i in range(2):
...:     index_ = list(range(1 + (i * 500), (500 + 1) + (i * 500)))
...: 
...:     results = index_.copy()
...:     results_list = list(results)
...:     print("Shape of list is : " + str(np.shape(results_list)))
...:     results_thou = np.array(results_list)
...:     print("shape of unformatted numpy is", np.shape(results_thou))
...:     results_formatted = results_thou.reshape(500, 10)
...:     print("shape of formatted numpy is", np.shape(results_formatted))
...: 
Shape of list is : (500,)
shape of unformatted numpy is (500,)
Traceback (most recent call last):
Input In [32] in <cell line: 1>
results_formatted = results_thou.reshape(500, 10)
ValueError: cannot reshape array of size 500 into shape (500,10)

对于i=0

In [36]: np.shape(index_)
Out[36]: (500,)
In [41]: i=0; range(1 + (i * 500), (500 + 1) + (i * 500))
Out[41]: range(1, 501)

我假设

results = executor.map(index_weather, secs)

产生与CCD_ 3形状匹配的结果。但您的(500,1,10(第一个结果表明,在这种情况下,index_weather返回了(1,10个(数组。弄清楚什么时候是这种情况,什么时候它只是返回一个标量,这不是我能做的

最新更新