如何在Python中创建由多个列表元素组成的主列表?

时间:2016-12-19 10:52:44

标签: python list python-3.x

我的代码应该做的是运行一个文本文件(我将其存储在一个以linenum作为键的dict中),然后在每行中搜索一个单词{ {1}}。如果程序找到该单词,则循环浏览存储在Lwordli中的不同列表,以确定该句子中的一个或多个其他单词是否与列表中的单词匹配。如果是,则执行MasterLi,如果没有,则转到condition条件并检查其他列表等。然后对elif中的每个句子执行此操作。

我有多个列表,我需要在1个master中存储if-else语句。我现在正在尝试这样的事情:

txt

然后这是一个例子,说明如何在我的代码中运行我的一些实际代码,这会抛出一个错误,如果有人能想到比四个For循环更好的方法,那会有所帮助,但是主要关注的是如何从Master中调用单个列表:

Li1 = []
Li2 = []

MasterLi = Li1, Li2

Lwordli = []

追踪错误

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. 

我认为这个错误是由于我如何尝试将列表存储在Master列表中以便调用,因为我的代码在我尝试执行for循环时工作正常:

    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

2 个答案:

答案 0 :(得分:3)

要创建包含所有列表元素的主列表,您可以使用+运算符,即MasterLi = Li1 + Li2。您还可以使用列表的extend方法来实现此目的。 您当前的方法是创建一个列表元组,这似乎不是您需要的。

答案 1 :(得分:1)

实际上,您的错误消息指出调用re.escape()时出现问题,该问题可能由cwordlword触发。我建议你改写这部分:

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'

为:

try:
    lword_escaped = re.escape(lword)
except TypeError as e:
    print("re.escape failed with on lword '{}'".format(lword)
    raise
try:
    cword_escaped = re.escape(cword)
except TypeError as e:
    print("re.escape failed with on cword '{}'".format(cword)
    raise

pattern = (
    r'\b' + lword_escaped + r'\b.*\b' + cword_escaped 
    + r'\b|\b' + cword_escaped + r'\b.*' + lword_escaped + r'\b'
    )

这不会解决问题本身,但至少你会知道究竟是什么触发了错误。

另外,如果“主列表”是指两个列表的串联,则拼写为master_list = list1 + list2。您使用MasterLi = Li1, Li2获得的是tuple两个列表。

最后,如果你想要的只是测试给定的单词是否属于给定的列表,并根据单词来自哪个列表进行分支,那么有更好的选择。假设你的Li1Li2列表在循环期间没有改变,你最好使用迭代器取(,)对并产生(<identifier>, word`)元组,即:< / p>

def chain(*args):
    for identifier, lst in *args:
        for word in lst:
            yield identifier, word

然后将for cword in MasterLi替换为:

for identifier, cword in chain(("l1", Li1), ("l2", Li2)):
    # various stuff here
    if identifier == "l1":
        # some processing here
    elif identifier == "l2":
        # some different processing here

等。这将更多更多的时间和空间效率(不需要构建一个巨大的“主列表”,没有无用的O(n)查找来找出给定单词来自哪个列表)。

或者你可以在(<list>, <function>)对上的函数和链中封装“#some processing here”和“#some different processing here”,即:

def handleLi1(...):
    # some processing here

def handleLi2(...):
    # some different processing here


def chain(*args):
    for lst, func in *args:
        for word in lst:
            yield word, func


for cword, func in chain((Li1, handleLi1), (Li2, handleLi2)):
    # various stuff here
    whatever = func(cword, whatever, arg, needed)

你只需确保两个“处理程序”函数采用相同的args(如果必须返回某些内容,则返回相同类型的值)