如何解决如何在Julia中替换字符串中的几个字符
我尝试了以下功能。 示例:
import Head from 'next/head'
import styles from '../styles/Home.module.css'
import Router from 'next/router'
import React,{ useEffect } from "react";
import { Redirect } from 'react-router';
export default function Home(props) {
useEffect(() => {
const { pathname } = Router
if (pathname == '/') {
Router.push('/login');
}
},[props]);
return ''
}
输出:dna = "ACGTGGTCTTAA"
function to_rna(dna)
for (nucleotides1,nucleotides2) in zip("GCTA","CGAU")
dna = replace(dna,nucleotides1 => nucleotides2)
end
return dna
end
,这是不期望的。
预期输出:"UGGUGGUGUUUU"
有人能指出出什么问题了吗
解决方法
您要按顺序执行每个字母的替换:
julia> function to_rna(dna)
for (nucleotides1,nucleotides2) in zip("GCTA","CGAU")
dna = replace(dna,nucleotides1 => nucleotides2)
@show nucleotides1 => nucleotides2
@show dna
end
return dna
end
to_rna (generic function with 1 method)
julia> to_rna(dna)
nucleotides1 => nucleotides2 = 'G' => 'C'
dna = "ACCTCCTCTTAA"
nucleotides1 => nucleotides2 = 'C' => 'G'
dna = "AGGTGGTGTTAA"
nucleotides1 => nucleotides2 = 'T' => 'A'
dna = "AGGAGGAGAAAA"
nucleotides1 => nucleotides2 = 'A' => 'U'
dna = "UGGUGGUGUUUU"
"UGGUGGUGUUUU"
julia> dna
"ACGTGGTCTTAA"
也就是说,在第一步等之后,您无法将RNA C
与DNA C
区分出来。
您希望它可以并行工作-像这样:
julia> to_rna2(dna) = map(dna) do nucleotide
NUCLEOTIDE_MAPPING[nucleotide]
end
to_rna2 (generic function with 1 method)
julia> NUCLEOTIDE_MAPPING = Dict(n1 => n2 for (n1,n2) in zip("GCTA","CGAU"))
Dict{Char,Char} with 4 entries:
'A' => 'U'
'G' => 'C'
'T' => 'A'
'C' => 'G'
julia> to_rna2(dna)
"UGCACCAGAAUU"
这也消除了重复遍历字符串四次的不必要工作。
replace
本身已经可以做到这一点-如果您给它提供一个数组并向其传递多个替换参数:
julia> replace(collect(dna),NUCLEOTIDE_MAPPING...)
12-element Array{Char,1}:
'U'
'G'
'C'
'A'
'C'
'C'
'A'
'G'
'A'
'A'
'U'
'U'
要取回字符串而不是数组,只需再次join
:
julia> replace(collect(dna),NUCLEOTIDE_MAPPING...) |> join
"UGCACCAGAAUU"
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。