简体   繁体   中英

Unable to properly find the number of sublists within a list

I have this list:

 x = [[[595.5  92.5  72.1]
     [253.5 274.5  88.1]
     [433.5  94.5  75.8]
     [458.5 276.5  85.3]
     [132.5  93.5  58.8]
     [764.5  92.5  79.6]
     [666.5 277.5  93.5]
     [275.5  92.5  67.7]]]

When I do len(x) it gives me 1, but we have 8 lists. I don't understand why, How do I get the value 8?

len(x[0])

Since you want to get the 1st dimension length

In other words, len(x) gives number of items at 0th dimension For eg

> x=1,2
> len(x)
2
> x=1,2,3
> len(x)
3

To get the number of rows in the first item

len(x[0])

To get the number of columns in the first item and first row

len(x[0][0])

To get the number of columns in the first item and second row

len(x[0][1])

So on and so forth

The issue is that you have a 3D list. That is, there are 3 levels of lists.

There is one top-level list (first pair of []). This top-level list contains one more list (second pair of []). Inside this second list, you finally have the 8 lists.

So when you do len(x), it looks at the top-level list and sees only one element inside it, which is the inner list.

So,

  • len(x) = 1 since there is only one list inside it
  • len(x[0]) is the length of the one list inside your top-level list . This is what you're looking for since this second inner list contains your remaining 8 lists.

TLDR: You need to use len(x[0])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM