在 Julia 中用多个值替换多个字符串

Dij*_*e85 5 string replace julia dataframes.jl

在 Python pandas 中,您可以传递一个字典, df.replace以便将每个匹配的键替换为其相应的值。我经常使用这个功能来替换西班牙语中的单词缩写,这些缩写会弄乱句子标记器。

\n

朱莉娅身上有类似的东西吗?或者甚至更好,以便我(和未来的用户)可以从经验中学习,关于如何用 Julia 漂亮且高性能的语法实现这样的功能有什么想法吗?

\n

谢谢你!

\n

编辑:根据要求添加示例

\n

输入:

\n
julia> DataFrames.DataFrame(Dict("A" => ["This is an ex.", "This is a samp.", "This is a samp. of an ex."]))\n3\xc3\x971 DataFrame\n Row \xe2\x94\x82 A                  \n     \xe2\x94\x82 String             \n\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xbc\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\n   1 \xe2\x94\x82 This is an ex.\n   2 \xe2\x94\x82 This is a samp.\n   3 \xe2\x94\x82 This is a samp. of an ex.\n
Run Code Online (Sandbox Code Playgroud)\n

期望的输出:

\n
3\xc3\x971 DataFrame\n Row \xe2\x94\x82 A                  \n     \xe2\x94\x82 String             \n\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xbc\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\n   1 \xe2\x94\x82 This is an example\n   2 \xe2\x94\x82 This is a sample\n   3 \xe2\x94\x82 This is a sample of an example\n
Run Code Online (Sandbox Code Playgroud)\n

Bog*_*ski 4

在 Julia 中,此功能也是replace. 它需要一个集合并替换其中的元素。最简单的形式是:

\n
julia> x = ["a", "ab", "ac", "b", "bc", "bd"]\n6-element Vector{String}:\n "a"\n "ab"\n "ac"\n "b"\n "bc"\n "bd"\n\njulia> replace(x, "a" => "aa", "b" => "bb")\n6-element Vector{String}:\n "aa"\n "ab"\n "ac"\n "bb"\n "bc"\n "bd"\n
Run Code Online (Sandbox Code Playgroud)\n

如果您有更复杂的替换模式,您可以传递一个执行替换的函数:

\n
julia> replace(x) do s\n           length(s) == 1 ? s^2 : s\n       end\n6-element Vector{String}:\n "aa"\n "ab"\n "ac"\n "bb"\n "bc"\n "bd"\n
Run Code Online (Sandbox Code Playgroud)\n

还有一个replace!就地做同样的事情。

\n

这是你想要的吗?

\n

编辑

\n

替换字符串向量中的子字符串:

\n
julia> df = DataFrame("A" => ["This is an ex.", "This is a samp.", "This is a samp. of an ex."])\n3\xc3\x971 DataFrame\n Row \xe2\x94\x82 A\n     \xe2\x94\x82 String\n\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xbc\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\n   1 \xe2\x94\x82 This is an ex.\n   2 \xe2\x94\x82 This is a samp.\n   3 \xe2\x94\x82 This is a samp. of an ex.\n\njulia> df.A .= replace.(df.A, "ex." => "example", "samp." => "sample")\n3-element Vector{String}:\n "This is an example"\n "This is a sample"\n "This is a sample of an example"\n
Run Code Online (Sandbox Code Playgroud)\n

注意两件事:

\n
    \n
  1. 您不需要传递DictDataFrame构造函数。只需传递对就足够了。
  2. \n
  3. 在作业中,我使用.=not =,它对已经存在的向量中的更新值进行就地替换(我将其显示出来是为了与 @Sundar R 在评论中提出的内容进行比较,这是分配新向量的替代方案;差异可能对于你的情况来说并不重要,但我只是想向你展示两种语法)。
  4. \n
\n