简体   繁体   English

Python:使用列表索引作为 function 参数

[英]Python: use a list index as a function argument

I'm trying to use list indices as arguments for a function that performs regex searches and substitutions over some text files.我正在尝试将列表索引用作 arguments 用于对某些文本文件执行正则表达式搜索和替换的 function。 The different search patterns have been assigned to variables and I've put the variables in a list that I want to feed the function as it loops through a given text.不同的搜索模式已分配给变量,并且我已将变量放在一个列表中,我想在 function 循环遍历给定文本时提供该列表。

When I call the function using a list index as an argument nothing happens (the program runs, but no substitutions are made in my text files), however, I know the rest of the code is working because if I call the function with any of the search variables individually it behaves as expected.当我使用列表索引作为参数调用 function 时,没有任何反应(程序运行,但在我的文本文件中没有进行替换),但是,我知道代码的 rest 正在工作,因为如果我用任何 C 调用 ZC1C4145068E68A94F搜索变量单独它的行为符合预期。

When I give the print function the same list index as I'm trying to use to call my function it prints exactly what I'm trying to give as my function argument, so I'm stumped!当我为打印 function 提供与我试图用来调用我的 function 相同的列表索引时,它会打印出我想要作为我的 ZC1C425268E68385D1AB5074C17A9 参数给出的内容,所以


search1 = re.compile(r'pattern1')
search2 = re.compile(r'pattern2')
search3 = re.compile(r'pattern3')

searches = ['search1', 'search2', 'search2']
i = 0

for …
  …
  def fun(find)
    …

  fun(searches[i])
  if i <= 2:
    i += 1  
…

As mentioned, if I use fun(search1) the script edits my text files as wished.如前所述,如果我使用fun(search1)脚本会根据需要编辑我的文本文件。 Likewise, if I add the line print(searches[i]) it prints search1 (etc.), which is what I'm trying to give as an argument to fun .同样,如果我添加行print(searches[i])它会打印search1 (等),这就是我试图作为fun的参数。

Being new to Python and programming, I've a limited investigative skill set, but after poking around as best I could and subsequently running print(searches.index(search1) and getting a pattern1 is not in list error, my leading (and only) theory is that I'm giving my function the actual regex expression rather than the variable it's stored in???作为 Python 和编程的新手,我的调查技能有限,但在尽我所能四处探索并随后运行print(searches.index(search1)并获得pattern1 is not in list错误之后,我的领先(并且只有) 理论是我给我的 function 实际的正则表达式而不是它存储的变量???

Much thanks for any forthcoming help!非常感谢任何即将到来的帮助!

Try to changes your searches list to be [search1, search2, search3] instead of ['search1', 'search2', 'search2'] (in which you just use strings and not regex objects)尝试将searches列表更改为[search1, search2, search3]而不是['search1', 'search2', 'search2'] (您只使用字符串而不是正则表达式对象)

Thanks to all for the help.感谢大家的帮助。 eyl327's comment that I should use a list or dictionary to store my regular expressions pointed me in the right direction. eyl327 关于我应该使用列表或字典来存储我的正则表达式的评论为我指明了正确的方向。

However, because I was using regex in my search patterns, I couldn't get it to work until I also created a list of compiled expressions (discovered via this thread on stored regex strings ).但是,因为我在搜索模式中使用了正则表达式,所以直到我还创建了一个已编译表达式的列表(通过这个线程在存储的正则表达式字符串上发现)之前,我才能让它工作。

Very appreciative of juanpa.arrivillaga point that I should have proved a MRE (please forgive, with a highly limited skill set, this in itself can be hard to do), I'll just give an excerpt of a slightly amended version of my actual code demonstrating the answer (one again, please forgive its long-windedness, I'm not presently able to do anything more elegant):非常感谢 juanpa.arrivillaga 点,我应该证明一个 MRE(请原谅,技能非常有限,这本身就很难做到),我只摘录我的实际版本的稍微修改的版本演示答案的代码(再次,请原谅它的啰嗦,我目前无法做任何更优雅的事情):


…

# put regex search patterns in a list
rawExps = ['search pattern 1', 'search pattern 2', 'search pattern 3']
# create a new list of compiled search patterns 
compiledExps = [regex.compile(expression, regex.V1) for expression in rawExps]

i = 0
storID = 0
newText = ""

for file in filepathList:
    for expression in compiledExps:
        with open(file, 'r') as text:
            thisText = text.read()
            lines = thisThis.splitlines()
            setStorID = regex.search(compiledExps[i], thisText)
            if setStorID is not None:
                storID = int(setStorID.group())
            for line in lines:
                def idSub(find):
                    global storID
                    global newText
                    match = regex.search(find, line)
                    if match is not None:
                        newLine = regex.sub(find, str(storID), line) + "\n"
                        newText = newText + newLine
                        storID = plus1(int(storID), 1)
                    else:
                        newLine = line + "\n"
                        newText = newText + newLine
                # list index number can be used as an argument in the function call
                idSub(compiledExps[i])
            if i <= 2:
                i += 1
        write()
        newText = ""
    i = 0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM