Python: Operações com Arrays: Difference between revisions

From Wiki
No edit summary
 
Line 25: Line 25:
  print(newArray)
  print(newArray)
  # array('i', [14, 18, 12, 10])
  # array('i', [14, 18, 12, 10])
== Arrays multi-dimensionais ==
Para arrays multi-dimensionais usamos o módulo numpy.
>>> from numpy import *
>>> arr = array([1,2,4,7,5,9])
>>> print(arr)
[1 2 4 7 5 9]
>>> print(arr.dtype)
int64
>>> arr = array([1,2,4,7,5.3])
>>> print(arr)
[ 1.  2.  4.  7.  5.3]
>>>print(arr.dtype)
float64
>>> arr = array([2,4,7,6,1])
>>> arr = arr + 5
>>> print(arr)
[ 7  9 12 11  6]
>>> print pow(arr,2)
[ 49  81 144 121  36]
# Vectorized operations
>>> arr2 = array([3,1,-2,-1,4])
>>> arr3 = arr + arr2
>>> print(arr3)
[10 10 10 10 10]
==== arange ====
>>> arr = arange(1,15,2)
>>> print arr
[ 1  3  5  7  9 11 13]
==== zeros ====
>>> arr = zeros(5)
>>> print arr
[ 0.  0.  0.  0.  0.]
==== ones ====
>>> arr = ones(7)
>>> print arr
[ 1.  1.  1.  1.  1.  1.  1.]
>>> arr = ones(7,int)
>>> print arr
[1 1 1 1 1 1 1]
==== logspace ====
>>> arr = logspace(1,15,2)
>>> print arr
[  1.00000000e+01  1.00000000e+15]
==== linspace ====
Cria partes
>>> arr = linspace(0,2,3)
>>> print arr
[ 0.  1.  2.]
>>> arr = linspace(0,5,4)
>>> print arr
[ 0.          1.66666667  3.33333333  5.        ]


= Ver também =
= Ver também =

Latest revision as of 02:41, 28 September 2018

Arrays em python só podem ser de apenas um tipo

Operações com Arrays

Básico

#!/usr/bin/python
from array import * 

vals = array('i',[5,9,6,7])
print vals
# array('i', [5, 6, 9, 7])

print vals[0]
# 5

vals.reverse()
print vals
# array('i', [7, 9, 6, 5])

for v in vals:
  print(v,end='')
# 7965

newArray = array(vals.typecode, (v*2 for v in vals))
print(newArray)
# array('i', [14, 18, 12, 10])

Ver também