python zip函数_Python zip()函数

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 values

print('\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 = 0

print('\nPrinting the values of zip')
for values in test:
    print(values)  # print each tuples
    cnt+=1

print('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 values

testList = 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函数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值