其他分享
首页 > 其他分享> > 元组

元组

作者:互联网

##元组
列表-[元素1,元素2,元素3,。。。。]
元组-(元素1,元素2,元素3,。。。。)支持count和index方法。

#代码:
rhyme = (1,2,3,4,5,"上山打老虎")
rhyme
(1, 2, 3, 4, 5, '上山打老虎')
rhyme = 1,2,3,4,5,"上山打老虎"
rhyme
(1, 2, 3, 4, 5, '上山打老虎')
rhyme[0]
1
rhyme[-1]
'上山打老虎'
rhyme[:3]
(1, 2, 3)
rhyme[3:]
(4, 5, '上山打老虎')
rhyme[::2]
(1, 3, 5)
rhyme[::-1]
('上山打老虎', 5, 4, 3, 2, 1)

nums = (2,3,5,4,3,1,2,3,4)
nums.count(3)
3
heros = ("蜘蛛侠","绿巨人","黑寡妇")
heros.index("黑寡妇")
2
s = (1,2,3)
t = (4,5,6)
s + t
(1, 2, 3, 4, 5, 6)
s * 3
(1, 2, 3, 1, 2, 3, 1, 2, 3)
w =s, t
w
((1, 2, 3), (4, 5, 6))


for each in s:
print(each)


1
2
3
type(x)
Traceback (most recent call last):
File "<pyshell#28>", line 1, in <module>
type(x)
NameError: name 'x' is not defined
for i in w:
for each in i:
print(each)


1
2
3
4
5
6
s = (1,2,3,4,5)
[each * 2 for each in s]
[2, 4, 6, 8, 10]
(each * 2 for each in s)
<generator object <genexpr> at 0x00000284FA46FCA0>
x = (520)
x
520
type(x)
<class 'int'>
x = (520,)
x
(520,)
type(x)
<class 'tuple'>
t = (123,"Flish",3.14)
x, y, z = t
x
123
y
'Flish'
z
3.14
t = [123,"Flish",3.14]
x, y, z = t
x
123
y
'Flish'
z
3.14
a,b,c,d,e = "Flish"
a
'F'
b
'l'
c
'i'
d
's'
e
'h'

 


x
x,y = 10,20
x
10
y
20
_=(10,20)
x,y=_
x
10
y
20
s = [1,2,3]
t = [4,5,6]
w = (s,t)
w
([1, 2, 3], [4, 5, 6])
w[0][0]=0
w
([0, 2, 3], [4, 5, 6])
w[1][1]
5
w[1][1]=1
w
([0, 2, 3], [4, 1, 6])

标签:元素,rhyme,上山,老虎,Flish,each,元组
来源: https://www.cnblogs.com/chongqing/p/16561865.html