最近博主在写代码的时候遇到了一个问题,就是使用的一个函数,需要传入的参数是多行的数组,可是我的原始数据是列表形式的,因此就涉及到列表转换为array形式的数组,便有了此文章。
我的列表是类似这样的一组数
C19i=[6.651, 6.513, 6.672, 6.584, 6.629, 7.051, 6.498, 6.62, 6.514, 6.585, 6.393, 6.658, 6.205, 6.714, 6.734, 6.746, 6.616, 6.545, 6.58, 6.359, 6.916, 6.675, 6.71, 6.523, 6.423, 6.737, 6.485, 6.347, 6.504, 6.527, 6.452, 6.687, 6.507, 6.47, 6.821, 6.607, 6.616, 6.6, 6.509, 6.563, 6.425, 6.052, 6.754, 6.686, 6.502, 6.477, 6.56, 6.499, 6.328, 6.585, 6.403, 6.447, 6.57, 6.513, 7.323, 6.342, 6.537, 6.545, 6.315, 6.526, 6.381, 6.298, 6.247, 6.392, 6.443, 6.534, 6.572, 6.393, 6.441, 6.256, 6.317, 6.371, 6.345, 6.401, 6.327, 6.317, 6.363, 6.219, 6.409, 6.427, 6.394, 6.425, 6.339, 6.252, 6.316, 6.538, 6.4, 6.425, 6.336, 6.373, 6.276, 6.26, 6.538, 6.241, 6.395, 6.437, 6.38, 6.473, 6.405, 6.567, 6.276, 6.46, 6.39, 6.344, 6.328, 6.455, 6.303, 6.362, 6.446, 6.329, 6.407, 6.256, 6.266, 6.286, 6.438, 6.353, 6.339, 6.446, 6.375, 6.444, 6.28, 6.473, 6.321, 6.252, 6.438, 6.346, 6.252, 6.287, 6.389, 6.415, 6.231, 6.377, 6.349, 6.254, 6.234, 6.385, 6.305, 6.3, 6.429, 6.458, 6.366, 6.325, 6.421, 6.212, 6.263, 6.505, 6.455, 6.431, 6.462, 6.362, 6.284, 6.27, 6.402, 6.401, 6.308, 6.224, 6.252, 6.297, 6.191, 6.53, 6.419, 6.455, 6.466, 6.517, 6.219, 6.335, 6.508, 6.356, 6.266, 6.413, 6.367, 6.287, 6.305, 6.522, 6.296, 6.367, 6.432, 6.493, 6.262, 6.357, 6.596, 6.376, 6.343, 6.303, 6.441, 6.191]
现在我想将三个这样的列表叠加在一起,转换成array数组,即如下的样子:
dcrowci = np.array([C19i,
C23i,
C36i
])
正常来说这样不会出现问题,最后的dcrowci的类型应该就是数组,但是有时候不知道程序怎么了,dcrowci不是理想的样子,理想样子如下:
[[6.651 6.513 6.672 6.584 6.629 7.051 6.498 6.62 6.514 6.585 6.393 6.658
6.205 6.714 6.734 6.746 6.616 6.545 6.58 6.359 6.916 6.675 6.71 6.523
6.423 6.737 6.485 6.347 6.504 6.527 6.452 6.687 6.507 6.47 6.821 6.607
6.616 6.6 6.509 6.563 6.425 6.052 6.754 6.686 6.502 6.477 6.56 6.499
6.328 6.585 6.403 6.447 6.57 6.513 7.323 6.342 6.537 6.545 6.315 6.526
6.381 6.298 6.247 6.392 6.443 6.534 6.572 6.393 6.441 6.256 6.317 6.371
6.345 6.401 6.327 6.317 6.363 6.219 6.409 6.427 6.394 6.425 6.339 6.252
6.316 6.538 6.4 6.425 6.336 6.373 6.276 6.26 6.538 6.241 6.395 6.437
6.38 6.473 6.405 6.567 6.276 6.46 6.39 6.344 6.328 6.455 6.303 6.362
6.446 6.329 6.407 6.256 6.266 6.286 6.438 6.353 6.339 6.446 6.375 6.444
6.28 6.473 6.321 6.252 6.438 6.346 6.252 6.287 6.389 6.415 6.231 6.377
6.349 6.254 6.234 6.385 6.305 6.3 6.429 6.458 6.366 6.325 6.421 6.212
6.263 6.505 6.455 6.431 6.462 6.362 6.284 6.27 6.402 6.401 6.308 6.224
6.252 6.297 6.191 6.53 6.419 6.455 6.466 6.517 6.219 6.335 6.508 6.356
6.266 6.413 6.367 6.287 6.305 6.522 6.296 6.367 6.432 6.493 6.262 6.357
6.596 6.376 6.343 6.303 6.441 6.191]
[23.275 23.348 23.535 23.765 23.696 23.576 23.514 23.35 23.496 23.654
23.6 23.514 23.394 23.36 23.278 23.586 23.475 23.554 23.501 23.515
23.419 23.138 23.569 23.463 23.61 23.394 23.502 22.838 23.46 23.372
23.423 23.747 23.391 23.521 23.497 23.433 23.541 23.552 23.548 23.463
23.497 23.631 23.374 23.43 23.613 23.691 23.356 23.383 23.709 23.398
23.486 23.707 23.587 23.65 23.708 23.544 23.665 23.566 23.635 23.554
23.583 23.629 23.504 23.424 23.495 23.647 23.49 23.434 23.536 23.721
23.471 23.677 23.704 23.609 23.56 23.571 23.593 23.415 23.545 23.668
23.566 23.521 23.619 23.649 23.556 23.501 23.745 23.709 23.478 23.603
23.684 23.509 23.398 23.593 23.647 23.645 23.638 23.625 23.6 23.478
23.596 23.726 23.683 23.685 23.529 23.545 23.574 23.749 23.548 23.558
23.621 23.528 23.671 23.83 23.698 23.598 23.421 23.65 23.619 23.561
23.794 23.58 23.747 23.472 23.552 23.649 23.583 23.717 23.509 23.589
23.432 23.634 23.601 23.627 23.703 23.567 23.651 23.558 23.524 23.574
23.531 23.476 23.488 23.651 23.576 23.576 23.612 23.601 23.484 23.498
23.595 23.39 23.48 23.745 23.684 23.558 23.566 23.444 23.651 23.505
23.567 23.471 23.457 23.551 23.604 23.499 23.555 23.649 23.626 23.618
23.455 23.594 23.523 23.413 23.63 23.572 23.483 23.423 23.666 23.383
23.567 23.572 23.661 23.521 23.327 None]
[-17.12 -17.033 -17.224 -17.225 -17.153 -17.179 -17.18 -17.03 -17.272
-17.215 -17.223 -17.197 -17.182 -17.233 -16.982 -17.184 -17.294 -17.071
-17.427 -17.383 -17.12 -16.998 -17.066 -17.342 -17.483 -17.078 -17.203
-17.444 -17.241 -17.25 -17.338 -17.281 -17.268 -17.287 -17.049 -17.264
-17.042 -17.23 -17.103 -17.229 -17.265 -17.343 -17.062 -16.933 -17.296
-17.304 -16.976 -17.04 -17.263 -16.868 -17.132 -17.17 -17.013 -17.101
-16.265 -17.037 -17.066 -17.153 -17.169 -17.096 -16.988 -17.36 -17.184
-17.032 -17.077 -17.263 -17.002 -16.953 -17.168 -16.972 -17.055 -17.23
-17.196 -17.026 -16.969 -17.179 -17.077 -17.152 -17.085 -17.199 -17.094
-16.971 -16.972 -16.985 -16.973 -16.988 -17.192 -17.194 -17.068 -16.794
-16.988 -16.934 -16.941 -17.097 -17.015 -16.978 -16.981 -17.052 -16.865
-16.927 -17.168 -17.107 -16.932 -16.997 -17.115 -17.014 -17.217 -17.071
-16.938 -17.005 -16.951 -16.835 -17.09 -17.031 -17.329 -16.993 -16.935
-16.899 -17.019 -17.099 -17.125 -17.137 -17.094 -16.981 -16.974 -17.176
-17.078 -17.021 -17.14 -17.044 -16.964 -17.068 -17.203 -16.907 -17.08
-17.148 -17.205 -16.929 -17.047 -17.224 -16.948 -17.086 -17.071 -17.167
-17.268 -17.069 -17.054 -16.973 -17.107 -17.091 -17.042 -17.095 -17.025
-17.134 -17.052 -17.188 -17.183 -17.204 -17.06 -16.879 -17.022 -17.157
-16.999 -17.164 -17.136 -17.125 -17.044 -17.213 -17.229 -17.184 -17.144
-17.216 -17.086 -16.919 -17.199 -17.18 -17.052 -17.194 -17.227 -17.041
-16.983 -17.237 -17.179 -17.415 -17.114 -17.16]]
但现实情况却为这个:
[list[6.651 6.513 6.672 6.584 6.629 7.051 6.498 6.62 6.514 6.585 6.393 6.658
6.205 6.714 6.734 6.746 6.616 6.545 6.58 6.359 6.916 6.675 6.71 6.523
6.423 6.737 6.485 6.347 6.504 6.527 6.452 6.687 6.507 6.47 6.821 6.607
6.616 6.6 6.509 6.563 6.425 6.052 6.754 6.686 6.502 6.477 6.56 6.499
6.328 6.585 6.403 6.447 6.57 6.513 7.323 6.342 6.537 6.545 6.315 6.526
6.381 6.298 6.247 6.392 6.443 6.534 6.572 6.393 6.441 6.256 6.317 6.371
6.345 6.401 6.327 6.317 6.363 6.219 6.409 6.427 6.394 6.425 6.339 6.252
6.316 6.538 6.4 6.425 6.336 6.373 6.276 6.26 6.538 6.241 6.395 6.437
6.38 6.473 6.405 6.567 6.276 6.46 6.39 6.344 6.328 6.455 6.303 6.362
6.446 6.329 6.407 6.256 6.266 6.286 6.438 6.353 6.339 6.446 6.375 6.444
6.28 6.473 6.321 6.252 6.438 6.346 6.252 6.287 6.389 6.415 6.231 6.377
6.349 6.254 6.234 6.385 6.305 6.3 6.429 6.458 6.366 6.325 6.421 6.212
6.263 6.505 6.455 6.431 6.462 6.362 6.284 6.27 6.402 6.401 6.308 6.224
6.252 6.297 6.191 6.53 6.419 6.455 6.466 6.517 6.219 6.335 6.508 6.356
6.266 6.413 6.367 6.287 6.305 6.522 6.296 6.367 6.432 6.493 6.262 6.357
6.596 6.376 6.343 6.303 6.441 6.191]
list[23.275 23.348 23.535 23.765 23.696 23.576 23.514 23.35 23.496 23.654
23.6 23.514 23.394 23.36 23.278 23.586 23.475 23.554 23.501 23.515
23.419 23.138 23.569 23.463 23.61 23.394 23.502 22.838 23.46 23.372
23.423 23.747 23.391 23.521 23.497 23.433 23.541 23.552 23.548 23.463
23.497 23.631 23.374 23.43 23.613 23.691 23.356 23.383 23.709 23.398
23.486 23.707 23.587 23.65 23.708 23.544 23.665 23.566 23.635 23.554
23.583 23.629 23.504 23.424 23.495 23.647 23.49 23.434 23.536 23.721
23.471 23.677 23.704 23.609 23.56 23.571 23.593 23.415 23.545 23.668
23.566 23.521 23.619 23.649 23.556 23.501 23.745 23.709 23.478 23.603
23.684 23.509 23.398 23.593 23.647 23.645 23.638 23.625 23.6 23.478
23.596 23.726 23.683 23.685 23.529 23.545 23.574 23.749 23.548 23.558
23.621 23.528 23.671 23.83 23.698 23.598 23.421 23.65 23.619 23.561
23.794 23.58 23.747 23.472 23.552 23.649 23.583 23.717 23.509 23.589
23.432 23.634 23.601 23.627 23.703 23.567 23.651 23.558 23.524 23.574
23.531 23.476 23.488 23.651 23.576 23.576 23.612 23.601 23.484 23.498
23.595 23.39 23.48 23.745 23.684 23.558 23.566 23.444 23.651 23.505
23.567 23.471 23.457 23.551 23.604 23.499 23.555 23.649 23.626 23.618
23.455 23.594 23.523 23.413 23.63 23.572 23.483 23.423 23.666 23.383
23.567 23.572 23.661 23.521 23.327 None]
list[-17.12 -17.033 -17.224 -17.225 -17.153 -17.179 -17.18 -17.03 -17.272
-17.215 -17.223 -17.197 -17.182 -17.233 -16.982 -17.184 -17.294 -17.071
-17.427 -17.383 -17.12 -16.998 -17.066 -17.342 -17.483 -17.078 -17.203
-17.444 -17.241 -17.25 -17.338 -17.281 -17.268 -17.287 -17.049 -17.264
-17.042 -17.23 -17.103 -17.229 -17.265 -17.343 -17.062 -16.933 -17.296
-17.304 -16.976 -17.04 -17.263 -16.868 -17.132 -17.17 -17.013 -17.101
-16.265 -17.037 -17.066 -17.153 -17.169 -17.096 -16.988 -17.36 -17.184
-17.032 -17.077 -17.263 -17.002 -16.953 -17.168 -16.972 -17.055 -17.23
-17.196 -17.026 -16.969 -17.179 -17.077 -17.152 -17.085 -17.199 -17.094
-16.971 -16.972 -16.985 -16.973 -16.988 -17.192 -17.194 -17.068 -16.794
-16.988 -16.934 -16.941 -17.097 -17.015 -16.978 -16.981 -17.052 -16.865
-16.927 -17.168 -17.107 -16.932 -16.997 -17.115 -17.014 -17.217 -17.071
-16.938 -17.005 -16.951 -16.835 -17.09 -17.031 -17.329 -16.993 -16.935
-16.899 -17.019 -17.099 -17.125 -17.137 -17.094 -16.981 -16.974 -17.176
-17.078 -17.021 -17.14 -17.044 -16.964 -17.068 -17.203 -16.907 -17.08
-17.148 -17.205 -16.929 -17.047 -17.224 -16.948 -17.086 -17.071 -17.167
-17.268 -17.069 -17.054 -16.973 -17.107 -17.091 -17.042 -17.095 -17.025
-17.134 -17.052 -17.188 -17.183 -17.204 -17.06 -16.879 -17.022 -17.157
-16.999 -17.164 -17.136 -17.125 -17.044 -17.213 -17.229 -17.184 -17.144
-17.216 -17.086 -16.919 -17.199 -17.18 -17.052 -17.194 -17.227 -17.041
-16.983 -17.237 -17.179 -17.415 -17.114 -17.16]]
进程已结束,退出代码 0
也就是说,转换的时候,list也传进了数组中,就非常不对劲,那么,怎么解决这个办法呢?还好,numpy提供了一个数组横向和纵向合并的函数,即vstack和hstack,那么使用下面的语句就可以达成目的啦:
dcrowci0 = np.vstack((C19i,C23i))
dcrowci = np.vstack((dcrowci0,C36i))
最后dcrowci就是理想的样子!
关于这两个函数,我在之前文章有些过,大家可以看一下!python函数之hstack函数–用于将两数组或矩阵合并
一起进步,一起努力,加油呀!