What is the purpose of a zip function (as in Python or C# 4.0)?

后端 未结 7 2194
北荒
北荒 2020-12-29 09:27

Someone asked How to do Python’s zip in C#?...

...which leads me to ask, what good is zip? In what scenarios do I need this? Is it really so foundational that I n

相关标签:
7条回答
  • 2020-12-29 09:57

    zip is useful if you'd like to iterate over multiple iterables simultaneously, which is a reasonably common scenario in Python.

    One real-world scenario where zip has come in handy for me is if you have an M by N array, and you want to look at columns instead of rows. For example:

    >>> five_by_two = ((0, 1), (1, 2), (2, 3), (3, 4), (4, 5))
    >>> two_by_five = tuple(zip(*five_by_two))
    >>> two_by_five
    ((0, 1, 2, 3, 4), (1, 2, 3, 4, 5))
    
    0 讨论(0)
提交回复
热议问题