Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python根据条件/使用函数查找和替换_Python_Regex - Fatal编程技术网

Python根据条件/使用函数查找和替换

Python根据条件/使用函数查找和替换,python,regex,Python,Regex,我希望脚本一次看一对,意思是: 评估n76a+q80a。如果abs(76-80)

我希望脚本一次看一对,意思是:

评估n76a+q80a。如果abs(76-80)<10,则将“+”替换为“\”: 否则不要改变任何事情。 然后评估q80a+l83a,然后做同样的事情

所需输出应为:

String = n76a+q80a+l83a+i153a+l203f+r207a+s211a+s215w+f216a+e283l
我试过的是

n76a_q80a_l83a+i153a+l203f_r207a_s211a_s215w_f216a+e283l
def aa_dist(x):
如果abs(int(x[1:3])-int(x[6:8])小于10:
打印re.sub(r'\+','\',x)
以alex的身份打开(输入_文件'r'):
oligos_list=alex.read()
机场管理区(寡头名单)
这就是到目前为止我所知道的。我知道我的代码只会将所有的“+”替换为“389;”,因为它只计算第一对和替换所有。我应该怎么做?

导入itertools,重新
def aa_dist(x):
if abs(int(x[1:3]) - int(x[6:8])) < 10:
    print re.sub(r'\+', '_', x)

with open(input_file, 'r') as alex:
    oligos_list = alex.read()
    aa_dist(oligos_list)
my_string=“n76a+q80a+l83a+i153a+l203f+r207a+s211a+s215w+f216a+e283l” #首先提取数字 my_numbers=map(int,re.findall(“[0-9]+”,my_字符串)) #在+(无用的注释)上拆分字符串 parts=my_string.split(“+”) def get_加注口((a,b)): ''此方法决定加入者'' 如果abs(a-b)<10,则返回“u”,否则返回“+” fillers=map(获取filler,zip(我的号码,我的号码[1:])#找出我们需要的填充物 打印“.join(itertools.chain.from_iterable(zip(parts,fillers)))+parts[-1]#它将始终跳过最后一部分,因此必须添加它
这是你实现这一目标的一种方法。。。也是毫无价值的注释的一个例子

import itertools,re
my_string=“n76a+q80a+l83a+i153a+l203f+r207a+s211a+s215w+f216a+e283l”
#首先提取数字
my_numbers=map(int,re.findall(“[0-9]+”,my_字符串))
#在+(无用的注释)上拆分字符串
parts=my_string.split(“+”)
def get_加注口((a,b)):
''此方法决定加入者''
如果abs(a-b)<10,则返回“u”,否则返回“+”
fillers=map(获取filler,zip(我的号码,我的号码[1:])#找出我们需要的填充物
打印“.join(itertools.chain.from_iterable(zip(parts,fillers)))+parts[-1]#它将始终跳过最后一部分,因此必须添加它
这是你实现这一目标的一种方法。。。也是毫无价值的注释的一个例子

import itertools,re
my_string=“n76a+q80a+l83a+i153a+l203f+r207a+s211a+s215w+f216a+e283l”
#首先提取数字
my_numbers=map(int,re.findall(“[0-9]+”,my_字符串))
#在+(无用的注释)上拆分字符串
parts=my_string.split(“+”)
def get_加注口((a,b)):
''此方法决定加入者''
如果abs(a-b)<10,则返回“u”,否则返回“+”
fillers=map(获取filler,zip(我的号码,我的号码[1:])#找出我们需要的填充物
打印“.join(itertools.chain.from_iterable(zip(parts,fillers)))+parts[-1]#它将始终跳过最后一部分,因此必须添加它
这是你实现这一目标的一种方法。。。也是毫无价值的注释的一个例子

import itertools,re
my_string=“n76a+q80a+l83a+i153a+l203f+r207a+s211a+s215w+f216a+e283l”
#首先提取数字
my_numbers=map(int,re.findall(“[0-9]+”,my_字符串))
#在+(无用的注释)上拆分字符串
parts=my_string.split(“+”)
def get_加注口((a,b)):
''此方法决定加入者''
如果abs(a-b)<10,则返回“u”,否则返回“+”
fillers=map(获取filler,zip(我的号码,我的号码[1:])#找出我们需要的填充物
打印“.join(itertools.chain.from_iterable(zip(parts,fillers)))+parts[-1]#它将始终跳过最后一部分,因此必须添加它

这是你实现这一目标的一种方法。。。并且也是仅通过
re
模块的无价值评论的示例

import itertools,re

my_string =  "n76a+q80a+l83a+i153a+l203f+r207a+s211a+s215w+f216a+e283l"
#first extract the numbers    
my_numbers = map(int,re.findall("[0-9]+",my_string))
#split the string on + (useless comment)
parts = my_string.split("+")

def get_filler((a,b)):
    '''this method decides on the joiner'''
    return "_" if abs(a-b) < 10 else '+'

fillers = map(get_filler,zip(my_numbers,my_numbers[1:])) #figure out what fillers we need
print "".join(itertools.chain.from_iterable(zip(parts,fillers)))+parts[-1] #it will always skip the last part so gotta add it

仅通过
re
模块

import itertools,re

my_string =  "n76a+q80a+l83a+i153a+l203f+r207a+s211a+s215w+f216a+e283l"
#first extract the numbers    
my_numbers = map(int,re.findall("[0-9]+",my_string))
#split the string on + (useless comment)
parts = my_string.split("+")

def get_filler((a,b)):
    '''this method decides on the joiner'''
    return "_" if abs(a-b) < 10 else '+'

fillers = map(get_filler,zip(my_numbers,my_numbers[1:])) #figure out what fillers we need
print "".join(itertools.chain.from_iterable(zip(parts,fillers)))+parts[-1] #it will always skip the last part so gotta add it

仅通过
re
模块

import itertools,re

my_string =  "n76a+q80a+l83a+i153a+l203f+r207a+s211a+s215w+f216a+e283l"
#first extract the numbers    
my_numbers = map(int,re.findall("[0-9]+",my_string))
#split the string on + (useless comment)
parts = my_string.split("+")

def get_filler((a,b)):
    '''this method decides on the joiner'''
    return "_" if abs(a-b) < 10 else '+'

fillers = map(get_filler,zip(my_numbers,my_numbers[1:])) #figure out what fillers we need
print "".join(itertools.chain.from_iterable(zip(parts,fillers)))+parts[-1] #it will always skip the last part so gotta add it

仅通过
re
模块

import itertools,re

my_string =  "n76a+q80a+l83a+i153a+l203f+r207a+s211a+s215w+f216a+e283l"
#first extract the numbers    
my_numbers = map(int,re.findall("[0-9]+",my_string))
#split the string on + (useless comment)
parts = my_string.split("+")

def get_filler((a,b)):
    '''this method decides on the joiner'''
    return "_" if abs(a-b) < 10 else '+'

fillers = map(get_filler,zip(my_numbers,my_numbers[1:])) #figure out what fillers we need
print "".join(itertools.chain.from_iterable(zip(parts,fillers)))+parts[-1] #it will always skip the last part so gotta add it


总是用“+”和小写字母吗?是的。总是这样。我认为索引值在
i153a+l203f
总是“+”和小写字母的情况下会发生变化吗?是的。总是这样。我认为索引值在
i153a+l203f
总是“+”和小写字母的情况下会发生变化吗?是的。总是这样。我认为索引值在
i153a+l203f
总是“+”和小写字母的情况下会发生变化吗?是的。一直都是这样。我想如果
i153a+l203f
谢谢,索引值会发生变化!一般来说,我对编程非常陌生,对itertool模块不太熟悉。请你再解释一下下面两行到底在做什么好吗?fillers=map(get_filler,zip(my_numbers,my_numbers[1:]))#找出我们需要打印的填充物。加入(itertools.chain.from_iterable(zip(parts,fillers)))+parts[-1]
itertools.chain
只需获取一个二维列表并将其展平。。。这是很多方法中的一种。。。上面的那行将数字列表自动拉入拉链,以获得相邻的数字对,并将它们映射到决定+或_的函数。谢谢!一般来说,我对编程非常陌生,对itertool模块不太熟悉。请你再解释一下下面两行到底在做什么好吗?fillers=map(get_filler,zip(my_numbers,my_numbers[1:]))#找出我们需要打印的填充物。加入(itertools.chain.from_iterable(zip(parts,fillers)))+parts[-1]
itertools.chain
只需获取一个二维列表并将其展平。。。这是很多方法中的一种。。。上面的那行将数字列表自动拉入拉链,以获得相邻的数字对,并将它们映射到决定+或_的函数。谢谢!一般来说,我对编程非常陌生,对itertool模块不太熟悉。请你再解释一下下面两行到底在做什么好吗?fillers=map(get_filler,zip(my_numbers,my_numbers[1:]))#找出我们需要打印的填充物。加入(itertools.chain.from_iterable(zip(parts,fillers)))+parts[-1]
itertools.chain
只需获取一个二维列表并将其展平。。。这是很多方法中的一种。。。上面的那行将数字列表自动拉入拉链,以获得相邻的数字对,并将它们映射到决定+或_的函数。谢谢!我真的是个新手