如何遍历列表以打印列表中的每个变量?

时间:2014-07-21 16:45:54

标签: python list printing iteration

如果我将此作为我的代码:

import sys
from sys import argv
if len(sys.argv) == 7:
    a,b,c,d,e,f,g = argv
    c_list = c.split(',')
    d_list = d.split(',')
    if len(c_list) == len(d_list):
        print b
    elif len(d_list) == len(c_list):
        print b
    else:
        sys.exit()
    for c in c_list:
        if len(c_list) == len(d_list):
            print c
        else:
            sys.exit()
    for d in d_list:
        if len(d_list) == len(c_list):
            print d
        else:
            sys.exit()
    print e
    print f
    print g

else:
    a,b,c,d,e,f = argv
    c_list = c.split(',')
    d_list = d.split(',')
    if len(c_list) == len(d_list):
        print b
    elif len(d_list) == len(c_list):
        print b
    else:
        sys.exit() 
    for c in c_list:
        if len(c_list) == len(d_list):
            print c
        else:
            sys.exit()
    for d in d_list:
        if len(d_list) == len(c_list):
            print d
        else:
            sys.exit()
    print e
    print f

它允许我将以下内容传递给argv:

a b c,c1,c2 d,d1,d2 e f g

和我的其他代码导致此列表打印为

    a 
    b 
    c
    c1
    c2
    d
    d1
    d2
    e
    f
    g

但是,我希望它打印以下内容:

    a
    b
    c
    d
    e 
    f
    g
    a
    b
    c1
    d1
    e
    f
    g

依此类推。我该怎么做?如果这看起来超级基本,我道歉。作为一名高中新生,我仍在努力弄清楚复杂的编码世界是如何运作的。

1 个答案:

答案 0 :(得分:0)

根据你的描述,我认为你想要这样的东西:

>>> a = 1
>>> b = 2
>>> c = [3, 4, 5]
>>> d = [6, 7, 8]
>>> for cx, dx in zip(c, d):
        for item in (a, b, cx, dx):
            print(item)
        print("---")


1
2
3
6
---
1
2
4
7
---
1
2
5
8
---