<\/script>')

Array in Python (Python)

Python - . , , , , - . , , , .

""

.

Arrays in Python




[], . . .

:

  • a = []

"" :

  • Prime = ['string1', 'string2', 'string3']
  • Prime[1] = 'string2'; //true

len() .

  • len(Prime) == 4; // true

for. Pascal , , .

  • for elem in [1, 4, 67]

. [ for in ];





"" . :

  • [[0 for j in range(m)] for i in range(n)]

NumPy

"" NumPy.

YP Python




, . . , .

  • import numpy as np

, "" - array(). ndarray.

  • array = np.array(/* */)

array ndarray. array.type(). . - <class 'numpy.ndarray'>.

, dtype=np.complex .

  • array2 = np.array([ /**/, dtype=np.complex)

, , zeros(). ones(). .





  • np.zeros(2, 2, 2)

, 2 .

  • array([
  • [[0, 0]]
  • [[0, 0]]]
  • )

, print(). , NumPy .

, set_printoptions(). 1000 . threshold.

NumPy

"" .

Numpy library




, . , . :

  • array1 = np.array([[1, 2, 3], [1, 2, 3]])
  • array2 = np.array([[1, 2, 3], [1, 2, 3], [1, 2, 3]])

array1 + array2 , - 2, - 3.

  • array1 = np.array([1, 2, 5, 7])
  • array2 = arange([1, 5, 1])

Array1 + array2 2, 4, 8, 11. , .

, ndarray sum().

  • np.array(array1 + array1) == array1 + array2

ndarray . np. ( ).

"" . shape().

One-dimensional, two-dimensional and three-dimensional array




(2, 2, 3). , shape() . , - . reshape(). .

. , ravel() , . transpose() .

, . "" "" (). for.

Slices in Python




"". [start:stop:step]. start , , stop - , - . start , , stop , - , . , .

, :

  • mas = [1, 2, 3, 4]

, mas[:]. [1, 2, 3, 4]. , -3, .

  • mas[-3]; //[4]

, . , mas[::2] [1, 3]. , , [::-2] , [3, 1].

. "" [:, 2] , . [:2] - .

, . Python , , . , np.arange(10) array2 = array1, , , array1 array2. . array1.shape(3, 4) array2.

  • array1.shape() == (3, 4);//true
  • array2.shape() == (3, 4);//true

view() . , array, view()

  • array.view()

array2 , , . :

  • array2 is array1; //false

, .

  • array1.shape(2, 6)
  • array1 == array2; // true

,

. . hstack() , vstack() - .

column_stack() . column_stack() row_stack(), , . , hsplit(), vsplit() - .




All Articles