标签:组合 使用方法 get abc split 结果 列表 ssi int
t1 = (1,2) #定义为元祖 t2 = 1,2 #将1和2封装成元祖 print(type(t1)) print(type(t2))
返回类型都为元组:
<class ‘tuple‘> <class ‘tuple‘>
使用方法:
a = 4 b = 5 temp = a a = b b = temp
# 等价于
a,b = b,a #等号右边使用了封装,左边使用了结构
使用示例,冒泡算法:
# 冒泡算法 list = [1,4,5,2,7,9,8,3,6] for i in range(len(list)): flag = False for j in range(len(list)-1-i): if list[j] > list[j+1]: list[j],list[j+1] = list[j+1],list[j] flag = True if not flag: break print(list)
示例:
list = [1,2] a,b = list print(a,b)
输出结果为:
1 2
结构方法示例:
a,b = 1,2 | 1 2 a,b = (1,2) | 输 1 2 a,b = [1,2] | 1 2 a,b = [10,20] | 出 10 20 a,b = {10,20} | 10 20 a,b = {‘a‘:10,‘b‘:20} | 结 a b a,b = {10,20,30} | #解构接收与封装数量不一致 错误 a,*b = {10,20,30} | 果 10 [20, 30] [a,b] = (1,2) | 1 2 [a,b] = 10,20 | 为 10 20 (a,b) ={30,40} | 30 40
示例1:
list = list(range(10)) a,*b,c = list print(a,b,c)
输出结果为:
0 [1, 2, 3, 4, 5, 6, 7, 8] 9
示例2:
list = list(range(10)) *list2 = list print(list2)
输出错误,不能单独使用*变量
*list2 = list ^ SyntaxError: starred assignment target must be in a list or tuple
示例3:
list = list(range(10)) *a,b = list print(a,b)
输出结果为:
[0, 1, 2, 3, 4, 5, 6, 7, 8] 9
示例4:
list = list(range(10)) a,*b = list print(a,b)
输出结果为:
0 [1, 2, 3, 4, 5, 6, 7, 8, 9]
示例5:
list = list(range(10)) a,*b,*c,d = list print(a,b,c,d)
输出结果错误,不能使用多个*变量
a,*b,*c,d = list ^ SyntaxError: two starred expressions in assignment
示例6:
a,*b,c = "abcdefg" print(b)
输出结果为:
[‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘f‘]
示例1:
list = [9,8,7,20] a,*b,c = list x,*_,z = list print(x,z)
输出结果为:
9 20
_是合法的标识符,看到下划线就知道这个变量就是不想被使用
示例2:
list = [2,1,3,5] _,*_,a = list print(_) print(a)
输出结果为:
[1, 3]
5
取出第二个,第四个,倒数第二个
list = list(range(10)) _,a,_,b,*_,c,_ = list print(a,b,c)
从list = [1,(2,3,4),5]中取4出来
list = [1,(2,3,4),5] _,(*_,a),_ = list print(a)
环境变量JAVA_HOME=/usr/bin,返回变量名和路径
x = ‘JAVA_HOME=/usr/bin‘ env,path = x.split(‘=‘,1) print(env) print(path)
以上输出结果为:
1 3 8
4
JAVA_HOME
/usr/bin
标签:组合 使用方法 get abc split 结果 列表 ssi int
原文地址:https://www.cnblogs.com/rivendare/p/9491675.html