亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Python3中的列表,元組,字典,字符串相關(guān)知識小結(jié)

 更新時間:2017年11月10日 11:54:27   作者:Infi_chu  
這篇文章主要介紹了Python3中的列表,元組,字典,字符串相關(guān)知識小結(jié),小編覺得挺不錯的,分享給大家,需要的朋友可以參考下。

一、知識概要

  1. 列表,元組,字典,字符串的創(chuàng)建方式

  2. 列表,元組,字典,字符串的方法調(diào)用

  3. 列表,元組,字典,字符串的常規(guī)用法

二、列表

# 列 表

# 列表基礎(chǔ)
list_1 = ['a','b','c','d','e','f']
list_2 = ['apple','banana','watermelon','strawberry','banana','apple']
print(list_1)
print("##########")
# 列表得下標(biāo)是從0開始的,之后的一次+1
print(list_1[0])
print("##########")
print(list_1[4])
print("##########")
print(list_1[1:3]) # 從下標(biāo)為1的元素開始,到下標(biāo)為3的終止
print("##########")
print(list_1[1:-2])   # -2是指將列表的順序倒置,結(jié)尾變?yōu)殚_頭,下標(biāo)相對來說變?yōu)?向前依次 -1
print("##########")
print(list_1[::2]) # 前面兩個冒號分別使用默認(rèn)的參數(shù),最后一個數(shù)字表示步長,兩步一取
print("##########")

# 列表的增、刪、改
list_1.append('z') # 在結(jié)尾加一個元素
print(list_1)
print("##########")
list_1.insert(1,'y')  # 在指定位置增加元素,在a后b前插入y
print(list_1)
print("##########")
a = list_2.extend(list_1)    # 將list_1和list_2合并
print(a)
print("##########")
list_1[4] = 'o'   # 修改第五個元素
print(list_1)
print("##########")
list_1[2:3] = ['p','q']   # 修改連續(xù)的元素
print(list_1)
print("##########")
list_1.remove(list_1[3])    # 刪除下標(biāo)為3的元素
print(list_1)
print("##########")
list_1.pop(2)    # 直接加下標(biāo)
print(list_1)
print("##########")
# del list_1[2]  刪除列表中的下標(biāo)為2的元素
# del list_1   直接刪除掉列表

# 列表的一些方法
# count
print(list_2.count('apple'))  # count是計算出現(xiàn)次數(shù)的方法
print("##########")

# index
print(list_2.index('banana'))    # 尋找banana在哪個位置
print("##########")
one_apple = list_2.index('apple')
print(one_apple)
list_3 = list_2[one_apple+1:]
two_apple = list_3.index('apple')
list_4_index = one_apple + two_apple +1
print(list_4_index)     # 輸出第二個位置
print("##########")

# reverse
list_1.reverse()  # 倒敘
print(list_1)
print("##########")

# sort
list_5 = [6,4,3,7,5,5,8,1]
list_5.sort()    # 排序(從小到大)
print(list_5)
print("##########")

# clear
list_5.clear()   # 清空列表
print(list_5)
print("##########")

三、元組

# 元 組
 tuple1 = (1,)    # 只有一個元素的話,后面加一個逗號,對之后的學(xué)習(xí)有所幫助
 tuple2 = (1,2,3,4,5)
 # tuple2[2] = 10  # 元組是不可修改的

四、字典

# 字 典

dictionary1 = {'country':'China','city':'beijing'}  # 創(chuàng)建字典(常用),前面為鍵,后面為鍵值
dictionary2 = dict((('city','shanghai'),))   # 創(chuàng)建字典
# 鍵是不可變類型:整型,字符串,元組
# 可變類型有:列表,字典
print(dictionary1)
print(dictionary1['city'])
print(dictionary2)
# 字典兩大特點:無序,鍵值唯一
print("##########")


dictionary2['city'] = 'tianjin'   # 修改字典,增加內(nèi)容
print(dictionary2)
print("##########")
return1 = dictionary2.setdefault('location','north')   # 增加內(nèi)容,如果有則不做修改
print(dictionary2)
print(return1)   # 返回鍵值
print("##########")
print(dictionary1.keys())    # 查看字典當(dāng)中用那些鍵
print(list(dictionary1.keys()))   # 用列表的形式展示
print(dictionary1.values())   # 只查看鍵值
print(dictionary1.items())   # 將字典當(dāng)中的所有鍵值對拿出
print("##########")

dictionary3 = {1:1,2:2}
dictionary4 = {1:3,4:5,6:7}
dictionary3.update(dictionary4)   # 更新字典,如果有鍵相同,則更新鍵值;如完全沒有,則更新在后方
print(dictionary3)
print(dictionary4)
print("##########")

# 刪除字典
eg = dictionary3.popitem()   # 不加鍵,則會隨機刪除鍵值對
print(eg,'||',dictionary3)
dictionary4.pop(6)   # 刪除鍵為6的信息
print(dictionary4)
del dictionary4[4]   # 刪除鍵為4的信息
print(dictionary4)
dictionary4.clear()   # 清空字典,只留下框架輪廓
print(dictionary4)
print("##########")

dictionary5 = dict.fromkeys(['a','b','c'],['z','y'])   # 分配率,將后面最為一個整體
print(dictionary5)

dictionary5['b'][0] = 'x'  # 需要理解深淺拷貝
print(dictionary5)
print("##########")
print(sorted(dictionary5))   # 字典的排序
print(sorted(dictionary5.values()))   # 根據(jù)值排序
print("##########")

# 字典的遍歷
dictionary6 = {'num1':10,'num2':52,'num3':33}
# 效率較高
for i in dictionary6:
  print(i,dictionary6[i])
print("##########")
# 效率很低
for a,b in dictionary6.items():
  print(a,b)
print("##########")

五、字符串

# 字符串
str1 = '1'
str2 = "2"   # Python中單引號與雙引號沒什么區(qū)別
print(str1)
print(str2)
print(str2*200)
str3 = 'asdqwezxc'
print(str3[2:])
print('as' in str3)   # 判斷此字段是否在字符串之中
print('aq' in str3)
print(str1+str2)    # 字符串拼接(效率很低)
eg1 = '......'.join([str1,str2])  # 通過單引號的符號連接將要拼接的兩個字符串
print(eg1)
print("##########")

# 字符串內(nèi)置方法
str4 = 'it is a bea\tutiful city 是 {name}\n'
print(str4.count('s'))   # 統(tǒng)計數(shù)目
print(str4.capitalize())    # 字符串首字母大寫
print(str4.center(100,'-'))  # 居中(距離和符號)
print(str4.endswith('ful'))   # 以某個內(nèi)容結(jié)尾
print(str4.startswith('it'))  # 以某個內(nèi)容開始
print(str4.expandtabs(tabsize=20))   # 對\t的空格數(shù)改為20,默認(rèn)為4
print(str4.find('b'))    # 查找到的第一個元素,返回下標(biāo)號
print("##########")
# 格式化輸出
print(str4.format(name = 'beijing'))  # 將name改為具體的名字
print(str4.format_map({'name':'shanghai'}))
print(str4.index('b'))   # 查找索引值,和fund()相似,但是index找不到會報錯
print(str4.isalnum())    # 不常用,是否是數(shù)字,字母,中文
print(str4.isdecimal())   # 不常用,是否是十進(jìn)制的數(shù)
print(str4.isdigit())    # 是否為數(shù)字
print(str4.isnumeric())   # 是否為數(shù)字
print(str4.isidentifier())   # 是否為非法變量
print("##########")
print(str4.islower())    # 是否都是小寫
print(str4.isupper())    # 是否都是大寫
print(str4.isspace())    # 是否是個空格
print(str4.istitle())    # 每一個詞的首字母是否是大寫
print(str4.lower())     # 大寫全部變小寫
print(str4.upper())     # 小寫全部變大寫
print(str4.swapcase())   # 字母大小寫反轉(zhuǎn)
print(str4.ljust(100,':'))     # 向左對齊
print(str4.rjust(100,':'))     # 向右對齊
print("##########")
print(str4.strip())     # 去掉換行符,空格
print(123)     # 前面的換行符被去掉
print("##########")
print(str4.replace('city','城市'))    # 替換內(nèi)容,也可以部分替換
print(str4.rfind('t'))   # 真實的索引位置
print(str4.split(' '))   # 字符串的分割
print(str4.rsplit('b',1))  # 以右為準(zhǔn),以目標(biāo)分割,分割一次

總結(jié)

以上就是本文關(guān)于Python3中的列表,元組,字典,字符串相關(guān)知識小結(jié)的全部內(nèi)容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站:淺談Python處理PDF的方法、python先序遍歷二叉樹問題、在Python web中實現(xiàn)驗證碼圖片代碼分享等,有什么問題可以隨時留言,小編會及時回復(fù)大家的。感謝朋友們對本站的支持!

相關(guān)文章

  • python3.x實現(xiàn)base64加密和解密

    python3.x實現(xiàn)base64加密和解密

    這篇文章主要為大家詳細(xì)介紹了python3.x實現(xiàn)base64加密和解密,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-03-03
  • python使用代理IP爬取貓眼電影專業(yè)評分?jǐn)?shù)據(jù)

    python使用代理IP爬取貓眼電影專業(yè)評分?jǐn)?shù)據(jù)

    在編寫爬蟲程序的過程中,IP封鎖無疑是一個常見且棘手的問題,盡管網(wǎng)絡(luò)上存在大量的免費IP代理網(wǎng)站,但其質(zhì)量往往參差不齊,令人堪憂,本篇文章中介紹一下如何使用Python的Requests庫和BeautifulSoup庫來抓取貓眼電影網(wǎng)站上的專業(yè)評分?jǐn)?shù)據(jù),需要的朋友可以參考下
    2024-03-03
  • python文件處理fileinput使用方法詳解

    python文件處理fileinput使用方法詳解

    這篇文章主要介紹了python文件處理fileinput使用方法詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-01-01
  • Python tensorflow與pytorch的浮點運算數(shù)如何計算

    Python tensorflow與pytorch的浮點運算數(shù)如何計算

    這篇文章主要介紹了Python tensorflow與pytorch的浮點運算數(shù)如何計算,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧
    2022-11-11
  • 高斯衰減python實現(xiàn)方式

    高斯衰減python實現(xiàn)方式

    這篇文章主要介紹了高斯衰減python實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • Python利用裝飾器click處理解析命令行參數(shù)

    Python利用裝飾器click處理解析命令行參數(shù)

    這篇文章主要為大家詳細(xì)介紹了Python如何利用裝飾器click實現(xiàn)處理解析命令行參數(shù)功能,文中的示例代碼簡潔易懂,需要的小伙伴快跟隨小編一起了解一下
    2022-10-10
  • python字符串,數(shù)值計算

    python字符串,數(shù)值計算

    這篇文章主要介紹了python字符串,數(shù)值計算的相關(guān)資料,需要的朋友可以參考下
    2016-10-10
  • 使用Python如何將視頻按照一定時間切割(比如:每10s進(jìn)行裁切)

    使用Python如何將視頻按照一定時間切割(比如:每10s進(jìn)行裁切)

    這篇文章主要介紹了使用Python將視頻按照一定時間切割(比如:每10s進(jìn)行裁切),本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-07-07
  • Django框架CBV裝飾器中間件auth模塊CSRF跨站請求問題

    Django框架CBV裝飾器中間件auth模塊CSRF跨站請求問題

    這篇文章主要介紹了Django CBV裝飾器 中間件 auth模塊 CSRF跨站請求,本文給大家介紹給CBV添加裝飾器有三種方法,三種方法都需要導(dǎo)入模塊,具體操作方法跟隨小編一起看看考下
    2021-08-08
  • python+pandas+時間、日期以及時間序列處理方法

    python+pandas+時間、日期以及時間序列處理方法

    今天小編就為大家分享一篇python+pandas+時間、日期以及時間序列處理方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-07-07

最新評論