一篇文章带你学习Python3的高级特性(1)
作者:FUXI_Willard
1.切片
# 切片:取list或tuple的部分元素 nameList = ["Willard","ChenJD","ChenBao","ChenXiaoBao","hackerLuo"] print("nameList的内容为:",nameList) print("------------------------------------------------------------------------------") # 传统方法取nameList前三个元素 print("nameList前三个元素:",[nameList[0],nameList[1],nameList[2]]) print("------------------------------------------------------------------------------") # 使用切片取前三个元素,注:切片也是左闭右开 print("使用切片操作取nameList前三个元素:",nameList[0:3]) print("使用切片操作取nameList第二个到第四个元素:",nameList[1:5]) print("------------------------------------------------------------------------------") print("------------------------------------------------------------------------------") # 切片的一些常用操作 numsList = list(range(30)) print("数字列表内容为:\n",numsList) print("------------------------------------------------------------------------------") print("数字列表前10个数内容:",numsList[0:10]) print("数字列表前10个数内容:",numsList[:10]) print("\n") print("数字列表后10个数内容:",numsList[-10:]) print("数字列表前11-20个数内容:",numsList[10:20]) print("数字列表前10个数每隔两个取一个:",numsList[0:10:2]) print("数字列表前10个数每隔两个取一个:",numsList[:10:2]) print("数字列表每隔3个数取一个:",numsList[::3]) print("数字列表内容:\n",numsList[:]) # Tips: # 1.tuple可以使用切片操作,操作的结果仍是tuple; # (0,1,2,3,4,5)[:3] ===> (0,1,2) # 2.字符串使用切片操作,一个字符就是一个元素 # "Welcome to FUXI Technology."[:3] ===> "Wel"
# 结果输出:
nameList的内容为: ['Willard', 'ChenJD', 'ChenBao', 'ChenXiaoBao', 'hackerLuo']
------------------------------------------------------------------------------
nameList前三个元素: ['Willard', 'ChenJD', 'ChenBao']
------------------------------------------------------------------------------
使用切片操作取nameList前三个元素: ['Willard', 'ChenJD', 'ChenBao']
使用切片操作取nameList第二个到第四个元素: ['ChenJD', 'ChenBao', 'ChenXiaoBao', 'hackerLuo']
------------------------------------------------------------------------------
------------------------------------------------------------------------------
数字列表内容为:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29]
------------------------------------------------------------------------------
数字列表前10个数内容: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
数字列表前10个数内容: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
数字列表后10个数内容: [20, 21, 22, 23, 24, 25, 26, 27, 28, 29]
数字列表前11-20个数内容: [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
数字列表前10个数每隔两个取一个: [0, 2, 4, 6, 8]
数字列表前10个数每隔两个取一个: [0, 2, 4, 6, 8]
数字列表每隔3个数取一个: [0, 3, 6, 9, 12, 15, 18, 21, 24, 27]
数字列表内容:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29]
2.迭代
# 给定一个list或tuple,通过for循环来遍历这个list或tuple,这种遍历称为迭代(iteration);# 迭代通过for...in来完成;# 实例1:迭代dictpersonInfoDict = {"name":"Willard","age":18,"job":"Engineer"}print("使用迭代读取dict内容!")print("personInfoDict的内容:",personInfoDict)print("------------------------------------------------------------------------------")# 读取personInfoDict的key值print("personInfoDict的key值:")for key in personInfoDict: print(key,end = " ")print("\n")print("personInfoDict的value值:")for value in personInfoDict.values(): print(value,end = " ") print("\n")print("personInfoDict的key-value值:")for item in personInfoDict.items(): print(item,end = " ")print("\n")print("------------------------------------------------------------------------------")print("------------------------------------------------------------------------------")# 实例2:迭代读取字符串userNameStr = "WillardLuo"print("使用迭代读取字符串内容!")print("userNameStr的内容:",userNameStr)print("------------------------------------------------------------------------------")print("userNameStr的每个元素为:")for word in userNameStr: print(word,end = " ")# 给定一个list或tuple,通过for循环来遍历这个list或tuple,这种遍历称为迭代(iteration); # 迭代通过for...in来完成; # 实例1:迭代dict personInfoDict = {"name":"Willard","age":18,"job":"Engineer"} print("使用迭代读取dict内容!") print("personInfoDict的内容:",personInfoDict) print("------------------------------------------------------------------------------") # 读取personInfoDict的key值 print("personInfoDict的key值:") for key in personInfoDict: print(key,end = " ") print("\n") print("personInfoDict的value值:") for value in personInfoDict.values(): print(value,end = " ") print("\n") print("personInfoDict的key-value值:") for item in personInfoDict.items(): print(item,end = " ") print("\n") print("------------------------------------------------------------------------------") print("------------------------------------------------------------------------------") # 实例2:迭代读取字符串 userNameStr = "WillardLuo" print("使用迭代读取字符串内容!") print("userNameStr的内容:",userNameStr) print("------------------------------------------------------------------------------") print("userNameStr的每个元素为:") for word in userNameStr: print(word,end = " ")
# 结果输出:
使用迭代读取dict内容!
personInfoDict的内容: {'name': 'Willard', 'age': 18, 'job': 'Engineer'}
------------------------------------------------------------------------------
personInfoDict的key值:
name age jobpersonInfoDict的value值:
Willard 18 EngineerpersonInfoDict的key-value值:
('name', 'Willard') ('age', 18) ('job', 'Engineer')------------------------------------------------------------------------------
------------------------------------------------------------------------------
使用迭代读取字符串内容!
userNameStr的内容: WillardLuo
------------------------------------------------------------------------------
userNameStr的每个元素为:
W i l l a r d L u o
# Tips: # 1.使用for...in循环需要作用于一个可迭代对象; # 2.判断一个对象是否是可迭代对象,可通过collections.abs模块的Iterable类型判断 from collections.abc import Iterable nameStr = "WillardLuo" nameList = ["Willard","ChenJD","ChenBao"] numberTuple = (1,2,3,4,5) personInfoDict = {"name":"Willard","age":18} number = 100 print("判断字符串是否是可迭代对象!") print(isinstance(nameStr,Iterable)) print("判断列表是否是可迭代对象!") print(isinstance(nameList,Iterable)) print("判断元组是否是可迭代对象!") print(isinstance(numberTuple,Iterable)) print("判断字典是否是可迭代对象!") print(isinstance(personInfoDict,Iterable)) print("判断数字是否是可迭代对象!") print(isinstance(number,Iterable)) print("------------------------------------------------------------------------------") print("------------------------------------------------------------------------------") # 实现对list进行下标循环 # 使用enumerate函数把一个list变成"索引-元素"对 nameList = ["Willard","ChenJD","ChenBao","ChenXiaoBao","hackerLuo"] for index,value in enumerate(nameList): print(index,value) print("------------------------------------------------------------------------------") willardInfo = {"name":"Willard","age":18,"job":"Engineer"} # 循环中同时引用两个变量,数据处理中常用 for key,value in willardInfo.items(): print(key,value)
# 结果输出:
判断字符串是否是可迭代对象!
True
判断列表是否是可迭代对象!
True
判断元组是否是可迭代对象!
True
判断字典是否是可迭代对象!
True
判断数字是否是可迭代对象!
False
------------------------------------------------------------------------------
------------------------------------------------------------------------------
0 Willard
1 ChenJD
2 ChenBao
3 ChenXiaoBao
4 hackerLuo
------------------------------------------------------------------------------
name Willard
age 18
job Engineer
3.列表生成式
# 列表生成式:List Comprehensions,用来创建list的生成式 # 实例1:生成[1×1,2×2,3×3,...,10×10]的列表 # 法一:循环 numsList = [] for num in range(1,11): numsList.append(num * num) print("numsList的内容:",numsList) print("---------------------------------------------------------") # 法二:列表生成式 print("列表生成式生成上述列表!") # 把要生成的元素放在前面,后面加循环 print([num * num for num in range(1,11)]) print("---------------------------------------------------------") # 在循环后添加if判断,筛选1-10中偶数的平方 print("列表生成式生成1-10偶数的平方的列表") print([num * num for num in range(1,11) if num % 2 == 0]) print("---------------------------------------------------------") # 双重循环 print("列表生成式应用于双重循环") print([m + n for m in "Willard" for n in "ChenJD"])
# 结果输出:
numsList的内容: [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
---------------------------------------------------------
列表生成式生成上述列表!
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
---------------------------------------------------------
列表生成式生成1-10偶数的平方的列表
[4, 16, 36, 64, 100]
---------------------------------------------------------
列表生成式应用于双重循环
['WC', 'Wh', 'We', 'Wn', 'WJ', 'WD', 'iC', 'ih', 'ie', 'in', 'iJ', 'iD', 'lC', 'lh', 'le', 'ln', 'lJ', 'lD', 'lC', 'lh', 'le', 'ln', 'lJ', 'lD', 'aC', 'ah', 'ae', 'an', 'aJ', 'aD', 'rC', 'rh', 're', 'rn', 'rJ', 'rD', 'dC', 'dh', 'de', 'dn', 'dJ', 'dD']
总结
本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注脚本之家的更多内容!