temp_list[1,2,3,4,5],利用for循环,依次输出list/ tuple/ dict/set构造的值
代码可以这么写:
temp_list = [1, 2, 3, 4, 5]
new_list = []
new_tuple = ()
new_dict = {}
new_set = set()
for x in temp_list:
new_list.append(x)
new_tuple+=(x,)
new_dict.update({f'{x}': x})
new_set.add(x)
print(new_list)
print(new_tuple)
print(new_dict)
print(new_set)
for i in temp_list:
print(i)
vlist=[1,2,3,4,5]
vtuple=(1,2,3,4,5)
vdict={'1': 1, '2': 2, '3': 3, '4': 4, '5': 5}
vset={1,2,3,4,5}
for x in vlist:
print('list:',x)
for x in vtuple:
print('tuple:',x)
for x in vdict:
print('dict:',x)
for x in vset:
print('set:',x)
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!