sub如何执行?

时间:2019-05-31 06:43:32

标签: python

sub如何打印24,因为i将得到['24'],而sub应该只运行一次?而且我完全困惑sub如何显示整数24,因为inner for循环只会运行一次。

l1=[['24']]
for i in l1:
    print(i)
    for sub in i:
        print(sub)

输出为:

['24']
24

1 个答案:

答案 0 :(得分:0)

如果您不了解自己的输出,请向其添加更多打印(和/或数据),直到执行以下操作:

l1=[['24',"Y"],"X"]
for i in l1:                   # iterates all elements of the outer list
    print("i is:",i, type(i))
    for sub in i:              # iterates all elements of the inner list
        print("sub is:",sub, type(sub))
        for s in sub:          # iterates the single characters of your string
            print("s is:",s,type(s))

输出(格式化):

i is: ['24', 'Y'] <class 'list'>
    sub is: 24 <class 'str'>
        s is: 2 <class 'str'>
        s is: 4 <class 'str'>
    sub is: Y <class 'str'>
        s is: Y <class 'str'>
i is: X <class 'str'>
    sub is: X <class 'str'>
        s is: X <class 'str'>

请参见How to debug small programs