如何从列表中打印3套?

我在下面列出

l = ["a1", "MYSQL1","emp", "b1", "MYSQL2","dep"]
for a, b,c in zip(l[::2], l[1::2], l[2::3]):
    print(a, b, c)

我正确地获得了第一线,但是休息却出错了

我的预期了

a1 MYSQL1 emp
b1 MYSQL2 dep
评论
  • znon
    znon 回复

    这有效:

    >>> a = ["a1", "MYSQL1","emp", "b1", "MYSQL2","dep"]
    >>> for vals in zip(*[iter(a)]*3) :
    ...     print vals
    ... 
    ('a1', 'MYSQL1', 'emp')
    ('b1', 'MYSQL2', 'dep')
    >>> 
    
  • 喂,要爱我
    喂,要爱我 回复

    Change ::2 to ::3:

    l = ["a1", "MYSQL","emp", "b1", "MYSQL","dep"]
    for a, b,c in zip(l[::3], l[1::3], l[2::3]):
        print(a, b, c)
    

    输出:

    a1 MYSQL emp
    b1 MYSQL dep