慕妹3242003
>>> lis = [[1,2,3], [4,5,6], [7,8,9]][::-1] 反转列表:>>> rev = lis[::-1]>>> rev[[7, 8, 9], [4, 5, 6], [1, 2, 3]]现在我们zip在rev的所有项目上使用,并将每个返回的元组附加到旋转后的元素上:>>> rotated = []>>> for item in zip(rev[0],rev[1],rev[2]):... rotated.append(item)... >>> rotated[(7, 4, 1), (8, 5, 2), (9, 6, 3)]zip 从传递给它的每个iterable的相同索引中选择项目(它最多运行到具有最小长度的项目),并将它们作为元组返回。什么是*:*用于解压缩revto的所有项目zip,因此我们无需手动输入 rev[0], rev[1], rev[2]即可zip(*rev)。上面的zip循环也可以写成:>>> rev = [[7, 8, 9], [4, 5, 6], [1, 2, 3]]>>> min_length = min(len(x) for x in rev) # find the min length among all items>>> rotated = []for i in xrange(min_length): items = tuple(x[i] for x in rev) # collect items on the same index from each # list inside `rev` rotated.append(items)... >>> rotated[(7, 4, 1), (8, 5, 2), (9, 6, 3)]