python zip函数

Good day learners, hope that you are doing well. We discussed about Python Modulo in our previous tutorial. In this tutorial we will learn about Python zip function.

美好的一天学习者,希望您一切都好。 我们在上一教程中讨论了Python Modulo 。 在本教程中,我们将学习Python zip函数。

Python拉链 (Python zip)

If you are a regular computer user, you should have used .zip file extension. Do you know what it is? Basically, .zip is a container itself. It holds the real file inside.

如果您是普通计算机用户,则应该使用.zip文件扩展名。 你知道这是什么吗? 基本上, .zip本身就是一个容器。 它在其中保存真实文件。

Similarly, Python zip is a container that holds real data inside. Python zip function takes iterable elements as input, and returns iterator. If Python zip function gets no iterable elements, it returns an empty iterator.

同样,Python zip是一个在其中保存真实数据的容器。 Python zip函数将可迭代元素作为输入,并返回iterator 。 如果Python zip函数没有可迭代的元素,则它将返回一个空的迭代器。

Python zip函数示例 (Python zip function example)

Let’s look at a simple python zip function example. In previous section, we told about arguments taken by Python zip and what it returns. So by passing no argument, zip returns an empty iterator.

让我们看一个简单的python zip函数示例。 在上一节中,我们介绍了Python zip所采用的参数及其返回值。 因此,通过不传递任何参数,zip将返回一个空的迭代器。

However, if we pass two iterable object of same lengths, then an iterable of python tuples will be returned where each element of the tuple will be from those iterable lists.

但是,如果我们传递两个相同长度的可迭代对象,则将返回一个可迭代的python元组 ,其中该元组的每个元素都将来自这些可迭代列表 。

Python zip function is mainly used to combining data of two iterable elements together. See the following code.

Python zip函数主要用于将两个可迭代元素的数据组合在一起。 请参阅以下代码。

test = zip()# referring a zip class
print('The type of an empty zip : ', type(test))list1 = ['Alpha', 'Beta', 'Gamma', 'Sigma']
list2 = ['one', 'two', 'three', 'six']test = zip(list1, list2)  # zip the valuesprint('\nPrinting the values of zip')
for values in test:print(values)  # print each tuples

So the output of the above python zip example program will be:

因此,上述python zip示例程序的输出为:

The type of an empty zip :  <class 'zip'>Print the values of zip
('Alpha', 'one')
('Beta', 'two')
('Gamma', 'three')
('Sigma', 'six')

Notice the output of python type function call, so zip function returns instance of zip python class.

注意python类型函数调用的输出,因此zip函数返回zip python class的实例。

具有不同长度的可迭代元素的Python zip示例 (Python zip example with iterable elements of different lengths)

Now, what will happen if we try to zip two or more iterable elements? Well, In this case, the Python zip function will add items up to the lowest possible index of those given iterable elements.

现在,如果我们尝试压缩两个或多个可迭代元素,将会发生什么? 好吧,在这种情况下,Python zip函数将添加项,直到给定可迭代元素的项的最低索引为止。

That means the count of tuples will be same as the lowest length of the given iterable element. The following example will help you understand this.

这意味着元组的数量将与给定可迭代元素的最小长度相同。 以下示例将帮助您理解这一点。

# list of 4 elements
list1 = ['Alpha', 'Beta', 'Gamma', 'Sigma']
# list of 5 elements
list2 = ['one', 'two', 'three', 'six', 'five']
# list of 3 elments
list3 = [1, 2, 3]test = zip(list1, list2, list3)  # zip the values
cnt = 0print('\nPrinting the values of zip')
for values in test:print(values)  # print each tuplescnt+=1print('Zip file contains ', cnt, 'elements.');

So the output of this code will be

因此,此代码的输出将是

Printing the values of zip
('Alpha', 'one', 1)
('Beta', 'two', 2)
('Gamma', 'three', 3)
Zip file contains  3 elements.

Python解压缩zip (Python extract zip)

We can also extract data from the Python zip function. To extract zip, we have to use the same zip() function. But we have add an asterisk(*) in front of that list you get from zipped variable.

我们还可以从Python zip函数提取数据。 要提取zip,我们必须使用相同的zip()函数。 但是我们在从压缩变量获得的列表的前面添加了一个星号(*)。

You can use list() function to get a list from zipped variable. However, this will return a number of tuple. The number will differ according to the number of arguments that the zip function took to zip the data. See the following code to understand.

您可以使用list()函数从压缩变量中获取列表。 但是,这将返回多个元组。 该数字将根据zip函数压缩数据所使用的参数数量而有所不同。 请参阅以下代码以了解。

list1 = ['Alpha', 'Beta', 'Gamma', 'Sigma']
list2 = ['one', 'two', 'three', 'six']test = zip(list1, list2)  # zip the valuestestList = list(test)a, b = zip( *testList )
print('The first list was ', list(a));
print('The second list was ', list(b));

And the output will be

输出将是

Note that if the initial lists are of different length, then you won’t get the original list back. For example, if list2 = ['one', 'two', 'three'] in above program, then output will be like below.

请注意,如果初始列表的长度不同,那么您将不会获得原始列表。 例如,如果上述程序中的list2 = ['one', 'two', 'three'] ,则输出将如下所示。

The first list was  ['Alpha', 'Beta', 'Gamma']
The second list was  ['one', 'two', 'three']

That’s all for Python zip function. Hope that you have learnt well. For further query please use the comment box.

这就是Python zip函数的全部内容。 希望你学得很好。 如需进一步查询,请使用注释框。

Reference: Official Documentation

参考: 官方文档

翻译自: https://www.journaldev.com/15891/python-zip-function

python zip函数

python zip函数_Python zip()函数相关推荐

  1. python字典zip函数_Python zip函数及用法

    zip() 函数是 Python 内嵌函数之一,它能够将好几个序列(列表.元组.字典.结合.字符串数组及其 range() 区段组成的列表)"缩小"成一个 zip 目标.说白了&q ...

  2. python反序数函数_python range()函数取反序遍历sequence的方法

    python range()函数取反序遍历sequence的方法 python中的range函数取反序有两种方式 第一种:先构建一个列表,然后对列表中的元素进行反转. 例如: a=range(5) f ...

  3. python莫比乌斯环_python基础|函数

    1 函数 在python中的函数,内置函数有很多,如:int(), str(), list(), dict(), set() 等内置整形函数,bool()内置布尔值函数,len()内置长度计算函数 , ...

  4. python有哪些函数_python常用函数有哪些

    Python常用函数: 1. print()函数:打印字符串 2. raw_input()函数:从用户键盘捕获字符 3. len()函数:计算字符长度 4. format(12.3654,'6.2f' ...

  5. python 封装函数_python封装函数

    广告关闭 腾讯云11.11云上盛惠 ,精选热门产品助力上云,云服务器首年88元起,买的越多返的越多,最高返5000元! python函数教程函数本身其实就相当于一个集装箱,她负责把我们之前写的那些代码 ...

  6. python递归函数例题_python练习题----函数、内置函数、递归等

    1. 列举布尔值为False的值 { }.' '.0.().[ ].False.None2.根据范围获取其中3和7整除的所有数的和,并返回调用者:符合条件的数字个数以及符合条件数字的总和 #自答 fr ...

  7. 在python中使用关键字define定义函数_python自定义函数def的应用详解

    这里是三岁,来和大家唠唠自定义函数,这一个神奇的东西,带大家白话玩转自定义函数 自定义函数,编程里面的精髓! def 自定义函数的必要函数:def 使用方法:def 函数名(参数1,参数2,参数-): ...

  8. python神秘的魔法函数_Python魔法函数

    1.什么是魔法函数 魔法函数即Python类中以__(双下划线)开头,以__(双下划线)结尾的函数,Python提供的函数,可让咱们随意定义类的特性 示例: class Company(object) ...

  9. python del函数_python del函数是什么以及如何使用?

    这是关于Python里比较难得一个函数,甚至于章节不多,但是讲的内容却很多很多,大家对部分内容不知道有没有过了解--面向对象,而在这里主要用到的函数就是del,大家如果不知道的话,可以跟随小编一起来看 ...

最新文章

  1. Vue父组件网络请求回数据后再给子组件传值demo示例
  2. SQL语法之基础查询(进阶1)and条件查询(进阶2)
  3. python 数组合并排重_并排深度学习:Julia vs Python
  4. ARM平台交叉编译valgrind
  5. [USACO08JAN]牛大赛Cow Contest
  6. CANoe 13 demo 下载和激活
  7. 私藏的18个黑科技网站
  8. Zedgraph 总结
  9. [FPGA基础应用]基于CPLD+ARM架构模拟PC104总线时序
  10. 【日常积累】hr标签的属性及样式
  11. nodejs php 模板,玩转nodejs
  12. 吐槽弹幕网,解决映兔源无法播放的书签插件
  13. MAC显示屏的网页图片兼容方案
  14. Python bool布尔类型(解析)
  15. LAND网络渗透测试
  16. 百度超级链数字藏品 仿鲸探数字藏品平台开发
  17. 快商通肖龙源:从智能客服到智能营销,客服中心迎来价值升级节点
  18. 为什么会亏钱,亏钱的逻辑(一)
  19. 青桔单车 chameleon 跨平台实践分享
  20. QT QWebEngineView+UEditor富文本编辑器

热门文章

  1. 利用SQL语句查询数据库中具体某个字段的重复行
  2. Oracle 隔离级别
  3. Windows phone 8 学习笔记(1) 触控输入
  4. DevExpress lookupedit下拉列表不显示内容的问题
  5. ROM PROM EPROM EEPROM FLASH(NAND、NOR)
  6. 易学易用的Windows PowerShell(转)
  7. [转载] python 短网址_使用Python生成url短链接的方法
  8. [转载] 用Tkinter打造GUI开发工具(45)用Tkinter做自己的中文代码编辑器
  9. 分布式系统关注点(6)——「负载均衡」到底该如何实施?
  10. OO Summary (Homework 5-7)