Lists ใน Python

Python เบื้องต้นสำหรับการเงิน

Adina Howe

Professor

Lists - วงเล็บเหลี่ยม [ ]

months = ['January', 'February', 'March', 'April', 'May', 'June']
Python เบื้องต้นสำหรับการเงิน

Python เริ่มนับ index จากศูนย์

list_figure

Python เบื้องต้นสำหรับการเงิน

การเลือกข้อมูลจาก list

months = ['January', 'February', 'March', 'April', 'May', 'June']
months[0]
'January'
months[2]
'March'
Python เบื้องต้นสำหรับการเงิน

การใช้ index ติดลบใน list

months = ['January', 'February', 'March', 'April', 'May', 'June']
months[-1]
'June'
months[-2]
'May'
Python เบื้องต้นสำหรับการเงิน

การเลือกหลายรายการด้วย slicing

ไวยากรณ์การ slicing

# Includes the start and up to (but not including) the end
mylist[startAt:endBefore] 

ตัวอย่าง

months = ['January', 'February', 'March', 'April', 'May', 'June']
months[2:5]
['March', 'April', 'May']
months[-4:-1]
['March', 'April', 'May']
Python เบื้องต้นสำหรับการเงิน

การ slicing แบบขยาย

months = ['January', 'February', 'March', 'April', 'May', 'June']
months[3:]
['April', 'May', 'June']
months[:3]
['January', 'February', 'March']
Python เบื้องต้นสำหรับการเงิน

การ slicing แบบกำหนด step

# Includes the start and up to (but not including) the end
mylist[startAt:endBefore:step] 
months = ['January', 'February', 'March', 'April', 'May', 'June']
months[0:6:2]
['January', 'March', 'May']
months[0:6:3]
['January', 'April']
Python เบื้องต้นสำหรับการเงิน

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

Python เบื้องต้นสำหรับการเงิน

Preparing Video For Download...