我有以下数组:
var crc32=function(r){for(var a,o=[],c=0;c<256;c++){a=c;for(var f=0;f<8;f++)a=1&a?3988292384^a>>>1:a>>>1;o[c]=a}for(var n=-1,t=0;t<r.length;t++)n=n>>>8^o[255&(n^r.charCodeAt(t))];return(-1^n)>>>0};
console.log(crc32('abc'));
console.log(crc32('abc').toString(16).toUpperCase()); // hex
现在,我想将这些值复制到同一行的n列中。结果应如下所示:
[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]
你知道这是怎么回事吗?
提前致谢。
答案 0 :(得分:2)
由于您正在使用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]])
答案 1 :(得分:0)
试试这个:
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])
我从你的示例输出中得到这个答案。你的措辞没有明确说明你想要的东西。
如果您使用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())
答案 2 :(得分:0)
使用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)])
编辑:为了使它更加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]
答案 3 :(得分:0)
我们可以运行for循环并将数字存储到空列表中。然后我们可以获取该空列表,然后将其转换为数组,以获得您正在寻找的输出。这是一个例子:
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)
这是你的输出:
[[ 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]]
简而言之,我们只需对每个数字运行一个for循环,将它存储三次到空列表,创建一个数组,重新整形,然后我们得到你正在寻找的输出。