0

how can I iterate over a list of lists like this:

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 
Alma
  • 97
  • 1
  • 12
  • Possible duplicate of [How to iterate through a list of lists in python?](http://stackoverflow.com/questions/9151104/how-to-iterate-through-a-list-of-lists-in-python) – Evans Murithi Nov 24 '16 at 13:03

2 Answers2

1

You cannot name a variable with just a single number.But here is a solution for your problem;Take this for example:

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)

output:

1
2
4
6
2
1
NN
VBG
JJR
Taufiq Rahman
  • 5,600
  • 2
  • 36
  • 44
  • actually what I have in mind is 3 groups of lists, and each list contains a couple of regular expressions.I want to iterate over these groups – Alma Nov 24 '16 at 13:22
  • What i see in your code is 3 lists so I demonstrated that but i think you should show where exactly your stuck at? Did this help you? – Taufiq Rahman Nov 24 '16 at 13:25
  • Thanks for your help.I tried to run what you have implemented but I was not lucky.I have 3 lists called pattern1,pattern2,pattern3 that each contain a coupe of patterns of POS tags like ['NN','VBG'].so for these 3 groups we have a lot of these patterns.I want to code to go through the text,and group all sentences that belong to each group of pattern1,2 and 3.How is that possible? – Alma Nov 24 '16 at 13:40
  • Seems to be another problem.Well after getting each text, you may group these texts into another list depending on the pattern and for the pattern you may have to use regex. – Taufiq Rahman Nov 24 '16 at 13:44
0

For a simple nested list, this will do:

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
user2390182
  • 72,016
  • 6
  • 67
  • 89