
编程开发
切片copy
当我们需要在Go语言中复制一个切片时,我们可以使用内置的copy函数。copy函数可以将一个切片的元素复制到另一个切片中。下面是一个示例: 在上面的示例中,我们首先创建了一个源切片
#!/usr/bin/python3
#coding:utf-8
#吴老二个人博客~~~www.wulaoer.or
wulaoer = ['python','ruby','golong']
wulaoers = str(wulaoer) #列表转字符串
print("转换之后的数据类型",type(wulaoers))
print("打印一下字符串",wulaoers)
输出结果:
转换之后的数据类型 <class 'str'> #字符串类型 打印一下字符串 ['python', 'ruby', 'golong']把列表转换成字符串了,不过打印的时候字符串的格式还是列表,只是类型变成字符串,如果我们想把列表转成字符串,格式也转成字符串又该如何操作呢?看下面的例子:
#!/usr/bin/python3
#coding:utf-8
#吴老二个人博客~~~www.wulaoer.or
wulaoer = ['python','ruby','golong']
wulaoers=''.join(wulaoer)
print("转换之后的数据类型",type(wulaoers))
print("打印一下字符串",wulaoers)
输出结果:
转换之后的数据类型 <class 'str'> 打印一下字符串 pythonrubygolong这里使用到了join()函数,这里先不说join()函数的用法,后面会针对join()函数单独说明,目前只需要记录这个方法即可。在工作中,第二种方法较为常用,比较容易分辨。
#!/usr/bin/python3
#coding:utf-8
#吴老二个人博客~~~www.wulaoer.or
wulaoer = "www.wulaoer.org is a platform for learning Golong, Python, Java and Ruby"
print(wulaoer,type(wulaoer)) #打印字符串和变量类型
wulaoer_list = wulaoer.split(',')
print(wulaoer_list,type(wulaoer_list)) #打印使用split()函数转成的列表
输出结果:
www.wulaoer.org is a platform for learning Golong, Python, Java and Ruby <class 'str'> ['www.wulaoer.org is a platform for learning Golong', ' Python', ' Java and Ruby'] <class 'list'>使用eval转换
#!/usr/bin/python3 #coding:utf-8 #吴老二个人博客~~~www.wulaoer.or wulaoer = "['python3','ruby','java','DevOps']" wulaoer_list=eval(wulaoer) print(wulaoer,type(wulaoer_list))输出结果:
['python3','ruby','java','DevOps'] <class 'list'>使用list()函数,将字符直接转列表
#!/usr/bin/python3 #coding:utf-8 #吴老二个人博客~~~www.wulaoer.or wulaoer="python3" wulaoer_list = list(wulaoer) print(wulaoer_list,type(wulaoer_list))输出结果:
['p', 'y', 't', 'h', 'o', 'n', '3'] <class 'list'>
#!/usr/bin/python3 #coding:utf-8 #吴老二个人博客~~~www.wulaoer.or wulaoer = ['python3','ruby','java','DevOps'] wolf = ['运维开发','运维开发','服务开发','运维工具'] wulaoer_dict = dict(zip(wulaoer,wolf)) print(wulaoer_dict,type(wulaoer_dict))输出结果:
{'python3': '运维开发', 'ruby': '运维开发', 'java': '服务开发', 'DevOps': '运维工具'} <class 'dict'>
如果我们把wulaoer这个列表中的元素改成有重复的元素,看一下打印结果:
#!/usr/bin/python3 #coding:utf-8 #吴老二个人博客~~~www.wulaoer.or wulaoer = ['python3','ruby','python3','DevOps'] wolf = ['运维开发','运维开发','服务开发','运维工具'] wulaoer_dict = dict(zip(wulaoer,wolf)) print(wulaoer_dict,type(wulaoer_dict))输出结果:
{'python3': '服务开发', 'ruby': '运维开发', 'DevOps': '运维工具'} <class 'dict'>
根据打印结果发现第二个'python3'对应的'服务开发'元素跳过去了,而且作为key的第二个元素'python3'也没有打印出来,执行结果也没有报错。
第二种方法使用for循环
虽然还没有说过for循环,不过这里在先简单提示一下,这里就是把列表中的元素一个一个的重新赋值给空的字典直到结束为止。先看一下代码:
#!/usr/bin/python3
#coding:utf-8
#吴老二个人博客~~~www.wulaoer.or
wulaoer = ['python3','ruby','python3','DevOps']
wolf = ['运维开发','运维开发','服务开发','运维工具']
wulaoer_dict = {}
for i in range(len(wulaoer)):
wulaoer_dict[wulaoer[i]]=wolf[i]
print(wulaoer_dict,type(wulaoer_dict))
输出结果:
{'python3': '服务开发', 'ruby': '运维开发', 'DevOps': '运维工具'} <class 'dict'>
这里输出的结果和上面的一样,也是把重复的key元素跳过。
将嵌套列表转字典
#!/usr/bin/python3 #coding:utf-8 #吴老二个人博客~~~www.wulaoer.or wulaoer = [['golong','DevOps'],['java','ruby']] wulaoer_dict = dict(wulaoer) print(wulaoer_dict,type(wulaoer_dict))输出结果:
{'golong': 'DevOps', 'java': 'ruby'} <class 'dict'>
#!/usr/bin/python3
#coding:utf-8
#吴老二个人博客~~~www.wulaoer.or
wulaoer={'python3': '服务开发', 'ruby': '运维开发', 'DevOps': '运维工具'}
wulaoer_list = list(wulaoer.keys()) + list(wulaoer.values())
print(wulaoer_list,type(wulaoer_list))
输出结果:
['python3', 'ruby', 'DevOps', '服务开发', '运维开发', '运维工具'] <class 'list'>这里先把字典的keys和value都转成列表之后,合并两个列表。还可以把字典作为一个元素加到列表中,列表嵌套字典的方式,这里就不举例了。
#!/usr/bin/python3 #coding:utf-8 #吴老二个人博客~~~www.wulaoer.or wulaoer = ['python3','ruby','python3','DevOps'] print(wulaoer,type(wulaoer)) wulaoer_tuple = tuple(wulaoer) print(wulaoer_tuple,type(wulaoer_tuple))输出结果:
['python3', 'ruby', 'python3', 'DevOps'] <class 'list'>
('python3', 'ruby', 'python3', 'DevOps') <class 'tuple'>
元组转列表一样,可以使用内置函数list()
#!/usr/bin/python3
#coding:utf-8
#吴老二个人博客~~~www.wulaoer.or
wulaoer = ('python3','ruby','python3','DevOps')
print(wulaoer,type(wulaoer))
wulaoer_list = list(wulaoer)
print(wulaoer_list,type(wulaoer_list))
输出结果:
('python3', 'ruby', 'python3', 'DevOps') <class 'tuple'>
['python3', 'ruby', 'python3', 'DevOps'] <class 'list'>
#!/usr/bin/python3 #coding:utf-8 #吴老二个人博客~~~www.wulaoer.or wulaoer = ['python3','ruby','python3','DevOps'] print(wulaoer,type(wulaoer)) wulaoer_set = set(wulaoer) print(wulaoer_set,type(wulaoer_set))输出结果:
['python3', 'ruby', 'python3', 'DevOps'] <class 'list'>
{'ruby', 'python3', 'DevOps'} <class 'set'>
#!/usr/bin/python3
#coding:utf-8
#吴老二个人博客~~~www.wulaoer.or
wulaoer = {'python3','ruby','java','DevOps'}
print(wulaoer,type(wulaoer))
wulaoer_list = list(wulaoer)
print(wulaoer_list,type(wulaoer_list))
输出结果:
{'ruby', 'java', 'DevOps', 'python3'} <class 'set'>
['ruby', 'java', 'DevOps', 'python3'] <class 'list'>
以上就是列表转换成其它数据类型的方法,字符串转字典或者元组以及集合的方法看下一章吧。
评论 0