Reputation: 21
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. 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.
Upvotes: 1
Views: 653
Reputation: 2190
We can run a for loop and store the number to an empty list. 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.
Upvotes: 0
Reputation: 59416
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:
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())
Upvotes: 0
Reputation: 402323
Since you're using numpy, use 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]])
Upvotes: 2
Reputation: 1573
Use a for loop
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:
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]
Upvotes: 0