2019-12-01

#将元组(1,2,3) 和集合{"four",5,6}合成一个列表

def syn(x,y):

my_list = []

for zin x:

my_list.append(z)

for jin y :

my_list.append(j)

return my_list

print(syn((1,2,3),{"four",5,6}))

# 将列表[3,7,0,5,1,8]中大于5元素置为0,小于5的元素置为1

def divide(list):

my_list=[]

for xin list:

if x >5:

my_list.append("0")

else:

my_list.append("1")

return my_list

zj = [3,7,0,5,1,8]

print(divide(zj))

# 将列表["mo","deng","ge"]和[1,2,3] 转换成[("mo",1),("deng",2),("ge",3)]

list1 = ["mo","deng","ge"]

list2 = [1,2,3]

list3 = [{k: v}for k, vin zip(list1, list2)]

print(list3)

# 若a = dict(),令 b = a,执行b.update({"x":1}),a亦改变,为何,如何避免

dict = {'mo':1,'deng':2,'ge':3}

a = dict

print(a)

b = a

b.update({"x":1})

print(a)

# 以上证明 b = a 只是变量的赋值,为浅拷贝,拷贝的内存地址

# 避免

import copy

b=copy.deepcopy(a)

b.update({"z":1})

print(a)

print(b)

# 将二维结构[['a',1],['b',2]]和(('x',3),('y',4))转换成字典

list1 = [['a',1],['b',2],['c',3]]

tuple = (('x',3),('y',4))

def chang_type(list):

list1=[]

list2=[]

list3=[]

for iin range(len(list)):

for jin range(len(list[i])):

z = list[i][j]

list1.append(z)

for xin range(len(list1)):

if x %2 ==0:

f = list1[x]

list2.append(f)

else:

m = list1[x]

list3.append(m)

list4 = [{k: v}for k, vin zip(list2, list3)]

return list4

print(chang_type(tuple))

print(chang_type(list1))

你可能感兴趣的:(2019-12-01)