GVKun编程网logo

Python用函数输出替换字符串模式(python的字符串替换函数)

17

本篇文章给大家谈谈Python用函数输出替换字符串模式,以及python的字符串替换函数的知识点,同时本文还将给你拓展KMP-字符串模式匹配(c++/python实现)、python–使用字符串数组替

本篇文章给大家谈谈Python用函数输出替换字符串模式,以及python的字符串替换函数的知识点,同时本文还将给你拓展KMP-字符串模式匹配(c++/python实现)、python – 使用字符串数组替换字符串、python – 如何用下一个字符串替换字符串中的字符?、python – 模式匹配并用if else循环替换字符串等相关知识,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

Python用函数输出替换字符串模式(python的字符串替换函数)

Python用函数输出替换字符串模式(python的字符串替换函数)

我在Python中有一个字符串 The quick @red fox jumps over the @lame brown dog.

我试图替换@以该词作为参数的函数输出开头的每个词。

def my_replace(match):    return match + str(match.index(''e''))#Psuedo-codestring = "The quick @red fox jumps over the @lame brown dog."string.replace(''@%match'', my_replace(match))# Result"The quick @red2 fox jumps over the @lame4 brown dog."

有聪明的方法吗?

答案1

小编典典

您可以将函数传递给re.sub。该函数将接收一个match对象作为参数,用于.group()将匹配提取为字符串。

>>> def my_replace(match):...     match = match.group()...     return match + str(match.index(''e''))...>>> string = "The quick @red fox jumps over the @lame brown dog.">>> re.sub(r''@\w+'', my_replace, string)''The quick @red2 fox jumps over the @lame4 brown dog.''

KMP-字符串模式匹配(c++/python实现)

KMP-字符串模式匹配(c++/python实现)

KMP算法可以在O(n+m)的时间数量级上完成模式匹配,其做法在于:没当一次匹配过程中出现字符比较不等时,不需要回溯指针,而是利用已经得到的“部分匹配”的结果将模式向右“滑动”尽可能远的一段距离后,继续进行比较。

在KMP算法中主要是先得到子字符串的next数组,计算next数组方法如下:

(1)、next[1]=0,next[1]=1

(2)、后面求解每一位的next[j]值时,根据j的前一位进行比较,令k=next[j-1];

(3)、将s[j-1]于s[k]进行比较:

  如果相等,则令该next[j]=k+1

  如果不相等,令k=next[k],若k不等于0,跳到(3),否则,next[j]=1

eg:

如下图j=4时,此时前面的next数组为0 1 1,所以k=next[3]=1 ,由于s[j-1]=s[3]=a、s[k]=s[1]=a,所以s[j]=s[4]=1+1=2

如下图j=5时,next={0,1,1,2},k=2,s[j-1]=a,s[k]=b,所以k=next[k]=next[2]=1,此时s[k]=s[1]=a,所以s[j]=s[5]=1+1=2

比如子字符串为:abaabcac,计算如下图

得到子字符串之后,在字符串匹配的时候,子字符串不再是移动1个字符串,而是移动next(j)个位置,代码如:(这里需要注意,next数组是从1开始的,没有0)

c++实现:

 

#include <iostream>
#include <stdlib.h>
#include <vector>
using namespace std;

//利用模版进行输出
template <typename T>
void print(vector<T> a)
{
    typename vector<T>::iterator it;//前面要加typename,要不会出错
    for(it=++a.begin();it!=a.end();it++)
    {
        cout<<*it;
    }
}


vector<int> next_str={-1,0,1};//为了使下标从1开始,在前面添加一个-1
void next_list(vector<char> v,int n)//得到子字符串的next数组
{
    int j=3,k;
    while(j<=n)
    {
        k=next_str[j-1];
        if(v[j-1]==v[k])
        {
            k++;
            next_str.push_back(k);
        }
        else
        {
            k=next_str[k];
            while(true)
            {
                if(k==0)
                {
                    next_str.push_back(1);
                    break;
                }
                if(v[j-1]==v[k])
                {
                    next_str.push_back(++k);
                    break;
                }
                else
                {
                    k=next_str[k];
                }
            }
        }
        j++;
    }
}

int get_index(vector<char> l,vector<char> s,int n,int m)
{
    int i=1,j=1;
    while(i<=n&&j<m)
    {
        if(j==0||l[i]==s[j])
        {
            i++;
            j++;
        }
        else
        {
            j=next_str[j];
        }
    }
    if(j==m)
    {
        return i-j+1;
    }
    else
    {
        return -1;
    }
}

int main()
{

    int n;
    cin>>n;
    vector<char> longstr={''#''};//为了下标从1开始,添加一个’#‘
    char c;
    for(int i=0;i<n;i++)
    {
        cin>>c;
        longstr.push_back(c);
    }

    int m;
    cin>>m;
    vector<char> shortstr={''#''};//为了下标从1开始,添加一个’#‘
    for(int i=0;i<m;i++)
    {
        cin>>c;
        shortstr.push_back(c);
    }

//    int n=20,m=8;
//    vector<char> longstr={''#'',''b'',''c'',''a'',''a'',''b'',''a'',''c'',''a'',''b'',''a'',''a'',''b'',''c'',''a'',''c'',''a'',''b'',''c'',''d'',''a''};
//    vector<char> shortstr={''#'',''a'',''b'',''a'',''a'',''b'',''c'',''a'',''c''};

    next_list(shortstr,m);
    print(next_str);
    cout<<endl;

    int index=get_index(longstr,shortstr,n,m);
    if(index>=0)
    {
        print(shortstr);
        cout<<"";
        print(longstr);
        cout<<"中,从位置"<<index<<"开始"<< endl;
    }
    else
    {
        print(shortstr);
        cout<<"不在";
        print(longstr);
        cout<<"中endl";
    }
    return 0;
}
View Code

 

python实现:

#-*- coding:utf-8 -*-

class KMP:

    def __init__(self,s_long,s_short):
        self.s_long=s_long
        self.s_short=s_short
        self.flag=0

    def get_nextList(self):
        l=[0,1]
        for i in range(2,len(self.s_short)):
            l.append(self.get_num_1(self.s_short[0:i]))
        print l
        b=0
        a=0
        while True:
            if self.s_short[a]==self.s_long[b]:
                a+=1
                b+=1
            else:
                a=l[a]-1
                if a==-1:
                   a += 1
                   b += 1
            if self.s_short==self.s_long[b:b+len(self.s_short)]:
                break
            if b==len(self.s_long):
                return 0
        return b+1

    ''''''
        功能(见图2、3):获得子字符串的next数组,和第一张图方法比一样,更容易理解
            s:用于存储该字符前面所有的子字符串(注意子字符串是从这个字符串开始从后往前,长度慢慢增长)
            while循环:用于确定前面最长重复的字符串(注意,应该从长的开始匹配,且必须从第一个字符开始)
                      返回最长字符串长度+1
    ''''''
    def get_num_1(self,string):
        s=[]
        for i in range(1,len(string)):
            s.append(string[len(string)-i:len(string)])
        while s:
            temp=s.pop()
            n=len(temp)
            if temp==string[0:n+0]:
                return len(temp)+1
        return 1


long=raw_input()
short=raw_input()
print KMP(long,short).get_nextList()
View Code

python – 使用字符串数组替换字符串

python – 使用字符串数组替换字符串

假设我有一个字符串s

s = "?,?,test4,test5"

我知道有三个问号,我想用下面的数组相应地替换每个问号

replace_array = ['test1','test2','test3']

获得

output = "test1,test2,test3,test5"

Python中是否有一个函数,比如s.magic_replace_func(* replace_array)可以实现预期的目标?

谢谢!

解决方法

使用str.replace并替换’?’使用'{}’,然后您可以使用str.format方法:

>>> s = "?,test5"
>>> replace_array = ['test1','test3']
>>> s.replace('?','{}',len(replace_array)).format(*replace_array)
'test1,test5'

python – 如何用下一个字符串替换字符串中的字符?

python – 如何用下一个字符串替换字符串中的字符?

我想用下一个字符替换字符串的每个字符,最后一个应该成为第一个.这是一个例子:

abcdefghijklmnopqrstuvwxyz

应成为:

bcdefghijklmnopqrstuvwxyza

是否可以在不使用替换功能的情况下进行26次?

解决方法

您可以使用 str.translate() method让Python在一个步骤中替换其他字符.

使用string.maketrans() function将ASCII字符映射到其目标;使用string.ascii_lowercase可以帮助您,因为它可以节省您自己键入所有字母:

from string import ascii_lowercase
try:
    # Python 2
    from string import maketrans
except ImportError:
    # Python 3 made maketrans a static method
    maketrans = str.maketrans 

cipher_map = maketrans(ascii_lowercase,ascii_lowercase[1:] + ascii_lowercase[:1])
encrypted = text.translate(cipher_map)

演示:

>>> from string import maketrans
>>> from string import ascii_lowercase
>>> cipher_map = maketrans(ascii_lowercase,ascii_lowercase[1:] + ascii_lowercase[:1])
>>> text = ''the quick brown fox jumped over the lazy dog''
>>> text.translate(cipher_map)
''uif rvjdl cspxo gpy kvnqfe pwfs uif mbaz eph''

python – 模式匹配并用if else循环替换字符串

python – 模式匹配并用if else循环替换字符串

我有一个包含多行以“1ECLI H — 12.345 …..”开头的文件.我想删除I和H之间的空格,并在迭代H模式时添加R / S / T.例如. H810如果连续三行重复,则应加上字母R,S(第二次迭代),T(第三次迭代).所以它将是H810R.任何帮助将不胜感激.
文字如下所示

1ECLI  H813   98   7.529   8.326   9.267
1ECLI  H813   99   7.427   8.470   9.251
1ECLI  C814  100   7.621   8.513   9.263
1ECLI  H814  101   7.607   8.617   9.289
1ECLI  H814  102   7.633   8.489   9.156
1ECLI  H814  103   7.721   8.509   9.305
1ECLI   C74  104   8.164   8.733  10.740
1ECLI  H74R  105   8.247   8.690  10.799

在chage

1ECLI H813R   98   7.529   8.326   9.267
1ECLI H813S   99   7.427   8.470   9.251
1ECLI  C814  100   7.621   8.513   9.263
1ECLI H814R  101   7.607   8.617   9.289
1ECLI H814s  102   7.633   8.489   9.156
1ECLI H814T  103   7.721   8.509   9.305
1ECLI   C74  104   8.164   8.733  10.740
1ECLI  H74R  105   8.247   8.690  10.799

谢谢.

最佳答案
如果您的真实输入文件与您发布的文件相同,即使是低于一个也可以提供所需的输出.

awk 'BEGIN{split("R,S,T",a,/,/)}f=$2~/^H[0-9]+$/{$2 = $2 a[++c]}!f{c=0}1' infile 

说明

> split(“R,T”,/) – 用分隔符逗号分割字符串“R,并保存在数组a中,所以它变为[1] = R,a [2 ] = S,a [3] = T.
> f = $2~ / ^ H [0-9] $/ – f是变量,验证regexp $2~ / ^ H [0-9] $/,返回布尔状态.如果它返回true则变量f将为真,否则为false
> $2 = $2 a [c]如果高于1则为真,则修改第二个字段,因此第二个字段将具有现有值加数组a值,对应于索引(c),c是预增量变量
>!f {c = 0}如果变量f为假,则重置变量c,而不是连续.
最后> 1执行默认操作,即打印当前/记录/行,打印$0.要知道awk是如何工作的,请使用awk’1’infile,它将打印所有记录/行,而awk’0’infile则不打印任何内容.除零以外的任何数字都为true,这会触发默认行为.

检测结果:

$cat infile
1ECLI  H813   98   7.529   8.326   9.267
1ECLI  H813   99   7.427   8.470   9.251
1ECLI  C814  100   7.621   8.513   9.263
1ECLI  H814  101   7.607   8.617   9.289
1ECLI  H814  102   7.633   8.489   9.156
1ECLI  H814  103   7.721   8.509   9.305
1ECLI   C74  104   8.164   8.733  10.740
1ECLI  H74R  105   8.247   8.690  10.799

$awk 'BEGIN{split("R,/)}f=$2~/^H[0-9]+$/{$2 = $2 a[++c]}!f{c=0}1' infile
1ECLI H813R 98 7.529 8.326 9.267
1ECLI H813S 99 7.427 8.470 9.251
1ECLI  C814  100   7.621   8.513   9.263
1ECLI H814R 101 7.607 8.617 9.289
1ECLI H814S 102 7.633 8.489 9.156
1ECLI H814T 103 7.721 8.509 9.305
1ECLI   C74  104   8.164   8.733  10.740
1ECLI  H74R  105   8.247   8.690  10.799

如果你想要更好的格式,如tab或其他一些char作为字段分隔符,那么你可以使用下面一个,修改OFS变量

$awk -v OFS="\t" 'BEGIN{split("R,/)}f=$2~/^H[0-9]+$/{$2 = $2 a[++c]}!f{c=0}{$1=$1}1'  infile
1ECLI   H813R   98  7.529   8.326   9.267
1ECLI   H813S   99  7.427   8.470   9.251
1ECLI   C814    100 7.621   8.513   9.263
1ECLI   H814R   101 7.607   8.617   9.289
1ECLI   H814S   102 7.633   8.489   9.156
1ECLI   H814T   103 7.721   8.509   9.305
1ECLI   C74     104 8.164   8.733   10.740
1ECLI   H74R    105 8.247   8.690   10.799

今天关于Python用函数输出替换字符串模式python的字符串替换函数的分享就到这里,希望大家有所收获,若想了解更多关于KMP-字符串模式匹配(c++/python实现)、python – 使用字符串数组替换字符串、python – 如何用下一个字符串替换字符串中的字符?、python – 模式匹配并用if else循环替换字符串等相关知识,可以在本站进行查询。

本文标签: