简体   繁体   English

将一维NumPy数组的值重复“ N”次

[英]Repeat values an a 1D NumPy array “N” times

I have the following array: 我有以下数组:

[9.975 9.976 9.977 9.978 9.979 9.98  9.981 9.982 9.983 9.984 9.985 9.986
9.987 9.988 9.989 9.99  9.991 9.992 9.993 9.994]

Now, I would like to copy these values in n columns in the same row. 现在,我想将这些值复制到同一行的n列中。 The result should look like this: 结果应如下所示:

[[9.975 9.975 9.975],
 [9.976 9.976 9.976],
 ..... 
 [9.994 9.994 9.994]]

Do you know how this is possible? 你知道这怎么可能吗?

Thanks in advance. 提前致谢。

Since you're using numpy, use np.repeat + np.reshape : 由于您使用的是numpy,请使用np.repeat + np.reshape

>>> np.repeat(arr, 3).reshape(-1, 3)
array([[9.975, 9.975, 9.975],
       [9.976, 9.976, 9.976],
       [9.977, 9.977, 9.977],
       [9.978, 9.978, 9.978],
       [9.979, 9.979, 9.979],
       [9.98 , 9.98 , 9.98 ],
       [9.981, 9.981, 9.981],
       [9.982, 9.982, 9.982],
       [9.983, 9.983, 9.983],
       [9.984, 9.984, 9.984],
       [9.985, 9.985, 9.985],
       [9.986, 9.986, 9.986],
       [9.987, 9.987, 9.987],
       [9.988, 9.988, 9.988],
       [9.989, 9.989, 9.989],
       [9.99 , 9.99 , 9.99 ],
       [9.991, 9.991, 9.991],
       [9.992, 9.992, 9.992],
       [9.993, 9.993, 9.993],
       [9.994, 9.994, 9.994]])

Try this: 尝试这个:

a = [ 9.975, 9.976, 9.977, 9.978, 9.979, 9.98,  9.981,
      9.982, 9.983, 9.984, 9.985, 9.986, 9.987, 9.988,
      9.989, 9.99,  9.991, 9.992, 9.993, 9.994 ]
n = 3
print([[x] * n for x in a])

I'm deriving this answer from your example output. 我从您的示例输出中得出此答案。 Your wording doesn't clearly state what you want. 您的措词并没有明确说明您想要什么。

If you are using numpy , I propose this solution: 如果您使用的是numpy ,那么我建议采用以下解决方案:

a = np.array([ 9.975, 9.976, 9.977, 9.978, 9.979, 9.98,  9.981,
               9.982, 9.983, 9.984, 9.985, 9.986, 9.987, 9.988,
               9.989, 9.99,  9.991, 9.992, 9.993, 9.994 ])
print(np.array([ a ] * 3).transpose())

Use a for loop 使用for循环

lst = [9.975, 9.976, 9.977, 9.978, 9.979, 9.98,  9.981, 9.982, 9.983, 9.984, 9.985, 9.986, 9.987, 9.988, 9.989, 9.99,  9.991, 9.992, 9.993, 9.994]
rslt = []
n = 3
for i in lst:
    rslt.append([i for j in range(n)])

Edit: To make it even more pythonic, though slightly less readable: 编辑:使它变得更pythonic,虽然可读性稍差:

lst = [9.975, 9.976, 9.977, 9.978, 9.979, 9.98,  9.981, 9.982, 9.983, 9.984, 9.985, 9.986, 9.987, 9.988, 9.989, 9.99,  9.991, 9.992, 9.993, 9.994]
n = 3
rslt = [[i for j in range(n)] for i in lst]

We can run a for loop and store the number to an empty list. 我们可以运行for循环并将数字存储到空列表中。 We can then take that empty list and then turn it into an array in order to get the output that you are seeking. 然后,我们可以获取该空列表,然后将其转换为数组,以获取所需的输出。 Here is an example: 这是一个例子:

import numpy as np



array = np.array([9.975, 9.976, 9.977, 9.978, 9.979, 9.98,  9.981, 9.982, 9.983, 9.984, 9.985, 9.986,
                  9.987, 9.988, 9.989, 9.99,  9.991, 9.992, 9.993, 9.994])
empty_list = []

for number in array:
    num1 = float(number)
    num2 = float(number)
    num3 = float(number)
    empty_list.append(num1)
    empty_list.append(num2)
    empty_list.append(num3)

array = np.array(empty_list).reshape(20, 3)
print(array)

And here is your output: 这是您的输出:

[[ 9.975  9.975  9.975]
 [ 9.976  9.976  9.976]
 [ 9.977  9.977  9.977]
 [ 9.978  9.978  9.978]
 [ 9.979  9.979  9.979]
 [ 9.98   9.98   9.98 ]
 [ 9.981  9.981  9.981]
 [ 9.982  9.982  9.982]
 [ 9.983  9.983  9.983]
 [ 9.984  9.984  9.984]
 [ 9.985  9.985  9.985]
 [ 9.986  9.986  9.986]
 [ 9.987  9.987  9.987]
 [ 9.988  9.988  9.988]
 [ 9.989  9.989  9.989]
 [ 9.99   9.99   9.99 ]
 [ 9.991  9.991  9.991]
 [ 9.992  9.992  9.992]
 [ 9.993  9.993  9.993]
 [ 9.994  9.994  9.994]]

In short, we simply run a for loop on each number, store it three times to the empty list, create an array, reshape it, and then we get the output that you are seeking. 简而言之,我们仅对每个数字运行一个for循环,将其存储三遍到空列表中,创建一个数组,对其进行整形,然后获得您要查找的输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM