Python解压迭代

Python2:

items = [1, 2, 3, 4]
a, b, c, d = items # Unpack items into variables

Python3:

a, *rest = items # a = 1, rest = [2,3,4]
a, *rest, d = items # a = 1, rest = [2,3], d = 4
*rest, d = items # rest = [1,2,3], d = 4

相关链接:

  • PEP 3132 - Extended Iterable Unpacking

你可能感兴趣的:(Python解压迭代)