タプルの組み込み関数

! ! ! 注: タプルが指すメモリの内容は不変です

タプルはメモリを指す

>>> tup = ('P','y','t','h','o','n')
>>> tup[0] = 'p'
Traceback (most recent call last):
  File "<pyshell#17>", line 1, in <module>
    tup[0] = 'p'
TypeError: 'tuple' object does not support item assignment
>>> id(tup)
2003349130600
>>> tup = (1,2,3)
>>> id(tup)
2003349651600
>>> 

1. len(tuple) はタプル要素の数を計算します

>>> tup = (1,2,3)
>>> len(tup)
3
>>> 

2. max(tuple) はタプルの要素の最大値を返します

>>> tup = (1,2,3)
>>> max(tup)
3
>>> 

3. min(tuple) はタプルの要素の最小値を返します

>>> tup = (1,2,3)
>>> min(tup)
1
>>> 

4番目に、 tuple(seq) はリストをタプルに変換します

>>> list = ['messi','xavi','Iniesta']
>>> list
['messi', 'xavi', 'Iniesta']
>>> tup = tuple(list)
>>> tup
('messi', 'xavi', 'Iniesta')
>>> 

おすすめ

転載: blog.csdn.net/weixin_42676530/article/details/105726862