Lists

Python สำหรับผู้ใช้ MATLAB

Justin Kiggins

Product Manager

List คืออะไร?

  • โครงสร้าง Python ง่ายๆ สำหรับเก็บข้อมูล
  • Lists เก็บได้ทุกประเภท
  • คล้าย cell array ใน MATLAB
  • แต่มีแค่หนึ่งมิติ
  • การ indexing เหมือน NumPy arrays
Python สำหรับผู้ใช้ MATLAB

การสร้าง list

my_list = [8, 6, 7, 5, 3, 0 ,9]

print(my_list[2])
7
print(my_list[-3:])
[3, 0, 9]
Python สำหรับผู้ใช้ MATLAB

การสร้าง NumPy arrays จาก lists

my_list = [8, 6, 7, 5, 3, 0 ,9]
import numpy as np

my_array = np.array(my_list)
type(my_array)
numpy.ndarray
Python สำหรับผู้ใช้ MATLAB

NumPy arrays หลายมิติจาก lists of lists

list_of_lists = [[2, 3], [9, 0], [1, 4]]

import numpy as np arr = np.array(list_of_lists) print(arr)
[[2, 3]
 [9, 0]
 [1, 4]]
arr.shape
(3, 2)
Python สำหรับผู้ใช้ MATLAB

ความแตกต่างระหว่าง lists และ NumPy arrays

NumPy Arrays Lists
ทุก element ต้องเป็นประเภทเดียวกัน รองรับหลายประเภทผสมกัน
(+) บวก element-wise (+) ต่อ list เข้าด้วยกัน
รองรับหลายมิติ มิติเดียว
Range & Boolean indexing เฉพาะ Range indexing
Python สำหรับผู้ใช้ MATLAB

ควรใช้อะไรเมื่อไหร่?

  • ต้องการคำนวณตัวเลข?

    • NumPy array
  • เก็บโครงสร้างข้อมูลซับซ้อน?

    • list
  • ข้อมูลหลายมิติ?

    • NumPy array
Python สำหรับผู้ใช้ MATLAB

มาฝึกกันเถอะ!

Python สำหรับผู้ใช้ MATLAB

Preparing Video For Download...