網站首頁 編程語言 正文
1.列表(List)
元組是由一對方括號構成的序列。列表創建后,可以根據自己的需要改變他的內容
>>> list=[1,2,3,4,5,6] >>> list[0]=8 >>> list[6]=0 >>> list [8, 2, 3, 4, 5, 6]
可以為列表添加新的數據:
>>> len(list) #查看這個列表中有多少數據 6 >>> list.append(7) #在列表尾插入 >>> list [8, 2, 3, 4, 5, 6, 7] >>> len(list) 7 >>> list.insert(3,10) ?#在列表任意位置插入數據,第一個參數表示索引,第二個參數表示插入的數據 >>> list [8, 2, 3, 10, 4, 5, 6, 7] >>>?
2.元組(Tuple)
元組是由一對圓括號構成的序列。元組創建后,不允許更改,即他的內容無法被修改,大小也無法改變。
>>> tuple=(1,2,3,4) >>> tuple (1, 2, 3, 4) >>> tuple[2] 3 >>> tuple[2]=8 Traceback (most recent call last): ? File "", line 1, in ? ? tuple[2]=8 TypeError: 'tuple' object does not support item assignment
雖然元組不支持改變大小,但可以將兩個tuple
進行合并。
>>> t=(5,6,8,6) >>> t+tuple (5, 6, 8, 6, 1, 2, 3, 4)
元組中的值雖然不允許直接更改,但我們可以利用列表來改變元組中的值,可以使用函數list()
將元組變為列表,使用函數tuple()將列表轉換為元組。
?>>>t=(5631,103,"Finn","Bilous","Wanaka","1999-09-22") ?>>> print (t) ?(5631,103,"Finn","Bilous","Wanaka","1999-09-22") ?>>>lst = list(t) ?>>>print (lst) ?[5631,103,"Finn","Bilous","Wanaka","1999-09-22"] ?>>>lst[4] = 'LA' ?>>>t= tuple(lst) ?>>>print(t) ?(5631,103,"Finn","Bilous","LA","1999-09-22")
在元組中查找指定值可以使用in關鍵詞,使用函數index()
能夠返回查找到的值在元組中的索引。
n=103 if n in t:#在元組t中查找103 ? ? indexn = t.index(n)#查找值在元組中的索引值(從0開始算) print(indexn)
輸出結果:
1
n="Finn" if n in t: ? ? indexn = t.index(n) print(indexn)
輸出結果:
2
原文鏈接:https://blog.csdn.net/m0_67392409/article/details/123629081
相關推薦
- 2023-05-22 python使用ctypes調用第三方庫時出現undefined?symbol錯誤詳解_python
- 2022-07-30 find、filter、map的區別
- 2022-07-04 pyqt5-tools安裝失敗的詳細處理方法_python
- 2022-12-24 React?組件的狀態下移和內容提升的操作方法_React
- 2022-03-30 聊聊redis-dump工具安裝問題_Redis
- 2022-04-09 C語言實現計算器的兩種方法_C 語言
- 2023-02-15 Python進行ffmpeg推流和拉流rtsp、rtmp實例詳解_python
- 2022-11-10 Netcore?Webapi返回數據的三種方式示例_C#教程
- 最近更新
-
- 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同步修改后的遠程分支