SoFunction
Updated on 2025-03-02

Simple examples of arrays and lists in python

#Environment win64+anaconda+python3.6

list & array

(1) List does not have all properties of array (such as dimensions, transposes, etc.)

Code 1:

#eg1_1
import numpy as np
a = ([[1,2,0,1],[1,6,9,55],[7,8,9,5]])#a is an arrayprint()
 
#Result:
[[ 1  1  7]
 [ 2  6  8]
 [ 0  9  9]
 [ 1 55  5]]
 
#eg1_2
a = [[1,2,0,1],[1,6,9,55],[7,8,9,5]] #a is the listprint()
 
#Result:
'list' object has no attribute 'T'

Code 2:

#eg1_3
import numpy as np
a=([[1,2,3],[1,1,4],[1,5,1]])
print()
 
#Result:
(3, 3)
 
#eg1_4
a=[[1,2,3],[1,1,4],[1,5,1]]
print()
 
#Result
'list' object has no attribute 'shape'

(By the way, how to convert an array into a column vector: ↓)

import numpy as np
a=([[1,2,3],[1,1,4],[1,5,1]])
a=(-1,1)
print(a)
 
#Result:
[[1]
 [2]
 [3]
 [1]
 [1]
 [4]
 [1]
 [5]
 [1]]

(2) The meaning of a[:m], a can be a list or an array, but in any case, a[:0] is empty

#eg2_1
import numpy as np
a=([[4,1,2],
            [7,4,10],
            [12,17,88]])
#a=([(4,1,2),
#            (7,4,10),
# (12,17,88)]) Among these two a[ and (different, they are actually exactly the sameprint(a[:0])
print(a[:1])
print(a[:2])
 
#Result:
[]
[[4 1 2]]
[[ 4  1  2]
 [ 7  4 10]]
 
#eg2_1
a=[(4,1,2),(7,4,10),(12,17,88)]
print(a[:0])
print(a[:1])
print(a[:2])
 
 
#Result:
[]
[(4, 1, 2)]
[(4, 1, 2), (7, 4, 10)]

(3) Calculation of array and list about "=="

#eg3_1
import numpy as np
a=(['dog','cat','car'])
b=(['dog','cat','trunk'])
acc = ((a == b))
print(acc)
 
#Result
0.6666666666666666
 
#eg3_2
import numpy as np
a=['dog','cat','car']
b=['dog','cat','trunk']
acc = ((a == b))
print(acc)
 
#Result
0.0

(4) Calculation of array and list about "*"

from numpy import *
#a is an arraya=array([[1,2,3],
   [4,5,6]])
b=4*a
print(b)    
 
[[ 4  8 12]
 [16 20 24]]
 
 
from numpy import *
#a is the lista=([[1,2,3],
   [4,5,6]])
b=4*a
print(b)
 
[[1, 2, 3], [4, 5, 6], [1, 2, 3], [4, 5, 6], [1, 2, 3], [4, 5, 6], [1, 2, 3], [4, 5, 6]]

Difference between python list and Numpy array

1. Both can be used to process multi-dimensional arrays.

The ndarray object in Numpy is used to handle multidimensional arrays, which acts as a fast and flexible big data container. Python lists can store one-dimensional arrays, and multi-dimensional arrays can be implemented through nesting lists.

2. Storage efficiency and input and output performance are different.

Numpy has been designed specifically for array operations and calculations. The storage efficiency and input and output performance are much better than nested lists in Python. The larger the array, the more obvious the advantages of Numpy are.

3. Element data type.

Generally, all elements in Numpy arrays must have the same type, while the element types in Python lists are arbitrary, so in general performance, Numpy arrays are not as good as Python lists, but in scientific calculations, many loop statements can be saved, and the code usage is much simpler than Python lists.

Summarize

This is the end of this article about arrays and lists in python. For more related python arrays and list content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!