元组

2022/8/8 23:25:51

本文主要是介绍元组,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

##元组
列表-[元素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])



这篇关于元组的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程