(python函数03)zip()函数

zip是用来压缩的,它接受一系列可迭代的对象作为参数,将对象中对应的元素打包成一个个元组(tuple),然后返回有这些tuples组成的对象,可强制转化为列表和字典。若传入参数的长度不等,则返回list的长度和参数中长度最短的对象相同。

示例代码01

 list_str = ['a', 'b', 'c', 'd']
 list_num = [1, 2, 3, 4]
 list_new = zip(list_num, list_str)
 print("zip结果(列表):", list(list_new))

运行结果01

 zip结果(列表): [(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd')]

示例代码02

 list_str = ['a', 'b', 'c', 'd']
 list_num = [1, 2, 3, 4]
 list_new = zip(list_num, list_str)
 print("zip结果(字典):", dict(list_new))

运行结果02

 zip结果(字典): {1: 'a', 2: 'b', 3: 'c', 4: 'd'}

示例代码03

 list_str = ['a', 'b', 'c', 'd']
 list_num = [1, 2, 3, 4]
 list_new = zip(list_num, list_str)
 print("zip结果(列表):", list(list_new))
 print("zip结果(字典):", dict(list_new))

运行结果03

 zip结果(列表): [(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd')]
 zip结果(字典): {}

  • 这三个例子说明打包一次,只能强制转化一次列表或者字典

示例代码04

 str = 'abcd'
 str2 = '1234'
 list_new = zip(str, str2)
 print('zip结果(字典):', dict(list_new))

运行结果04

 zip结果(字典): {'a': '1', 'b': '2', 'c': '3', 'd': '4'}

示例代码05

list_str = ['a', 'b', 'c', 'd']
list_num = [1, 2, 3, 4]
x, y, z, p = zip(list_num, list_str)
print(x)
print(y)
print(z)
print(p)

运行结果

(1, 'a')
(2, 'b')
(3, 'c')
(4, 'd')

这个代码不知道怎么描述,记住吧哈。看别人代码的时候会遇到的!


  • 字符串也是迭代类型喔!

有了压缩,肯定少不了解压缩,在python中,对zip函数的解压缩就是*zip函数,解压缩并非是返回原来的两个列表,而是返回一个列表,之前的内容被定义为元组类型

示例代码06

a = [1, 2, 3]
b = ['a', 'b', 'c']
c = ['x', 'y', 'z']
d = zip(a, b, c)
e = zip(*d)
print('zip(a, b, c)----->', d)
print('zip(a, b, c)----->类型', type(d))
print('zip(*zip(a, b, c))----->', e)
print('zip(*zip(a, b, c))----->类型', type(e)) x, y, z = e
print(z)
print(y)
print(x)

运行结果06

zip(a, b, c)-----> <zip object at 0x00000206E95E0788>
zip(a, b, c)----->类型 <class 'zip'>
zip(*zip(a, b, c))-----> <zip object at 0x00000206E95E0648>
zip(*zip(a, b, c))----->类型 <class 'zip'>
('x', 'y', 'z')
('a', 'b', 'c')
(1, 2, 3)

哇塞!好神奇啊!这是语言,仿佛没有为什么!

 

(python函数03)zip()函数的更多相关文章

  1. python中使用zip函数出现<zip object at 0x02A9E418>

    在Python中使用zip函数,出现<zip object at 0x02A9E418>错误的原因是,你是用的是python2点多的版本,python3.0对python做了改动 zip方 ...

  2. 【Python】无须numpy,利用map函数与zip(*)函数对数组转置(转)

    http://blog.csdn.net/yongh701/article/details/50283689 在Python的numpy中,对类似array=[[1,2,3],[4,5,6],[7,8 ...

  3. python中的 zip函数详解

    python中zip()函数用法举例 定义:zip([iterable, ...]) zip()是Python的一个内建函数,它接受一系列可迭代的对象作为参数,将对象中对应的元素打包成一个个tuple ...

  4. 【Python基础】zip函数的使用

    zip函数的使用 描述 zip() 函数用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的列表. 如果各个迭代器的元素个数不一致,则返回列表长度与最短的对象相同, ...

  5. python中的zip()函数和map()函数

    一.zip()函数 1.语法: zip(iterable, ...) 参数说明: iterable,...-- 一个或多个迭代器; 在python2中: zip() 函数用于将可迭代的对象作为参数,将 ...

  6. python学习-32 zip函数

    zip 拉链方法 例如:1. ')))) 运行结果: [(')] Process finished with exit code 0 2. a = {'name':'abc','age':18,'ad ...

  7. python中的zip函数的使用

    >>> x = [, , ] >>> y = [, , ] >>> z = [, , ] >>> xyz = list(zip( ...

  8. Python中的zip()与*zip()函数详解

    前言 实验环境: Python 3.6: 示例代码地址:下载示例: 本文中元素是指列表.元组.字典等集合类数据类型中的下一级项目(可能是单个元素或嵌套列表). zip(*iterables)函数详解 ...

  9. python开发笔记之zip()函数用法详解

    今天分享一篇关于python下的zip()函数用法. zip()是Python的一个内建函数,它接受一系列可迭代的对象作为参数,将对象中对应的元素按顺序组合成一个tuple,每个tuple中包含的是原 ...

随机推荐

  1. 【C++】类

    一个简单例子: 1 //c++ 类 2 #include<iostream> 3 using namespace std; 4 class Point 5 { 6 private: 7 i ...

  2. 08:'my_tag' is not a registered tag library. Must be one of

    确保每次修改模板标签时都重新启动 Django 开发服务器(或确保它自己重新启动).如果服务器没有重新启动,Django 将不会注册标签. 从 django 1.9 开始,您可以在如下设置中加载这些新 ...

  3. Django基础之视图层

    内容概要 小白必会三板斧 request对象方法初识 form表单上传文件 Jsonresponse FBV与CBV 内容详细 1 小白必会三板斧 HttpResponse render redire ...

  4. centos 7 iotop 安装

    安装指令:yum -y install iotop 指定查看aubunt 用户的读写状态:iotop -u aubunt -P -k -t 允许在非交互模式下每隔3秒刷新一次,只刷新6次:iotop ...

  5. 为你的Go应用创建轻量级Docker镜像?

    缩小Go二进制文件大小 环境 youmen@youmendeMacBook-Pro % gcc -dumpversion 12.0.5 youmen@youmendeMacBook-Pro % go ...

  6. Linux 常用命令 随口说

    ls cd pwd du disk usage -h -s. 文件大小 df disk free -h 磁盘占用 fdisk mount top/htop + ps + grep + wc + pki ...

  7. js笔记12

    1.元素的属性 div.attributes是所有标签属性构成的数组集合 div.classList是所有class名构成的数组集合 在classList的原型链上可以看到add()和remove() ...

  8. ASP.Net Core Configuration 理解与源码分析

    Configuration 在ASP.NET Core开发过程中起着很重要的作用,这篇博客主要是理解configuration的来源,以及各种不同类型的configuration source是如何被 ...

  9. keycloak~自定义redirect_uri的方法

    在使用keycloak集成springboot的过程中,对于需要授权访问的接口,它会跳到keycloak里进行登录,之前有个redirect_uri,登录成功后会跳回本客户端,而这个地址默认没有修改的 ...

  10. 1.3.5、通过Method匹配

    server: port: 8080 spring: application: name: gateway cloud: gateway: routes: - id: guo-system4 uri: ...