How do I create a master list made up of items from multiple lists in Python?

advertisements

What my code is supposed to do, is to run through a text file (which I've stored in a dict with linenum as the keys), and then search each line for a word from Lwordli. If the program finds the word, then loop through the different Lists stored in my MasterLi to identify if one or more other words in that sentence match up to what is in the lists. If it does, then do condition, if not, then move on to the elif condition and check another list, etc. Then do that for each sentence in txt.

I have multiple lists which i need to store inside 1 master for an if-else statement. I'm currently just trying something like this:

Li1 = []
Li2 = []

MasterLi = Li1, Li2

Lwordli = []

Then this is an example of how it would need to run inside my code with some of my actual code which is throwing back an error, if anyone can think of a better way than four For loops, that would be helpful, but main focus is how to call individual lists from Master:

for key, value in Txt.items()
    for cword in MasterLi: #Outer List loop
       for lword in lwordli:
           for section in value:

           pattern = r'\b' + re.escape(lword) + r'\b.*\b' + re.escape(cword) + r'\b|\b' + re.escape(
                    cword) + r'\b.*' + re.escape(lword) + r'\b'

           if re.search(pattern, section, re.I | re.M):

               if cword in Li1:

                   # Do condition

               elif cword in Li2:

                    # Do condition etc.

Traceback Error

    Traceback (most recent call last):
  File "C:/Users/Lewis Collins/PycharmProjects/Test/main.py", line 134, in <module>
    languagemodel()
  File "C:/Users/Lewis Collins/PycharmProjects/Test/main.py", line 94, in languagemodel
    cword) + r'\b.*' + re.escape(lword) + r'\b'
  File "C:\Users\Lewis Collins\AppData\Local\Programs\Python\Python35-32\lib\re.py", line 267, in escape
    return bytes(s)
TypeError: 'str' object cannot be interpreted as an integer

I believe this error is caused by how I'm trying to store lists inside a Master list to be called as my code works perfect if i attempt to do for loop as:

for word in Li1:


To create a master list that contains elements of all the lists, you can use the + operator, i.e.MasterLi = Li1 + Li2. You can also achieve this by using theextend method of lists. Your current approach is creating a tuple of lists, which does not seem to be what you need.