python元組簡單介紹
更新時間:2021年10月26日 10:32:45 作者:Silent丿丶黑羽
這篇文章主要給大家分享中得python基礎 元組,元組的特點是一種不可變序列,一旦創(chuàng)建就不能修改,帶著些許了解和小編一起進入文章得具體內(nèi)容吧
元組的特點:是一種不可變序列,一旦創(chuàng)建就不能修改
1、拆包
將元組的元素取出賦值給不同變量
>>> a = ('hello', 'world', 1, 2, 3) >>> str1, str2, n1, n2, n3 = a >>> str1 'hello' >>> str2 'world' >>> n1 1 >>> n2 2 >>> n3 3 >>> str1, str2, *n = a >>> str1 'hello' >>> str2 'world' >>> n [1, 2, 3] >>> str1, _, n1, n2, _ = a
2、enumerate
解釋:用于元組遍歷,獲得元組對象,第一個元素是索引,第二個是數(shù)值
a = ('1', 2, 35, 'hello') for i in enumerate(a): print(i) >>> (0, '1') >>> (1, 2) >>> (2, 35) >>> (3, 'hello')
3、list()
元組轉換成列表
Python a = ('1', 2, 35, 'hello') print(list(a)) >>> ['1', 2, 35, 'hello']
到此這篇關于python基礎 元組詳情的文章就介紹到這了,更多相關python 元組內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Python中用format函數(shù)格式化字符串的用法
這篇文章主要介紹了Python中用format函數(shù)格式化字符串的用法,格式化字符串是Python學習當中的基礎知識,本文主要針對Python2.7.x版本,需要的朋友可以參考下2015-04-04