Python中list 和numpy.array的区别

1. list可以用append 或者  + 来新增元素或者添加数组,但array不行

2. 大多数array的操作都是elementwise级别的,即对每个元素进行单独处理,而不是视为一个整体再处理,

比如 A + A,得到的结果是A中每个元素的值对应✖️2的结果,

又比如有一个numpy.array A = [1, 2, 3], A + A = [2, 4, 6]。

又比如 A ** 2 = [1, 4, 9],

这种特性使得numpy.array适合快速对每个元素进行单独处理。

----------------------------------------------------------------

以下是练习代码:

(py3) mbp$ ipython
Python 3.7.4 (default, Aug 13 2019, 15:17:50) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.8.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import numpy as np                                                      

In [2]: L = [1, 2, 3]                                                           

In [3]: A = np.arrar([1, 2, 3])                                                 
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
 in 
----> 1 A = np.arrar([1, 2, 3])

AttributeError: module 'numpy' has no attribute 'arrar'

In [4]: A = np.array([1, 2, 3])                                                 

In [5]: for e in L: 
   ...:     print e                                                             
  File "", line 2
    print e
          ^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print(e)?


In [6]: for e in L: 
   ...:     print (e) 
   ...:                                                                         
1
2
3

In [7]: for e in A: 
   ...:     print (e) 
   ...:                                                                         
1
2
3

In [8]: L.append(4)                                                             

In [9]: L                                                                       
Out[9]: [1, 2, 3, 4]

In [10]: A.append(4)                                                            
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
 in 
----> 1 A.append(4)

AttributeError: 'numpy.ndarray' object has no attribute 'append'

In [11]: L = L + [5]                                                            

In [12]: L                                                                      
Out[12]: [1, 2, 3, 4, 5]

In [13]: A = A + [4 ,5]                                                         
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
 in 
----> 1 A = A + [4 ,5]

ValueError: operands could not be broadcast together with shapes (3,) (2,) 

In [14]: L2 = []                                                                

In [15]: for e in L: 
    ...:     L2.append(e + e) 
    ...:                                                                        

In [16]: L2                                                                     
Out[16]: [2, 4, 6, 8, 10]

In [17]: A + A                                                                  
Out[17]: array([2, 4, 6])

In [18]: 2*A                                                                    
Out[18]: array([2, 4, 6])

In [19]: 2*L                                                                    
Out[19]: [1, 2, 3, 4, 5, 1, 2, 3, 4, 5]

In [20]: L ** 2                                                                 
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
 in 
----> 1 L ** 2

TypeError: unsupported operand type(s) for ** or pow(): 'list' and 'int'

In [21]: A **2                                                                  
Out[21]: array([1, 4, 9])

In [22]: np.sqrt(A)                                                             
Out[22]: array([1.        , 1.41421356, 1.73205081])

In [23]: np.log(A)                                                              
Out[23]: array([0.        , 0.69314718, 1.09861229])

In [24]: np.exp(A)                                                              
Out[24]: array([ 2.71828183,  7.3890561 , 20.08553692])

 

你可能感兴趣的:(Python)