Python函数:zip()
2021/7/19 11:05:30
本文主要是介绍Python函数:zip(),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
函数zip() ,将字符串转换为字典,可以像套公式一样,最里面是zip(),然后转换成其他类型的数据类型 zip() 函数用于将可迭代的对象作为参数,将对象中对应的元素打包成 一个个元组,然后返回由这些元组组成的列表,如果各个迭代器的元素个 数不一致,则返回列表长度与最短的对象相同。 ''' list1 = [1,2,3] list2 = [1,2,3] b = dict(zip(list1,list2)) # 将列表转换为字典 print(b) # 输出:{1: 1, 2: 2, 3: 3} str1 = '123456' str2 = '123456' a = dict(list(zip(str1,str2))) # 将字符串转换为字典 输出{'1': '1', '2': '2', '3': '3', '4': '4', '5': '5', '6': '6'} print(a) tuple1 = ('1','2','3') tuple2 = ('1','2','3') a = dict(list(zip(tuple1,tuple2))) # 将元组转换为字典 输出{'1': '1', '2': '2', '3': '3'} print(a)
tuple1 = ('1','2','3') a = list(zip(tuple1)) # 将元组转换为列表 输出[('1',), ('2',), ('3',)] print(a)
这篇关于Python函数:zip()的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-08有遇到过吗?同样的规则 Excel 中 比Python 结果大
- 2024-03-30开始python成长之路
- 2024-03-29python optparse
- 2024-03-29python map 函数
- 2024-03-20invalid format specifier python
- 2024-03-18pool.map python
- 2024-03-18threads in python
- 2024-03-14python Ai 应用开发基础训练,字符串,字典,文件
- 2024-03-13id3 algorithm python
- 2024-03-13sum array elements python