網(wǎng)站首頁 編程語言 正文
python使用函數(shù)改變list
函數(shù)內(nèi)改變外部的一個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中的內(nèi)容需要這么寫
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函數(shù)用法
描述
list()函數(shù)是Python的內(nèi)置函數(shù)。它可以將任何可迭代數(shù)據(jù)轉(zhuǎn)換為列表類型,并返回轉(zhuǎn)換后的列表。當(dāng)參數(shù)為空時,list函數(shù)可以創(chuàng)建一個空列表。
語法
list(object)
名稱 | 說明 | 備注 |
object | 待轉(zhuǎn)換為列表的數(shù)據(jù)類型 | 可省略的參數(shù) |
使用示例
1. 創(chuàng)建一個空列表(無參調(diào)用list函數(shù))
>>> test = list() >>> test []
2. 將字符串轉(zhuǎn)換為列表
>>> test = list('cat') >>> test ['c', 'a', 't']
3. 將元組轉(zhuǎn)換為列表
>>> a_tuple = ('I love Python.', 'I also love HTML.') >>> test = list(a_tuple) >>> test ['I love Python.', 'I also love HTML.']
4. 將字典轉(zhuǎn)換為列表
>>> a_dict = {'China':'Beijing', 'Russia':'Moscow'} >>> test = list(a_dict) >>> test ['China', 'Russia']
??注意:將字典轉(zhuǎn)換為列表時,會將字典的值舍去,而僅僅將字典的鍵轉(zhuǎn)換為列表。如果想將字典的值全部轉(zhuǎn)換為列表,可以考慮使用字典方法dict.values()
5. 將集合轉(zhuǎn)換為列表
>>> a_set = {1, 4, 'sdf'} >>> test = list(a_set) >>> test [1, 'sdf', 4]
6. 將其他可迭代序列轉(zhuǎn)化為列表
下面的代碼將range類型和map類型的可迭代序列轉(zhuǎn)換為列表:
>>> 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]
注意事項(xiàng)
1. 參數(shù)必須是可迭代序列對象
list函數(shù)的參數(shù)必須是可迭代對象。當(dāng)選用不可迭代的對象作為參數(shù)時,Python報錯。
>>> test = list(12)
Traceback (most recent call last):
? File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable
將列表轉(zhuǎn)換為列表
可以使用list函數(shù)將列表轉(zhuǎn)換為一個列表,這么做Python不會有任何的異常或者報錯。它的作用是將參數(shù)列表進(jìn)行深拷貝:
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
相關(guān)推薦
- 2022-07-17 關(guān)于elasticsearch連接時斷時續(xù)以及Kibana出現(xiàn)server is not ready
- 2022-11-17 python實(shí)現(xiàn)excel轉(zhuǎn)置問題詳解_python
- 2022-07-11 BeanDefinition的作用 及 new ApplicationContext容器的加載過程
- 2023-03-30 C語言對結(jié)構(gòu)體數(shù)組按照某項(xiàng)規(guī)則進(jìn)行排序的實(shí)現(xiàn)過程探究_C 語言
- 2022-08-03 Django框架中模型的用法_python
- 2022-11-03 一文詳解C++子類函數(shù)為什么不能重載父類函數(shù)_C 語言
- 2023-01-19 python中的字符串切割?maxsplit_python
- 2022-08-06 GIN的路由以及傳參問題_Golang
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支