為什麼在 Python 中的函數中修改了列表?

固態硬盤

我有以下代碼:

def recursive_sort(list_to_sort, key):
"""
sort a list by a specified key recursively
"""
if len(list_to_sort) == 1:
    return list_to_sort
for i in range(0,len(list_to_sort) - 1):
    if list_to_sort[i][key] > list_to_sort[i + 1][key]:
        list_to_sort[i], list_to_sort[i+1] = list_to_sort[i+1], list_to_sort[i]

return recursive_sort(list_to_sort[:-1], key) + [list_to_sort[-1]]

我在 main() 中使用以下命令運行它:

sensor_list = [('4213', 'STEM Center', 0), ('4201', 'Foundations Lab', 1), ('4204', 'CS Lab', 2), ('4218', 'Workshop Room', 3), ('4205', 'Tiled Room', 4), ('Out', 'Outside', 10)]


print("\nOriginal unsorted list\n", sensor_list)
print("\nList sorted by room number\n", recursive_sort(sensor_list, 0))
print("\nList sorted by room name\n", recursive_sort(sensor_list, 1))
print("\nOriginal unsorted list\n", sensor_list)

這將打印輸出:

Original unsorted list
 [('4213', 'STEM Center', 0), ('4201', 'Foundations Lab', 1), ('4204', 'CS Lab', 2), 
('4218', 'Workshop Room', 3), ('4205', 'Tiled Room', 4), ('Out', 'Outside', 10)]

List sorted by room number
 [('4201', 'Foundations Lab', 1), ('4204', 'CS Lab', 2), ('4205', 'Tiled Room', 4), 
('4213', 'STEM Center', 0), ('4218', 'Workshop Room', 3), ('Out', 'Outside', 10)]

List sorted by room name
 [('4204', 'CS Lab', 2), ('4201', 'Foundations Lab', 1), ('Out', 'Outside', 10), 
('4213', 'STEM Center', 0), ('4205', 'Tiled Room', 4), ('4218', 'Workshop Room', 3)]

Original unsorted list
 [('4204', 'CS Lab', 2), ('4201', 'Foundations Lab', 1), ('4213', 'STEM Center', 0), 
('4205', 'Tiled Room', 4), ('Out', 'Outside', 10), ('4218', 'Workshop Room', 3)]

為什麼第一次和第四次打印返回不同的列表,sensor_list 不應該保持不變嗎?

拉里羊駝

正如@PresidentJamesK.Polk 所提到的,這稱為就地修改這意味著無論你對參數/變量做什麼,它都會在函數之外修改它。發生這種情況是因為您正在修改參數本身而不是將其複製到列表中。為了防止這種情況,在你的函數中你可以說:

def recursive_sort(list_to_sort, key):
   result = list_to_sort.copy() # Or just regular assignment, but this is guaranteed to prevent in-place modification
   
   # Subsequent code with 'list_to_sort' replaced with result so
   # that it modifies the new, copied variable instead of the original

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

為什麼 Python 將列表匹配為元組?

為什麼在python中沒有執行異步函數

為什麼 Python 列表在排序時變慢?

為什麼我的變量只在 Python 中的某個函數/語句中發生變化?

為什麼在 Python 中用參數調用這個“構造函數”?

為什麼這個簡單的python函數只能工作一次

為什麼python列表佔用這麼多內存?

為什麼我的圖像在 OpenCV Python 中顯示為灰色?

在python中為什麼列表使用“+”而字典使用“|” 達到同樣的目的?

為什麼 Python 在計算中將精確小數轉換為近似值?

為什麼 re.sub() 在 Python 中默認添加不匹配的字符串?

為什麼 Selenium 通過 xpath 查找元素在 Python 中工作而在 Java 中出錯?

為什麼在 PostgreSQL Python 中更新表時行會被拋到底部?

為什麼我的遞歸方法在 Python 中不起作用?

為什麼我的死亡矩形在 python pygame 中沒有移動

為什麼我的圖像在 Python 中無法識別?

為什麼 Python 中的原子分組比簡單的非捕獲交替慢?

為什麼 |= 可以在 Python 中對字典進行就地值更新?

為什麼“ndarray.all() is False”在python中總是返回False?

Python 從網頁中抓取鏈接 - 為什麼沒有 URL?

為什麼 Python 整數輸入不能正確檢查?

為什麼當我更改複製的列表時原始列表會更改(PYTHON)

在 python 中的列表中鍵入轉換錯誤。確保它作為整數 [] 而不是 str 列表返回的正確方法是什麼?

為什麼標準 ml 中的此函數附加列表不起作用?

為什麼 Python IDLE 不好?

那麼,為什麼我必須在基類中定義虛函數?

為什麼 Python 的 yield 語句不能在一個函數中調用,也不能在它調用的另一個函數中調用,為什麼在嘗試時沒有報告錯誤?

為什麼我不能在函數中設置我的構造函數?

在java中,為什麼它很慢?