且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从列表列表中的列表中提取最后一项?(Python)

更新时间:2022-06-22 22:45:27

我建议两次遍历列表,如下所示:

I would suggest looping through the list twice, like so:

lst = [[[11, 12, 15], [12, 13, 14], [13, 14, 15], [14, 15, 17], [15, 16, 17]], [[14, 15, 18], [15, 16, 17]]]

# Result of iteration
last_lst = []

# Iterate through lst
for item1 in lst:
    # Initialize temporary list
    last_item1 = []
    
    #Iterate through each list in lst
    for item2 in item1:
        # Add last item to temporary list
        last_item1.append(item2[-1])
        
    # Add the temporary list to last_lst
    last_lst.append(last_item1)