第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

Python 函數(shù)行為

Python 函數(shù)行為

開心每一天1111 2024-01-24 15:22:52
如果我有一個使用以下形式的元組列表 構(gòu)建最小堆結(jié)構(gòu)的函數(shù): [(vertex),length,(another_vertex),...........],我將該函數(shù)寫入采用兩個輸入:堆結(jié)構(gòu)和先前形式的稱為三元組的新元素。它應(yīng)該根據(jù)“(頂點)”從添加的三元組中構(gòu)建沒有重復(fù)項的堆,并根據(jù)“長度”以升序方式構(gòu)建堆,我將其編寫如下:def heap_add_or_replace(heap, triplet):   heap.append(triplet)   # Sorting according to the first element of every triplet to have duplicates get next to each other   vertexSort = sorted(heap, key = lambda x: x[0])   # Sorting according to the distance in ascending manner   lenSort = sorted(vertexSort, key = lambda x: x[1])   # Function to remove duplicates   def remvDuplicate(struct):     check = set()     result = []     for i in struct:        if i[0] not in check:            result.append(i)            check.add(i[0])     return result  # Setting the final return value  heap = remvDuplicate(lenSort)return heap我的問題是使用以下兩種方法調(diào)用函數(shù)有什么區(qū)別:triplet_heap = list()a = heap_add_or_replace(triplet_heap,((2,3),0.9,(1,0)))print("the new heap is: " + str(a))b = heap_add_or_replace(triplet_heap,((7,2),0.3,(2,2)))print("the new heap is: " + str(b))和,new_heap = list()heap_add_or_replace(new_heap,((2,3),0.9,(1,0)))print("the new heap is: " + str(new_heap))heap_add_or_replace(new_heap,((7,2),0.3,(2,2)))print("the new heap is: " + str(new_heap))因為在第二種方法中該函數(shù)的行為不正確:正確的輸出 - 第一次調(diào)用:新堆是: [((2, 3), 0.9, (1, 0))]新堆是: [((7, 2), 0.3 , (2, 2)), ((2, 3), 0.9 , (1, 0))]錯誤的輸出 - 第二次調(diào)用:新堆是: [((2, 3), 0.9, (1, 0))]新堆是: [((2, 3), 0.9 , (1, 0)), ((7, 2), 0.3 , (2, 2))]提前致謝。
查看完整描述

2 回答

?
慕神8447489

TA貢獻1780條經(jīng)驗 獲得超1個贊

在第一種情況下,即“正確的情況”,您正在查看函數(shù)的返回值heap_add_or_replace。

在第二種情況下,即“不正確的情況”,您將查看list每次將原始數(shù)據(jù)傳遞給heap_add_or_replace函數(shù)時會發(fā)生什么情況。

在該函數(shù)中,您在第一次進行排序時heap_add_or_replace復(fù)制輸入。list排序的結(jié)果以及此后所做的所有操作都不會反映在原始列表中。因此,您傳入的原始列表永遠不會被排序或過濾重復(fù)項。這就是兩個案例不同的原因。


查看完整回答
反對 回復(fù) 2024-01-24
?
嗶嗶one

TA貢獻1854條經(jīng)驗 獲得超8個贊

以下是與您的兩種情況類似的情況:


a = []


def f(l):

    l.append(0) #affect global variable a

    l = 5       #new local variable created inside the function

    return l    #return l = 5!


#first case

b = f(a)        #b is assigned value from return l (5)


#second case

print(a)        #printing global variable not result from function ([0])


查看完整回答
反對 回復(fù) 2024-01-24
  • 2 回答
  • 0 關(guān)注
  • 173 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號