亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

獲取列表列表列表的長度

獲取列表列表列表的長度

墨色風雨 2022-10-25 15:58:19
我有一些看起來有點奇怪的列表,如下所示:listlist = [[], [[[[], []], [[]], []]], [[]]]我試圖找到所有列表長度的總和。如果它是一個簡單的多維數組(nxnxnx ...),找到列表長度的總和將非常容易,但在這種情況下,我不確定應該從哪里開始。我嘗試使用'for循環',但我認為這不是對這類問題的明確答案,應該有一個非常簡單的方法來解決這類問題。我會很感激任何建議。提前致謝!=]
查看完整描述

3 回答

?
千巷貓影

TA貢獻1829條經驗 獲得超7個贊

這可能會將您推向正確的方向:


def lenall(lst):

    if isinstance(lst, list):

        return 1 + sum(map(lenall, lst))

    return 0


>>> lenall(listlist)

12

這實際上計算了數據結構中列表的總數。如果要添加所有長度,則必須將其更改為:


def lenall(lst):

    if isinstance(lst, list):

        return len(lst) + sum(map(lenall, lst))

    return 0


>>> lenall(listlist)

11


查看完整回答
反對 回復 2022-10-25
?
拉丁的傳說

TA貢獻1789條經驗 獲得超8個贊

這是一個可能的解決方案:


listlist = [[], [[[[], []], [[]], []]], [[]]]


def length(lst):

    return len(lst) + sum(length(l) for l in lst if isinstance(l, list))


print(length(listlist))

輸出


11


查看完整回答
反對 回復 2022-10-25
?
子衿沉夜

TA貢獻1828條經驗 獲得超3個贊

這是應該做你想做的事情的代碼。


我包含了該函數的兩個版本,一個是為了清楚起見,另一個是更小但功能相同。


####################

# Create a list of lists / elements

####################

#x = [[1,2,3],[3,4],[3,4,[3,4,5],[4],[5,4,6]],[4,3,4,5,[[[6]]]]]

x = [[], [[[[], []], [[]], []]], [[]]]


############################################################

# First implementation (included for clarity)

############################################################

def get_len_lists(this_list):

    # Set number of elements to 0

    num_elem = 0                              


    # Loop through each element in the list...

    for elem in this_list:                          


        # .. if it's a list...

        if type(elem) == list:                      


            # ... if the list is empty, count that as an element

            if elem == list():                      

                # ... so add one

                num_elem += 1                       

            else:

                # ... get the number of elements...

                num_elem += get_len_lists(elem) + 1 


        # ... otherwise...

        else:                                       

            # ... just add one to the length of the list

            num_elem += 1                           


    # Return the number of elements in the list

    return num_elem                                 



############################################################

# Smaller implementation

############################################################

def get_len_lists_2(this_list):

    # Set number of elements to 0

    num_elem = 0                                    


    # Loop through each element in the list...

    for elem in this_list:                          


        # We add one for each level, regardless of whether it is an element or a list

        num_elem += 1                              


        # If it's a list...

        if type(elem) == list:                      


            # .. get the number of elements in the list

            num_elem += get_len_lists(elem)         


    # Return the number of elements in the list

    return num_elem                                 



result1 = get_len_lists(x) + 1

result2 = get_len_lists_2(x) + 1


print(result1)

print(result2)

輸出是:


12

12


查看完整回答
反對 回復 2022-10-25
  • 3 回答
  • 0 關注
  • 170 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號