Lists ซ้อน Lists

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

Adina Howe

Professor

Lists ซ้อน Lists

  • List สามารถเก็บข้อมูลได้หลายประเภท รวมถึง list อื่นด้วย

  • ตัวอย่าง: nested list ที่เก็บชื่อเดือนและดัชนีราคาผู้บริโภค

      cpi = [['Jan', 'Feb', 'Mar'], [238.11, 237.81, 238.91]]
    
Python เบื้องต้นสำหรับการเงิน

การ Subset Nested Lists

months = ['Jan', 'Feb', 'Mar']
print(months[1])
'Feb'
cpi = [['Jan', 'Feb', 'Mar'], [238.11, 237.81, 238.91]]
print(cpi[1])
[238.11, 237.81, 238.91]
Python เบื้องต้นสำหรับการเงิน

การ Subset Nested Lists เพิ่มเติม

จะดึงดัชนีราคาเฉพาะค่าออกมาได้อย่างไร?

cpi = [['Jan', 'Feb', 'Mar'], [238.11, 237.81, 238.91]]
print(cpi[1])
[238.11, 237.81, 238.91]
print(cpi[1][0])
238.11
Python เบื้องต้นสำหรับการเงิน

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

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

Preparing Video For Download...