[Python] zip()
zip()
zip(*iterables, strict=False)
zip() returns an iterator of tuples,
where the i-th tuple contains the i-th element from each of the argument iterables.
Like:
zip([1, 2, 3], ['sugar', 'spice', 'everything nice']) """ (1, 'sugar') (2, 'spice') (3, 'everything nice') """
Without the strict=True
argument,
any bug that results in iterables of different lengths will be silenced,
possibly manifesting as a hard-to-find bug in another part of the program.
範例
a = [1, 2, 3] b = [4, 5, 6] c = [7, 8, 9, 0] zipped = zip(a, b, c) ## zipped = [(1, 4, 7), (2, 5, 8), (7, 8, 9)] ## d, e, f = (zip(*zipped)) ## zip(*zipped) = [(1, 2, 3), (4, 5, 6), (7, 8, 9)] d = (1, 2, 3) e = (4, 5, 6) f = (7, 8, 9) ##
Last Updated on 2024/06/16 by A1go