400-650-7353
您所在的位置:首頁 > IT干貨資料 > python > 【Python基礎知識】Python通用序列操作

【Python基礎知識】Python通用序列操作

  • 發(fā)布: python培訓
  • 來源:python干貨資料
  • 2020-10-30 09:49:02
  • 閱讀()
  • 分享
  • 手機端入口

列表和字符串都是Python中的序列類型,它們有很多共同特性,如都可以進行“+”操作和“*”操作,都可以使用for循環(huán)迭代等。

為什么要使用序列呢?通過圖中有序與無序的對比可能會得出答案,在很多情況下,有序的序列可能會更加方便操作。

序列是有序元素的集合。在計算機中,序列的一個典型示例就是在內(nèi)存中保存數(shù)據(jù),內(nèi)存的地址是從小到大有序排列的,每一個地址存放一個數(shù)據(jù),如圖所示。

實際上,Python中的序列有一些操作是通用的,即可以用到每一種序列類型中。以下序列操作分別用列表和字符串舉例。

1. min()函數(shù)和max()函數(shù)

min()函數(shù)和max()函數(shù)分別返回序列的最小項和最大項。

  1. >>> numbers = [15, -2342102
  2. >>> max(numbers) 
  3. 102 
  4. >>> min(numbers) 
  5. -2 
  6. >>> max('Python'
  7. 'y' 
  8. >>> min('Python'
  9. 'P' 

2. in和not in

使用in和not in操作符來判斷某個子序列是否在該序列中:

  1. >>> 1 in [123
  2. True 
  3. >>> 4 not in [123
  4. True 
  5. >>> 'p' in 'Python'  # Python區(qū)分大小寫 
  6. False 
  7. >>> 'yth' in 'Python'  # 不僅僅可以判斷單個字符 
  8. True 

3. “+”和“*”

使用“+”操作符來拼接序列,使用“*”操作符來重復相加序列:

  1. >>> 'Py' + 'thon' 
  2. 'Python' 
  3. >>> 'I love you!' * 5 
  4. 'I love you!I love you!I love you!I love you!I love you!' 

列表的“+”操作與extend()方法類似,但是“+”操作不是就地操作,有返回值:

  1. >>> list1 = [123
  2. >>> list2 = [456
  3. >>> list3 = list1 + list2 
  4. >>> list3 
  5. [123456
  6. >>> list4 = list1.extend(list2) 
  7. >>> list4  # list4是None 
  8. >>> list1  # list2追加到了list1上 
  9. [123456

包含數(shù)字的列表和包含字符串的列表進行“*”操作:

  1. >>> numbers_list = [1] * 3  
  2. >>> strings_list = ['Python'] * 3 
  3. >>> numbers_list 
  4. [111
  5. >>> strings_list 
  6. ['Python''Python''Python'
  7. >>> numbers_list[0] = 3  
  8. >>> strings_list[0] = 'C'  
  9. >>> numbers_list   
  10. [311
  11. >>> strings_list 
  12. ['C''Python''Python'

4. 索引和切片

索引和切片都是通用的序列操作,因此,不僅列表有索引和切片,字符串也有索引和切片:

  1. >>> word = 'Python' 
  2. >>> word[0]  # 第1個字符 
  3. 'P' 
  4. >>> word[-2]  # 倒數(shù)第2個字符 
  5. 'o' 
  6. >>> word[:2]  # 前2個字符 
  7. 'Py' 
  8. >>> word[:2] + word[2:]  # 字符拼接 
  9. 'Python' 
  10. >>> word[-3:]  # 后3個字符 
  11. 'hon' 

5. len()函數(shù)

len()函數(shù)用于獲取序列的長度:

  1. >>> words = """Python is a programming language that lets you work quickly and integrate systems more effectively.""" 
  2. >>> len(words) 
  3. 99 
  4. >>> lists_ = ['Python'312, []] 
  5. >>> len(lists) 
  6. 3 

6. index()方法

序列中的index()方法用于查找第一個出現(xiàn)指定子序列的索引位置,如果不存在,那么會拋出ValueError異常:

  1. >>> word = 'banana' 
  2. >>> word.index('a'
  3. 1 
  4. >>> word.index('na'
  5. 2 
  6. >>> word.index('an'
  7. 1 
  8. >>> word.index('c'
  9. Traceback (most recent call last): 
  10.   File "", line 1in  
  11. ValueError: substring not found 

index()方法也可以指定查找范圍,即查找索引位置的起始值和結束值:

  1. >>> numbers = [31415
  2. >>> numbers.index(1
  3. 1 
  4. >>> numbers.index(12
  5. 3 
  6. >>> word = 'banana' 
  7. >>> word.index('a'24)    
  8. 3 

7. count()方法

不僅僅是列表,每一種序列類型都有count()方法:

  1. >>> word = 'banana' 
  2. >>> word.count('a'
  3. 3 
  4. >>> word.count('na'
  5. 2 
  6. >>> word.count('c'
  7. 0 
  8. >>> numbers = [101011
  9. >>> numbers.count(0
  10. 2 
  11. >>> numbers.count(1
  12. 4 

如果對Python開發(fā)感興趣或者想要深入學習的現(xiàn)在可以免費領取學習大禮包哦(點擊領取80G課程資料 備注:領資料)。

文章“【Python基礎知識】Python通用序列操作”已幫助

>>本文地址:http://nfbqydst.cn/zhuanye/2020/59212.html

THE END  

聲明:本站稿件版權均屬中公教育優(yōu)就業(yè)所有,未經(jīng)許可不得擅自轉(zhuǎn)載。

1 您的年齡

2 您的學歷

3 您更想做哪個方向的工作?

獲取測試結果
  • 大前端大前端
  • 大數(shù)據(jù)大數(shù)據(jù)
  • 互聯(lián)網(wǎng)營銷互聯(lián)網(wǎng)營銷
  • JavaJava
  • Linux云計算Linux
  • Python+人工智能Python
  • 嵌入式物聯(lián)網(wǎng)嵌入式
  • 全域電商運營全域電商運營
  • 軟件測試軟件測試
  • 室內(nèi)設計室內(nèi)設計
  • 平面設計平面設計
  • 電商設計電商設計
  • 網(wǎng)頁設計網(wǎng)頁設計
  • 全鏈路UI/UE設計UI設計
  • VR/AR游戲開發(fā)VR/AR
  • 網(wǎng)絡安全網(wǎng)絡安全
  • 新媒體與短視頻運營新媒體
  • 直播帶貨直播帶貨
  • 智能機器人軟件開發(fā)智能機器人
 

快速通道fast track

近期開班時間TIME