简体   繁体   English

为什么zip会返回元组?

[英]Why does zip return tuples?

I'm wondering why zip() was designed to return tuples and not lists for instance. 我想知道为什么zip()被设计为返回元组而不是列表。 Is it something that has to do with performance or mutability? 它与性能或可变性有关吗? Or is it something else? 或者是别的什么?

In the fact is that the zip function is designed to : 事实上,zip功能旨在:

Return a list of tuples, where each tuple contains the i-th element from each of the argument sequences. 返回元组列表,其中每个元组包含来自每个参数序列的第i个元素。 (Source: the docstring of zip function) (来源:zip函数的文档字符串)

From this with given arguments you only get one possible result which can't be affected by something else than input that you give. 从这个给定的参数,你只得到一个可能的结果,不会受到你给出的输入以外的其他东西的影响。 So if the result was list you can modify it and the result will no more be the one expected. 因此,如果结果是列表,您可以修改它,结果将不再是预期的结果。 The tuples are the solution because they are immutable and you can't affect the result. 元组是解决方案,因为它们是不可变的,你不能影响结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM