Python序列:字符串、元组、列表
扩展参考: https://realpython.com/python-lists-tuples/
序列Sequence
有一些Python的类型,它的成员是有序排列的,且可以通过下标偏移量访问它的一个或几个成员,这类Python类型统称为序列。
序列操作符 | 说明 |
---|---|
seq[start:stop:step ] | 切片 |
seq[index] | 获得某一元素 |
seq[index1:index2] | 起止的切片 |
seq1 + seq2 | 连接操作 |
seq * n | 重复操作 |
obj in / not in seq | 成员关系 |
例子:
# coding=utf-8
__author__ = 'zyt'
a = 'HelloWorld'
b = 'NiceToMeetYou'
for c in enumerate(a):
print c
print a[1]
print a[1:7:2]
print a[::-2]
print 'e' in a
print '---------'
print a[:]
print a[::]
print a[:5]
print a[:-3] # 默认第一个索引0或-len()
print '---------'
print a + b
print a * 3
print 'z' not in b
for i in range(-1, -len(b), -1):
print b[:i]
运行结果:
python test.py
(0, 'H')
(1, 'e')
(2, 'l')
(3, 'l')
(4, 'o')
(5, 'W')
(6, 'o')
(7, 'r')
(8, 'l')
(9, 'd')
e
elW
drWle
True
---------
HelloWorld
HelloWorld
Hello
HelloWo
---------
HelloWorldNiceToMeetYou
HelloWorldHelloWorldHelloWorld
True
NiceToMeetYo
NiceToMeetY
NiceToMeet
NiceToMee
NiceToMe
NiceToM
NiceTo
NiceT
Nice
Nic
Ni
N
对象的浅拷贝&深拷贝
copy模块 - Shallow and deep copy operations
copy.copy(x)
:Return a shallow copy of x.copy.deepcopy(x)
:Return a deep copy of x.