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

วิธีหา Length ของ List of Lists ใน Python

ใช้ Nested Loop นับจำนวน Elements ในแต่ละ Sublist

>>> a=[[1, 2, 3], [4, 5, 6]]
>>> c=0
>>> for x in a:
...     for y in x:
...         c=c+1
>>> c
6

วิธีอื่น: ใช้ sum(len(sublist) for sublist in a) หรือ len(list(itertools.chain.from_iterable(a)))