在python中內(nèi)置了多種序列, 比較重要的序列有三個, 分別是字符串踩麦、列表和元祖, 序列是一種數(shù)據(jù)結(jié)構(gòu)
序列的通用操作
- 索引
# 字符串
str = 'hello'
print(str[0]) # h
print(str[-1]) # 0
# 列表
lst = [1, 'hi', True]
print(lst[0]) # 1
print(lst[-1]) # True
# 元祖
tup = (1, 'hi', True)
print(tup[0]) # 1
print(tup[-1]) # True
- 切片
str = 'hello'
print(str[1:2]) # e
print(str[:2]) # he
print(str[1:]) # ello
print(str[1:2:2]) # 步長為2, e
print(str[::2]) # hlo
- 加法
s1 = 'hello '
s2 = 'world'
print(s1 + s2) # hello world
lst1 = [1, 2, 3]
lst2 = [4, 5, 6]
print(lst1 + lst2) # [1, 2, 3, 4, 5, 6]
- 乘法
s1 = 'hello'
print(s1 * 5) # hellohellohellohellohello
lst1 = [1, 2]
print(lst1 * 3) # [1, 2, 1, 2, 1, 2]
- 元素是否存在
s1 = 'hello'
print('l' in s1) #True
lst1 = [1, 'hello', True]
print('he' in lst1); #False
- 長度爱榕、最大值、最小值
//長度
s1 = 'hello'
print(len(s1)) # 5
lst = [1, 2, 3, 'hello', True]
print(len(lst)) # 5
//最大值最小值
lst = [1, 2, 3, 6, 2]
print(max(lst)) #6
print(min(lst)) #1
列表
- list
lst = list('hello')
print(lst) # ['h', 'e', 'l', 'l', 'o']
print(''.join(lst)) # hello
- 修改刪除元素
# 修改元素的值
lst = [1, 2, 3, 4]
lst[2] = 6
print(lst) # [1, 2, 6, 4]
# 刪除元素
del lst[1]
print(lst) # [1, 6, 4]
# 給切片賦值
lst[2:] = list('hello')
print(lst) # [1, 6, 'h', 'e', 'l', 'l', 'o']
- 列表方法
lst = [1, 2, 3]
# append, 追加到末尾
lst.append(4)
print(lst) # [1, 2, 3, 4]
# clear, 清空列表
lst.clear()
print(lst) # [] 等價于 lst[:] = []
# copy, 會拷貝一個副本
lst1 = [1, 2, 3]
lst2 = lst1
lst2[1] = 10
print(lst1) # [1, 10, 3]
lst3 = lst1.copy()
lst3[1] = 20
print(lst1) # [1, 10, 3]
# count, 指定元素在列表中出現(xiàn)的次數(shù)
lst4 = [1, 2, 3, 4, 4, 2, 4]
print(lst4.count(4)) # 3
# extend, 用一個列表擴展另外一個列表
a = [1, 2, 3]
b = [4, 5, 6]
a.extend(b)
print(a) # [1, 2, 3, 4, 5, 6], 和+的區(qū)別, +并不會修改a
# index, 指定值第一個出現(xiàn)的索引
print(a.index(1)) # 0
# insert, 將一個對象插入到列表
c = [1, 2, 3]
c.insert(2, 5)
print(c) # [1, 2, 5, 3]
# pop, 刪除, 末尾刪除, 并返回這一元素, 出棧, 沒有提供push, 可以使用append代替
d = [1, 2, 3]
print(d.pop()) # 3
print(d) # [1, 2]
# remove, 刪除第一個為指定值的元素
e = [1, 2, 4, 5]
e.remove(4)
print(e) # [1, 2, 5]
# reverse, 反轉(zhuǎn)
f = [1, 2, 3]
f.reverse()
print(f) # [3, 2, 1]
# sort, 排序
g = [4, 6, 2, 7, 3]
g.sort()
print(g) # [2, 3, 4, 6, 7]
元祖
和列表一樣, 唯一的不同就是元祖不能修改
t = (1, 2, 3)
// 列表轉(zhuǎn)元祖
t1 = tupe([1, 2, 3])
字符串
字符串是不可變的,因此所有的元素賦值和切片賦值都是非法的
- center
通過填充字符(默認(rèn)空格)來讓字符串居中
s1 = 'hello'
s2 = s1.center(30)
s3 = s1.center(30, '*')
print(s2) # hello
print(s3) # ************hello*************
- find
在字符串中查找子串, 存在就返回索引, 不存在就返回-1
s1 = 'hello'
index1 = s1.find('l')
print(index1) # 2
s2 = 'hello world'
index2 = s2.find('world') # 6
index3 = s2.find('world', 1, 7) # -1, 第二個參數(shù)包含起點,第三個參數(shù)不包含終點
print(index2)
print(index3)
- join
與splite相反, 合并序列的元素
lst = ['1', '2', '3', '4']
s1 = ''.join(lst) # 1234
s2 = '-'.join(lst) # 1-2-3-4
print(s1)
print(s2)
- lower
返回字符串的小寫
'Hello'.lower() # hello
- replace
一個字符串替換另外一個字符串, 并返回新串
'hello world'.replace('world', 'python') # hello python
- split
與join相反, 字符串拆分為序列
'hello world'.split(' ') #['hello', 'world']
- strip
將字符串的開頭和末尾空白刪除
' hello, world '.strip() # 'hello, world'
- translate
和replace都是替換字符串, - 判斷是否滿足條件
存在很多以is開頭的方法, 判斷字符串是否具有特定的性質(zhì), 如: islower, isupper.....