Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธี Iterate ผ่าน List of Tuples ใน Python

วิธีง่ายที่สุด: ใช้ Nested For Loops 2 ชั้น Outer Loop ดึง Tuple แต่ละอัน Inner Loop วน Item ใน Tuple ใช้ print(y, end=' ') พิมพ์ Item ใน Tuple เดียวกันบรรทัด print() ขึ้นบรรทัดใหม่หลัง Tuple แต่ละอัน

ตัวอย่าง

L=[(1,2,3), (4,5,6), (7,8,9,10)]
for x in L:
    for y in x:
        print(y, end=' ')
    print()

ผลลัพธ์

1 2 3
4 5 6
7 8 9 10