我需要附加在每个循环中创建的数组,以便最后获得单个ndarray.代码结构如下:
for...:
.
.
.
for...:
list1 = array([some_math_here])
list2.append(list1)
#each loop creats a list, converting it to array() gives different shaped arrays:
array(list2).shape
(2939, 4)
(2942, 4)
(2027, 4)
(2030, 4)
#list3 collects all the generated results
list3.append(list2)
问:
我如何拥有n * 4列和不同行数的数组而不是list3?
我尝试通过创建一个空数组a = array([0.,1.]),然后追加一个(a,array(list_2)),但不起作用.我知道hstack vstack等,但不能在循环中将它们与append一起使用.有什么建议吗?
更新
?这是带有建议方法输出的实际代码:
files_ = glob.glob('D:\Test files\*.txt')
tfile_ = loadtxt('times.txt')
averages_, d = [], []
with open ('outfile.csv', 'wb') as outfile:
writer = csv.writer(outfile)
for fcount_, fname_ in enumerate(files_):
data = loadtxt(fname_ , usecols = (1,2,3,4))
average_, fcol = [], []
seg_len = 3
for x in range(0, len(data[:,0]), seg_len):
sample_means = [mean(data[x:x+seg_len,i]) for i in range(4)]
none_zeros = [x if x >= 0 else x == 0 for x in sample_means]
average_.append(none_zeros)
fcol = cumsum(array(average_)[:,0])
average_ = array([row + [col] for row, col in zip(average_, fcol)])
averages_.append(average_)
d = concatenate(array(averages_))
df = pd.DataFrame(d)
df.to_csv('pdtest2.csv')
输出:
0 1 2 3 4
0 0.037039 0.103792 0.136116 0.579297 0.037039
1 0.051183 0.104669 0.177728 0.593771 0.088222
2 0.059517 0.105437 0.174274 0.571402 0.147739
3 0.053212 0.102476 0.167530 0.645745 0.200950
4 0.054637 0.104450 0.165228 0.596622 0.054637
5 0.051622 0.101161 0.166708 0.595964 0.106259
6 0.057324 0.099077 0.168024 0.596841 0.163583
7 0.054692 0.103573 0.157168 0.598596 0.218275
8 0.066699 0.100612 0.145984 0.591578 0.284974
9 0.120866 5.527104 4.678589 2.401020 0.120866
10 0.113958 5.176220 4.669872 2.361985 0.234824
11 0.121469 4.879613 4.659017 2.359573 0.356293
12 0.122511 4.695618 4.642240 2.363959 0.478803
13 0.126650 4.621933 4.620447 2.347073 0.605453
14 0.132708 4.676868 4.517568 2.364617 0.132708
15 0.125087 4.693535 4.459672 2.381941 0.257795
16 0.132708 4.715246 4.444705 2.334353 0.390503
17 0.133476 4.745619 4.406300 2.317467 0.523979
当我想要时:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
0 0.037038522 0.103792144 0.136115724 0.57929719 0.037038522 0.054637318 0.104450043 0.16522775 0.596621864 0.054637318 0.12086581 5.527104488 4.678589189 2.401020431 0.12086581 0.132707991 4.67686799 4.517567512 2.364616645 0.132707991
1 0.051183348 0.104669343 0.177727829 0.593770968 0.08822187 0.051621948 0.101160549 0.166708023 0.595963965 0.106259265 0.113957871 5.176219782 4.669871979 2.361985046 0.234823681 0.125087328 4.693534961 4.459672089 2.381941338 0.257795319
2 0.059516735 0.105436892 0.17427386 0.571402402 0.147738605 0.057323738 0.099077202 0.168023821 0.596841163 0.163583003 0.121468884 4.879613015 4.659016582 2.359572747 0.356292565 0.132707991 4.715245885 4.444704808 2.334353258 0.39050331
3 0.05321187 0.102476346 0.167530397 0.645744989 0.200950475 0.054692143 0.103572845 0.157168489 0.598595561 0.218275146 0.122510557 4.695618334 4.642240062 2.363958746 0.478803122 0.13347554 4.745619253 4.406299754 2.317467166 0.52397885
4 0 0 0 0 0 0.066698797 0.1006123 0.145984208 0.591577971 0.284973943 0.126649838 4.621932787 4.620447035 2.347072653 0.60545296 0 0 0 0 0
本文探讨了在Python中如何处理循环过程中产生的不规则形状的数组,并最终整合为一个单一的ndarray。作者分享了一段具体代码示例,展示了如何在每次循环中计算并收集数据,同时提出了一些关于如何有效组织这些数据的问题。
986

被折叠的 条评论
为什么被折叠?



