網站首頁 編程語言 正文
python使用函數改變list
函數內改變外部的一個list如果這么寫
def rotate(nums, k): ? ? length=len(nums) ? ? if length!=0: ? ? ? ? nums=nums[length-k:length]+nums[0:length-k] ? l=[1,2,3,4,5,6,7] rotate(l,3) print(l)
外部的list并沒有改變,而返回的是[1, 2, 3, 4, 5, 6, 7]
要改變list中的內容需要這么寫
def rotate(nums, k): ? ? length=len(nums) ? ? if length!=0: ? ? ? ? nums[:]=nums[length-k:length]+nums[0:length-k] ? l=[1,2,3,4,5,6,7] rotate(l,3) print(l)
這樣就返回的是[5, 6, 7, 1, 2, 3, 4]
python list函數用法
描述
list()函數是Python的內置函數。它可以將任何可迭代數據轉換為列表類型,并返回轉換后的列表。當參數為空時,list函數可以創建一個空列表。
語法
list(object)
名稱 | 說明 | 備注 |
object | 待轉換為列表的數據類型 | 可省略的參數 |
使用示例
1. 創建一個空列表(無參調用list函數)
>>> test = list() >>> test []
2. 將字符串轉換為列表
>>> test = list('cat') >>> test ['c', 'a', 't']
3. 將元組轉換為列表
>>> a_tuple = ('I love Python.', 'I also love HTML.') >>> test = list(a_tuple) >>> test ['I love Python.', 'I also love HTML.']
4. 將字典轉換為列表
>>> a_dict = {'China':'Beijing', 'Russia':'Moscow'} >>> test = list(a_dict) >>> test ['China', 'Russia']
??注意:將字典轉換為列表時,會將字典的值舍去,而僅僅將字典的鍵轉換為列表。如果想將字典的值全部轉換為列表,可以考慮使用字典方法dict.values()
5. 將集合轉換為列表
>>> a_set = {1, 4, 'sdf'} >>> test = list(a_set) >>> test [1, 'sdf', 4]
6. 將其他可迭代序列轉化為列表
下面的代碼將range類型和map類型的可迭代序列轉換為列表:
>>> test1 = list(range(10)) >>> test2 = list(map(int, [23.2, 33.1])) >>> test1 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> test2 [23, 33]
注意事項
1. 參數必須是可迭代序列對象
list函數的參數必須是可迭代對象。當選用不可迭代的對象作為參數時,Python報錯。
>>> test = list(12)
Traceback (most recent call last):
? File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable
將列表轉換為列表
可以使用list函數將列表轉換為一個列表,這么做Python不會有任何的異常或者報錯。它的作用是將參數列表進行深拷貝:
if __name__ == '__main__': source_list = ["a", "b", "c", "d"] new_list1 = list(source_list) print(id(source_list), id(new_list1)) # output: 4313597760 4312890304 new_list2 = source_list print(new_list1) # output: ['a', 'b', 'c', 'd'] print(new_list2) # output: ['a', 'b', 'c', 'd'] source_list[0] = "e" print(new_list1) # output: ['a', 'b', 'c', 'd'] print(new_list2) # output: ['e', 'b', 'c', 'd']
原文鏈接:https://blog.csdn.net/why12345678901/article/details/81271728
相關推薦
- 2023-11-14 Kubernetes常用命令(持續更新)
- 2024-01-31 1273 - Unknown collation: ‘utf8mb4_0900_ai_ci‘, Ti
- 2022-05-21 python實現會員管理系統_python
- 2023-03-28 Go字符串操作深入解析_Golang
- 2023-01-13 Pytorch實現Fashion-mnist分類任務全過程_python
- 2022-11-02 Python運維自動化之paramiko模塊應用實例_python
- 2022-07-22 px和em和rem的區別
- 2023-10-10 Object.defineProperty和Proxy分別實現響應式原理的簡單示例
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支