2016-11-05 27 views
-1

我正嘗試在python中使用numpy創建一個餘弦表。我想有旁邊的角度的餘弦的角度,所以它看起來是這樣的:使用numpy並排打印兩個陣列

0.0 1.000 5.0 0.996 10.0 0.985 15.0 0.966 
20.0 0.940 25.0 0.906 and so on. 

我試圖做使用for循環,但我不知道如何得到這個工作。 目前,我有

有什麼建議嗎?

+4

輸出是什麼?如果你發佈你的代碼而不是截圖,對每個人都有幫助。 – Batman

+0

你的代碼在循環中根本不使用'angle'。你期望在這裏發生什麼? – Eric

回答

1

您可以使用python的zip函數同時檢查兩個列表中的元素。

import numpy as np 
degreesVector = np.linspace(0.0, 360.0, 73.0) 
cosinesVector = np.cos(np.radians(degreesVector)) 
for d, c in zip(degreesVector, cosinesVector): 
    print d, c 

如果你想使一個numpy的陣列出度和餘弦值,你可以用這種方式修改for循環:

table = [] 
for d, c in zip(degreesVector, cosinesVector): 
    table.append([d, c]) 
table = np.array(table) 

現在在同一行!

np.array([[d, c] for d, c in zip(degreesVector, cosinesVector)]) 
2

熊貓是這樣的任務很方便的模塊:

In [174]: import pandas as pd 
    ...: 
    ...: x = pd.DataFrame({'angle': np.linspace(0, 355, 355//5+1), 
    ...:     'cos': np.cos(np.deg2rad(np.linspace(0, 355, 355//5+1)))}) 
    ...: 
    ...: pd.options.display.max_rows = 20 
    ...: 
    ...: x 
    ...: 
Out[174]: 
    angle  cos 
0  0.0 1.000000 
1  5.0 0.996195 
2 10.0 0.984808 
3 15.0 0.965926 
4 20.0 0.939693 
5 25.0 0.906308 
6 30.0 0.866025 
7 35.0 0.819152 
8 40.0 0.766044 
9 45.0 0.707107 
.. ...  ... 
62 310.0 0.642788 
63 315.0 0.707107 
64 320.0 0.766044 
65 325.0 0.819152 
66 330.0 0.866025 
67 335.0 0.906308 
68 340.0 0.939693 
69 345.0 0.965926 
70 350.0 0.984808 
71 355.0 0.996195 

[72 rows x 2 columns] 
+0

不錯的使用熊貓!順便說一句,你可以使用'x.head()',而不是'pd.options.display.max_rows = 20' – Jakub

+0

@Jakub,謝謝!我只想顯示頭部和尾部... – MaxU

0

就在numpy的一些格式的想法,使用@MaxU的語法

a = np.array([[i, np.cos(np.deg2rad(i)), np.sin(np.deg2rad(i))] 
       for i in range(0,361,30)]) 
args = ["Angle", "Cos", "Sin"] 
frmt = ("{:>8.0f}"+"{:>8.3f}"*2) 
print(("{:^8}"*3).format(*args)) 
for i in a: 
    print(frmt.format(*i)) 
Angle Cos  Sin 
     0 1.000 0.000 
     30 0.866 0.500 
     60 0.500 0.866 
     90 0.000 1.000 
    120 -0.500 0.866 
    150 -0.866 0.500 
    180 -1.000 0.000 
    210 -0.866 -0.500 
    240 -0.500 -0.866 
    270 -0.000 -1.000 
    300 0.500 -0.866 
    330 0.866 -0.500 
    360 1.000 -0.000 
0

你接近 - 但如果您遍歷角度,只需生成該角度的cosine

In [293]: for angle in range(0,60,10): 
    ...:  print('{0:8}{1:8.3f}'.format(angle, np.cos(np.radians(angle)))) 
    ...:  
     0 1.000 
     10 0.985 
     20 0.940 
     30 0.866 
     40 0.766 
     50 0.643 

要使用數組,你有很多的選擇:

In [294]: angles=np.linspace(0,60,7) 
In [295]: cosines=np.cos(np.radians(angles)) 

遍歷一個指標:

In [297]: for i in range(angles.shape[0]): 
    ...:  print('{0:8}{1:8.3f}'.format(angles[i],cosines[i])) 

使用zip拋出值2×2:

for a,c in zip(angles, cosines): 
    print('{0:8}{1:8.3f}'.format(a,c)) 

略有變化:

for ac in zip(angles, cosines): 
    print('{0:8}{1:8.3f}'.format(*ac)) 

你可以串聯陣列彙集成一個二維數組,並顯示:

In [302]: np.vstack((angles, cosines)).T 
Out[302]: 
array([[ 0.  , 1.  ], 
     [ 10.  , 0.98480775], 
     [ 20.  , 0.93969262], 
     [ 30.  , 0.8660254 ], 
     [ 40.  , 0.76604444], 
     [ 50.  , 0.64278761], 
     [ 60.  , 0.5  ]]) 

In [318]: print(np.vstack((angles, cosines)).T) 
[[ 0.   1.  ] 
[ 10.   0.98480775] 
[ 20.   0.93969262] 
[ 30.   0.8660254 ] 
[ 40.   0.76604444] 
[ 50.   0.64278761] 
[ 60.   0.5  ]] 

np.column_stack可以做到這一點而不轉置。

而且可以傳遞數組您的格式有:

for ac in np.vstack((angles, cosines)).T: 
    print('{0:8}{1:8.3f}'.format(*ac)) 

,或者你可以寫一個csv風格文件,savetxt(這只是在二維數組的「行」進行迭代,並且與寫fmt):

In [310]: np.savetxt('test.txt', np.vstack((angles, cosines)).T, fmt='%8.1f %8.3f') 
In [311]: cat test.txt 
    0.0  1.000 
    10.0  0.985 
    20.0  0.940 
    30.0  0.866 
    40.0  0.766 
    50.0  0.643 
    60.0  0.500 

不幸的是savetxt需要舊樣式格式。並試圖寫入sys.stdout在Py3中運行到字節v unicode字符串問題。

3

比方說,你有:

>>> d = np.linspace(0, 360, 10, endpoint=False) 
>>> c = np.cos(np.radians(d)) 

如果你不介意有一些括號,這樣就在身邊,那麼你可以簡單地串聯逐列使用np.c_,並顯示:

>>> print(np.c_[d, c]) 
[[ 0.00000000e+00 1.00000000e+00] 
[ 3.60000000e+01 8.09016994e-01] 
[ 7.20000000e+01 3.09016994e-01] 
[ 1.08000000e+02 -3.09016994e-01] 
[ 1.44000000e+02 -8.09016994e-01] 
[ 1.80000000e+02 -1.00000000e+00] 
[ 2.16000000e+02 -8.09016994e-01] 
[ 2.52000000e+02 -3.09016994e-01] 
[ 2.88000000e+02 3.09016994e-01] 
[ 3.24000000e+02 8.09016994e-01]] 

但是,如果你在乎刪除它們,一個可能性是使用一個簡單的regex:

>>> import re 
>>> print(re.sub(r' *\n *', '\n', 
       np.array_str(np.c_[d, c]).replace('[', '').replace(']', '').strip())) 
0.00000000e+00 1.00000000e+00 
3.60000000e+01 8.09016994e-01 
7.20000000e+01 3.09016994e-01 
1.08000000e+02 -3.09016994e-01 
1.44000000e+02 -8.09016994e-01 
1.80000000e+02 -1.00000000e+00 
2.16000000e+02 -8.09016994e-01 
2.52000000e+02 -3.09016994e-01 
2.88000000e+02 3.09016994e-01 
3.24000000e+02 8.09016994e-01 

我刪除了括號,然後將它傳遞給正則表達式來刪除每行中任一側的空格。

np.array_str還可以讓你設置精度。要獲得更多控制權,您可以改用np.array2string