定义一个函数,能够输入字典和元组。将字典的值(value) 和 元组的值交换,
交换结束后,打印并且返回 字典和元祖。
tu = (1,2,3)
di=dict(x=4,y=5,z=6)
def swop(*arg,**kwarg):
tu = list(arg) # 转为list
dic = kwarg
j = 0 # 元组索引
if len(tu) >= len(dic): # 判断字典与元组的长度
for i in dic: # 交换值
dic[i],tu[j] = tu[j],dic[i]
j+=1 # 索引+1
else:
u = 0
while u >len(tu):
for i in dic:
dic[i],tu[u]=tu[u],dic[i]
u+=1
tu = tuple(tu)
print(tu,dic)
网友评论