简体   繁体   中英

How to get one value from a list of sublist?

I have been trying to get inside a list of sublist just to get one value out but cannot seem to get it working any ideas. Below is the list of sublist and for example I want to print just the first number of the first list out. Hope that makes sense. I have tried looking at other sources but none got exactly what I was after.

[[0, 0, 0, 3, 0, 4, 0, 3], [0, 0, 0, 0, 0, 3, 0, 7], [0, 0, 1, 0, 0, 5, 0, 4], [0, 0, 0, 1, 3, 1, 0, 5], [1, 1, 1, 0, 0, 0, 2, 5], [0, 0, 0, 1, 1, 5, 0, 3], [0, 0, 0, 5, 3, 0, 0, 2]]

To acces an item inside a list of lists, you fist select the list you want to access and then the item you want to access:

the first list is at index 0 and the first item is at index 0 of that:

list_of_lists = [[0,1,2,3],[10,11,12,13]]
first_of_first = list_of_lists[0][0]

for the third item of the second list it would be:

third_of_second = list_of_lists[1][2]

Note that python is 0 indexed, so the indexes always turn out one lower than the "Nth" item so the second item is at index 1. Also note that when you say "the third item of the second list" that the order in which you access it is in reverse, ie list_of_lists[1][2]

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