定义列表列表并迭代它

时间:2016-11-24 13:01:07

标签: python list

如何迭代这样的列表列表:

kinds = [[1],[2],[3]]
1 = [[x],[y],[z]]
2 = [[k],[l],[m]]
3 = [[z],[y],[w]]
for i in kinds:
    for j in in each element of kinds:
        #do this 

2 个答案:

答案 0 :(得分:1)

您不能仅使用一个数字来命名变量。但这里是您的问题的解决方案;例如:

var1 = [1,2,4]
var2 = [6,2,1]
var3 = ['NN','VBG','JJR']
kinds = [var1,var2,var3]
for list in kinds:
    for items in list:
         print (items)

输出:

1
2
4
6
2
1
NN
VBG
JJR

答案 1 :(得分:0)

对于一个简单的嵌套列表,这将执行:

kinds = [[1],[2],[3]]
for i in kinds:  # i is a list, so iterate it!
    for j in i:  # assumes kinds is list of lists (iterable of iterables)
        # do stuff