python中修改元组

 

>>> test1 = ("aaa","bbb","ccc","ddd","eee","fff","ggg")
>>> type(test1)
<class 'tuple'>
>>> test2 = test1[:4]
>>> test2
('aaa', 'bbb', 'ccc', 'ddd')
>>> test3 = test1[4:]
>>> test3
('eee', 'fff', 'ggg')
>>> insert = "xxx",
>>> test4 = test2 + insert + test3
>>> test1
('aaa', 'bbb', 'ccc', 'ddd', 'eee', 'fff', 'ggg')
>>> test4
('aaa', 'bbb', 'ccc', 'ddd', 'xxx', 'eee', 'fff', 'ggg')
>>> test5 = test1[:3]
>>> test5
('aaa', 'bbb', 'ccc')
>>> test6 = test1[4:]
>>> test6
('eee', 'fff', 'ggg')
>>> displace = "yyy",
>>> test7 = test5 + displace + test6
>>> id(test1)
2326018208096
>>> id(test7)
2326018299264
>>> test1
('aaa', 'bbb', 'ccc', 'ddd', 'eee', 'fff', 'ggg')
>>> test7
('aaa', 'bbb', 'ccc', 'yyy', 'eee', 'fff', 'ggg')

 

上一篇:Python入门基础篇 No.3 —— 变量的声明_初始化_删除变量_垃圾回收机制


下一篇:python中列表的去重复和取重复