简体   繁体   English

Python中的正则表达式替换列表中的项目

[英]Regular Expression in Python to Replace Items in a List

This is for an exercise we are doing in uni.这是我们在大学里做的一个练习。 I am trying to get all k1-9 and p1-9 strings in the txt file and change them so that each k(n) = 1*n and each p(n) = 0*n (ie p5= 00000, k3= 111, p2= 00).我试图在 txt 文件中获取所有 k1-9 和 p1-9 字符串并更改它们,以便每个 k(n) = 1*n 和每个 p(n) = 0*n(即 p5= 00000,k3= 111,p2 = 00)。 I have managed to gather the k1-9 and p1-9 in a list called codes but I dont know how to proceed.我已设法将 k1-9 和 p1-9 收集到名为代码的列表中,但我不知道如何进行。

import re

with open("suspicious_knitting.txt") as file:
    string = file.read()
    codes = re.findall("k[1-9]|p[1-9]" ,string)

Printing codes is like this.:打印代码是这样的:

['k1', 'p1', 'k1', 'p1', 'k1', 'p2', 'k1', 'p2', 'k1', 'p3', 'k1', 'p3', 'k1', 'p1', 'k2', 'p1', 'k2', 'p3', 'k1', 'p2', 'k2', 'p1', 'k2', 'p1', 'k1', 'p1', 'k1', 'p1', 'k2', 'p2', 'k3', 'p1', 'k1', 'p2', 'k1', 'p2', 'k2', 'p1', 'k1', 'p1', 'k1', 'p2', 'k1', 'p2', 'k1', 'p2', 'k2', 'p2', 'k5', 'p2', 'k3', 'p1', 'k1', 'p1', 'k1', 'p2', 'k3', 'p1', 'k2', 'p3']

You could use sub :你可以使用sub

import re

text = ' '.join(
    ['k1', 'p1', 'k1', 'p1', 'k1', 'p2', 'k1', 'p2', 'k1', 'p3', 'k1', 'p3', 'k1', 'p1', 'k2', 'p1', 'k2', 'p3',
     'k1', 'p2', 'k2', 'p1', 'k2', 'p1', 'k1', 'p1', 'k1', 'p1', 'k2', 'p2', 'k3', 'p1', 'k1', 'p2', 'k1', 'p2',
     'k2', 'p1', 'k1', 'p1', 'k1', 'p2', 'k1', 'p2', 'k1', 'p2', 'k2', 'p2', 'k5', 'p2', 'k3', 'p1', 'k1', 'p1',
     'k1', 'p2', 'k3', 'p1', 'k2', 'p3'])


def repl(match):
    return int(match.group(2)) * match.group(1)


result = re.sub('([kp])([1-9])', repl, text)
print(result)

Output输出

k p k p k pp k pp k ppp k ppp k p kk p kk ppp k pp kk p kk p k p k p kk pp kkk p k pp k pp kk p k p k pp k pp k pp kk pp kkkkk pp kkk p k p k pp kkk p kk ppp

Explanation解释

The pattern ([kp])([1-9]) matches a k or a p followed by any digit between 1 and 9 .模式([kp])([1-9])匹配一个k或一个p后跟19之间的任何数字。 For the sub part let's look at the documentation:对于子部分,让我们看一下文档:

Return the string obtained by replacing the leftmost non-overlapping occurrences of pattern in string by the replacement repl.返回通过替换 repl 替换 string 中最左边的不重叠模式出现的字符串。

It turns out that repl can be a function, that receives a match object .事实证明, repl可以是一个接收匹配对象的函数 In this case the repl takes the second matching group (number of repetitions) cast it to int an multiplies for the first matching group, the letter k or p.在这种情况下, repl将第二个匹配组(重复次数)强制转换为 int 与第一个匹配组(字母 k 或 p)的乘法。

Note that I used as input the example in your question joined by space.请注意,我在您的问题中使用空格连接的示例作为输入。

Here is a more classic approach.这里有一个更经典的方法。 I continued based on your code, and just replaced values as described in code comments.我继续基于你的代码,只是替换了代码注释中描述的值。

    import re

    with open("suspicious_knitting.txt") as file:
        string = file.read()
        codes = re.findall("k[1-9]|p[1-9]" ,string)

        for i in range(len(codes) - 1):
            letter = codes[i][0] # this will be k or p
            number = codes[i][1] # this is number after k/p
            if letter == 'k':
                codes[i] = letter + ('1' * int(number)) # for example, if variable number is 5, 1 will be repeated 5 times
            else:
                codes[i] = letter + ('0' * int(number))

        # now array codes contains desireable values :)

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

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