如何在Julia中重复字符串中的单个字符

如何在Julia中重复字符串中的单个字符,julia,Julia,这显示了如何在Python中重复字符串中的单个字符 >>> s = '123abc' >>> n = 3 >>> ''.join([c*n for c in s]) '111222333aaabbbccc' 你在朱莉娅身上会怎么做 编辑 作为朱莉娅的新人,我对这门语言所能提供的东西感到惊讶 例如,我认为上面的Python代码与任何语言中的代码一样简单。然而,正如我下面的答案所示,Julia等价代码join([c^n表示s中的c])可以说是更

这显示了如何在Python中重复字符串中的单个字符

>>> s = '123abc'
>>> n = 3
>>> ''.join([c*n for c in s])
'111222333aaabbbccc'
你在朱莉娅身上会怎么做

编辑

作为朱莉娅的新人,我对这门语言所能提供的东西感到惊讶

例如,我认为上面的Python代码与任何语言中的代码一样简单。然而,正如我下面的答案所示,Julia等价代码
join([c^n表示s中的c])
可以说是更简单的,对于任何语言来说都可能达到最简单的程度

另一方面,@niczky12表明,在
字符串
函数中添加省略号运算符后,速度可以比稍微简单的
连接
函数大幅度提高

在一个例子中,朱莉娅因简洁而闪耀。在另一种情况下,茱莉亚因速度而闪耀

对于Python程序员来说,当他们注意到
c^n
在Python中只是
c*n
时,第一种情况应该可以立即阅读。当他们看到使用省略号操作符提高速度时,额外的复杂性可能不会阻止他们学习Julia。读者可能开始认为我希望许多Python程序员会认真对待Julia。他们不会错的


感谢@rickhg12hs对基准点的建议。我学到了很多。

你可以用Julia comprehension或生成器来完成

julia> VERSION
v"1.0.0"

julia> s = "123abc"
"123abc"

# n is number of times to repeat each character.
julia> n = 3
3

# Using a Julia comprehension with [...]
julia> join([c^n for c in s])
"111222333aaabbbccc"

# Using a Julia generator without the [...]
julia> join(c^n for c in s)
"111222333aaabbbccc"
对于小弦,在速度上应该没有什么实际差异

编辑

TL;DR:一般来说,生成器的速度比理解速度快一些。但是,请参见案例3了解相反的情况。记忆评估结果非常相似。

@rickhg12hs建议最好有基准

使用great BenchmarkTools包,结果如下

n=重复每个字符的次数

s=“abcdefghijklmnopqrstuvxyz”在每种情况下

在每种情况下,首先列出理解时间中位数C,然后列出生成器时间中位数G。时间四舍五入似乎是适当的,原始数字在编号摘要的下面。当然,越小越好

对记忆的估计没有太大的不同

1。n=26,C=3.8 vs.G=2.8μs,G快

julia> using BenchmarkTools

julia> n = 26;

julia> @benchmark join([c^n for c in s])
BenchmarkTools.Trial:
  memory estimate:  3.55 KiB
  allocs estimate:  39
  --------------
  minimum time:     3.688 μs (0.00% GC)
  median time:      3.849 μs (0.00% GC)
  mean time:        4.956 μs (16.27% GC)
  maximum time:     5.211 ms (99.85% GC)
  --------------
  samples:          10000
  evals/sample:     8

julia> @benchmark join(c^n for c in s)
BenchmarkTools.Trial:
  memory estimate:  3.19 KiB
  allocs estimate:  36
  --------------
  minimum time:     2.661 μs (0.00% GC)
  median time:      2.756 μs (0.00% GC)
  mean time:        3.622 μs (19.94% GC)
  maximum time:     4.638 ms (99.89% GC)
  --------------
  samples:          10000
  evals/sample:     9
julia> n = 260;

julia> @benchmark join([c^n for c in s])
BenchmarkTools.Trial:
  memory estimate:  19.23 KiB
  allocs estimate:  39
  --------------
  minimum time:     8.125 μs (0.00% GC)
  median time:      10.691 μs (0.00% GC)
  mean time:        18.559 μs (35.36% GC)
  maximum time:     43.930 ms (99.92% GC)
  --------------
  samples:          10000
  evals/sample:     1

julia> @benchmark join(c^n for c in s)
BenchmarkTools.Trial:
  memory estimate:  18.88 KiB
  allocs estimate:  36
  --------------
  minimum time:     7.270 μs (0.00% GC)
  median time:      8.126 μs (0.00% GC)
  mean time:        10.872 μs (18.04% GC)
  maximum time:     10.592 ms (99.87% GC)
  --------------
  samples:          10000
  evals/sample:     4
julia> n = 2600; 

julia> @benchmark join([c^n for c in s])
BenchmarkTools.Trial:
  memory estimate:  150.16 KiB
  allocs estimate:  39
  --------------
  minimum time:     51.746 μs (0.00% GC)
  median time:      63.293 μs (0.00% GC)
  mean time:        77.315 μs (2.79% GC)
  maximum time:     3.721 ms (96.85% GC)
  --------------
  samples:          10000
  evals/sample:     1

julia> @benchmark join(c^n for c in s)
BenchmarkTools.Trial:
  memory estimate:  149.80 KiB
  allocs estimate:  36
  --------------
  minimum time:     47.897 μs (0.00% GC)
  median time:      63.720 μs (0.00% GC)
  mean time:        88.716 μs (17.58% GC)
  maximum time:     42.457 ms (99.83% GC)
  --------------
  samples:          10000
  evals/sample:     1
julia> n = 26000; 

julia> @benchmark join([c^n for c in s])
BenchmarkTools.Trial:
  memory estimate:  1.44 MiB
  allocs estimate:  39
  --------------
  minimum time:     457.589 μs (0.00% GC)
  median time:      666.710 μs (0.00% GC)
  mean time:        729.592 μs (10.91% GC)
  maximum time:     42.673 ms (98.76% GC)
  --------------
  samples:          6659
  evals/sample:     1

julia> @benchmark join(c^n for c in s)
BenchmarkTools.Trial:
  memory estimate:  1.44 MiB
  allocs estimate:  36
  --------------
  minimum time:     475.977 μs (0.00% GC)
  median time:      516.176 μs (0.00% GC)
  mean time:        659.001 μs (10.36% GC)
  maximum time:     42.268 ms (98.41% GC)
  --------------
  samples:          7548
  evals/sample:     1
2。n=260,C=10.7 vs.G=8.1μs,G快

julia> using BenchmarkTools

julia> n = 26;

julia> @benchmark join([c^n for c in s])
BenchmarkTools.Trial:
  memory estimate:  3.55 KiB
  allocs estimate:  39
  --------------
  minimum time:     3.688 μs (0.00% GC)
  median time:      3.849 μs (0.00% GC)
  mean time:        4.956 μs (16.27% GC)
  maximum time:     5.211 ms (99.85% GC)
  --------------
  samples:          10000
  evals/sample:     8

julia> @benchmark join(c^n for c in s)
BenchmarkTools.Trial:
  memory estimate:  3.19 KiB
  allocs estimate:  36
  --------------
  minimum time:     2.661 μs (0.00% GC)
  median time:      2.756 μs (0.00% GC)
  mean time:        3.622 μs (19.94% GC)
  maximum time:     4.638 ms (99.89% GC)
  --------------
  samples:          10000
  evals/sample:     9
julia> n = 260;

julia> @benchmark join([c^n for c in s])
BenchmarkTools.Trial:
  memory estimate:  19.23 KiB
  allocs estimate:  39
  --------------
  minimum time:     8.125 μs (0.00% GC)
  median time:      10.691 μs (0.00% GC)
  mean time:        18.559 μs (35.36% GC)
  maximum time:     43.930 ms (99.92% GC)
  --------------
  samples:          10000
  evals/sample:     1

julia> @benchmark join(c^n for c in s)
BenchmarkTools.Trial:
  memory estimate:  18.88 KiB
  allocs estimate:  36
  --------------
  minimum time:     7.270 μs (0.00% GC)
  median time:      8.126 μs (0.00% GC)
  mean time:        10.872 μs (18.04% GC)
  maximum time:     10.592 ms (99.87% GC)
  --------------
  samples:          10000
  evals/sample:     4
julia> n = 2600; 

julia> @benchmark join([c^n for c in s])
BenchmarkTools.Trial:
  memory estimate:  150.16 KiB
  allocs estimate:  39
  --------------
  minimum time:     51.746 μs (0.00% GC)
  median time:      63.293 μs (0.00% GC)
  mean time:        77.315 μs (2.79% GC)
  maximum time:     3.721 ms (96.85% GC)
  --------------
  samples:          10000
  evals/sample:     1

julia> @benchmark join(c^n for c in s)
BenchmarkTools.Trial:
  memory estimate:  149.80 KiB
  allocs estimate:  36
  --------------
  minimum time:     47.897 μs (0.00% GC)
  median time:      63.720 μs (0.00% GC)
  mean time:        88.716 μs (17.58% GC)
  maximum time:     42.457 ms (99.83% GC)
  --------------
  samples:          10000
  evals/sample:     1
julia> n = 26000; 

julia> @benchmark join([c^n for c in s])
BenchmarkTools.Trial:
  memory estimate:  1.44 MiB
  allocs estimate:  39
  --------------
  minimum time:     457.589 μs (0.00% GC)
  median time:      666.710 μs (0.00% GC)
  mean time:        729.592 μs (10.91% GC)
  maximum time:     42.673 ms (98.76% GC)
  --------------
  samples:          6659
  evals/sample:     1

julia> @benchmark join(c^n for c in s)
BenchmarkTools.Trial:
  memory estimate:  1.44 MiB
  allocs estimate:  36
  --------------
  minimum time:     475.977 μs (0.00% GC)
  median time:      516.176 μs (0.00% GC)
  mean time:        659.001 μs (10.36% GC)
  maximum time:     42.268 ms (98.41% GC)
  --------------
  samples:          7548
  evals/sample:     1
3。n=2600,C=62.3与G=63.7μs相比,C更快

julia> using BenchmarkTools

julia> n = 26;

julia> @benchmark join([c^n for c in s])
BenchmarkTools.Trial:
  memory estimate:  3.55 KiB
  allocs estimate:  39
  --------------
  minimum time:     3.688 μs (0.00% GC)
  median time:      3.849 μs (0.00% GC)
  mean time:        4.956 μs (16.27% GC)
  maximum time:     5.211 ms (99.85% GC)
  --------------
  samples:          10000
  evals/sample:     8

julia> @benchmark join(c^n for c in s)
BenchmarkTools.Trial:
  memory estimate:  3.19 KiB
  allocs estimate:  36
  --------------
  minimum time:     2.661 μs (0.00% GC)
  median time:      2.756 μs (0.00% GC)
  mean time:        3.622 μs (19.94% GC)
  maximum time:     4.638 ms (99.89% GC)
  --------------
  samples:          10000
  evals/sample:     9
julia> n = 260;

julia> @benchmark join([c^n for c in s])
BenchmarkTools.Trial:
  memory estimate:  19.23 KiB
  allocs estimate:  39
  --------------
  minimum time:     8.125 μs (0.00% GC)
  median time:      10.691 μs (0.00% GC)
  mean time:        18.559 μs (35.36% GC)
  maximum time:     43.930 ms (99.92% GC)
  --------------
  samples:          10000
  evals/sample:     1

julia> @benchmark join(c^n for c in s)
BenchmarkTools.Trial:
  memory estimate:  18.88 KiB
  allocs estimate:  36
  --------------
  minimum time:     7.270 μs (0.00% GC)
  median time:      8.126 μs (0.00% GC)
  mean time:        10.872 μs (18.04% GC)
  maximum time:     10.592 ms (99.87% GC)
  --------------
  samples:          10000
  evals/sample:     4
julia> n = 2600; 

julia> @benchmark join([c^n for c in s])
BenchmarkTools.Trial:
  memory estimate:  150.16 KiB
  allocs estimate:  39
  --------------
  minimum time:     51.746 μs (0.00% GC)
  median time:      63.293 μs (0.00% GC)
  mean time:        77.315 μs (2.79% GC)
  maximum time:     3.721 ms (96.85% GC)
  --------------
  samples:          10000
  evals/sample:     1

julia> @benchmark join(c^n for c in s)
BenchmarkTools.Trial:
  memory estimate:  149.80 KiB
  allocs estimate:  36
  --------------
  minimum time:     47.897 μs (0.00% GC)
  median time:      63.720 μs (0.00% GC)
  mean time:        88.716 μs (17.58% GC)
  maximum time:     42.457 ms (99.83% GC)
  --------------
  samples:          10000
  evals/sample:     1
julia> n = 26000; 

julia> @benchmark join([c^n for c in s])
BenchmarkTools.Trial:
  memory estimate:  1.44 MiB
  allocs estimate:  39
  --------------
  minimum time:     457.589 μs (0.00% GC)
  median time:      666.710 μs (0.00% GC)
  mean time:        729.592 μs (10.91% GC)
  maximum time:     42.673 ms (98.76% GC)
  --------------
  samples:          6659
  evals/sample:     1

julia> @benchmark join(c^n for c in s)
BenchmarkTools.Trial:
  memory estimate:  1.44 MiB
  allocs estimate:  36
  --------------
  minimum time:     475.977 μs (0.00% GC)
  median time:      516.176 μs (0.00% GC)
  mean time:        659.001 μs (10.36% GC)
  maximum time:     42.268 ms (98.41% GC)
  --------------
  samples:          7548
  evals/sample:     1
4。n=26000,C=667与G=516μs相比,G更快

julia> using BenchmarkTools

julia> n = 26;

julia> @benchmark join([c^n for c in s])
BenchmarkTools.Trial:
  memory estimate:  3.55 KiB
  allocs estimate:  39
  --------------
  minimum time:     3.688 μs (0.00% GC)
  median time:      3.849 μs (0.00% GC)
  mean time:        4.956 μs (16.27% GC)
  maximum time:     5.211 ms (99.85% GC)
  --------------
  samples:          10000
  evals/sample:     8

julia> @benchmark join(c^n for c in s)
BenchmarkTools.Trial:
  memory estimate:  3.19 KiB
  allocs estimate:  36
  --------------
  minimum time:     2.661 μs (0.00% GC)
  median time:      2.756 μs (0.00% GC)
  mean time:        3.622 μs (19.94% GC)
  maximum time:     4.638 ms (99.89% GC)
  --------------
  samples:          10000
  evals/sample:     9
julia> n = 260;

julia> @benchmark join([c^n for c in s])
BenchmarkTools.Trial:
  memory estimate:  19.23 KiB
  allocs estimate:  39
  --------------
  minimum time:     8.125 μs (0.00% GC)
  median time:      10.691 μs (0.00% GC)
  mean time:        18.559 μs (35.36% GC)
  maximum time:     43.930 ms (99.92% GC)
  --------------
  samples:          10000
  evals/sample:     1

julia> @benchmark join(c^n for c in s)
BenchmarkTools.Trial:
  memory estimate:  18.88 KiB
  allocs estimate:  36
  --------------
  minimum time:     7.270 μs (0.00% GC)
  median time:      8.126 μs (0.00% GC)
  mean time:        10.872 μs (18.04% GC)
  maximum time:     10.592 ms (99.87% GC)
  --------------
  samples:          10000
  evals/sample:     4
julia> n = 2600; 

julia> @benchmark join([c^n for c in s])
BenchmarkTools.Trial:
  memory estimate:  150.16 KiB
  allocs estimate:  39
  --------------
  minimum time:     51.746 μs (0.00% GC)
  median time:      63.293 μs (0.00% GC)
  mean time:        77.315 μs (2.79% GC)
  maximum time:     3.721 ms (96.85% GC)
  --------------
  samples:          10000
  evals/sample:     1

julia> @benchmark join(c^n for c in s)
BenchmarkTools.Trial:
  memory estimate:  149.80 KiB
  allocs estimate:  36
  --------------
  minimum time:     47.897 μs (0.00% GC)
  median time:      63.720 μs (0.00% GC)
  mean time:        88.716 μs (17.58% GC)
  maximum time:     42.457 ms (99.83% GC)
  --------------
  samples:          10000
  evals/sample:     1
julia> n = 26000; 

julia> @benchmark join([c^n for c in s])
BenchmarkTools.Trial:
  memory estimate:  1.44 MiB
  allocs estimate:  39
  --------------
  minimum time:     457.589 μs (0.00% GC)
  median time:      666.710 μs (0.00% GC)
  mean time:        729.592 μs (10.91% GC)
  maximum time:     42.673 ms (98.76% GC)
  --------------
  samples:          6659
  evals/sample:     1

julia> @benchmark join(c^n for c in s)
BenchmarkTools.Trial:
  memory estimate:  1.44 MiB
  allocs estimate:  36
  --------------
  minimum time:     475.977 μs (0.00% GC)
  median time:      516.176 μs (0.00% GC)
  mean time:        659.001 μs (10.36% GC)
  maximum time:     42.268 ms (98.41% GC)
  --------------
  samples:          7548
  evals/sample:     1

版本1.0.0(2018-08-08)中测试代码

当我试图编写
map(x->x^3,“123abc”)
时,我遇到了一个错误

julia> map(x -> x^3, "123abc")
ERROR: ArgumentError: map(f, s::AbstractString) requires f to return AbstractChar; try map(f, collect(s)) or a comprehension instead
所以,还有另一种方法

julia> map(x -> x^3, collect("123abc"))
6-element Array{String,1}:
 "111"
 "222"
 "333"
 "aaa"
 "bbb"
 "ccc"

julia> join(map(x -> x^3, collect("123abc")))
"111222333aaabbbccc"
而且,
重复
可能更方便

julia> repeat(collect("123abc"), inner=3)
18-element Array{Char,1}:
 '1'
 '1'
 '1'
 '2'
 '2'
 '2'
 '3'
 '3'
 '3'
 'a'
 'a'
 'a'
 'b'
 'b'
 'b'
 'c'
 'c'
 'c'
julia> join(repeat(collect("123abc"), inner=3))
"111222333aaabbbccc"

除了上面的答案,我还发现
string
函数运行得更快。以下是我的基准:

julia> n = 2;

julia> s = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

julia> string((c^n for c in s)...) # proof that it works
"AABBCCDDEEFFGGHHIIJJKKLLMMNNOOPPQQRRSSTTUUVVWWXXYYZZ"

julia> n = 26000;

julia> @benchmark join(c^n for c in s)
BenchmarkTools.Trial:
  memory estimate:  1.44 MiB
  allocs estimate:  36
  --------------
  minimum time:     390.616 μs (0.00% GC)
  median time:      425.861 μs (0.00% GC)
  mean time:        484.638 μs (6.54% GC)
  maximum time:     45.006 ms (98.99% GC)
  --------------
  samples:          10000
  evals/sample:     1

julia> @benchmark string((c^n for c in s)...)
BenchmarkTools.Trial:
  memory estimate:  1.29 MiB
  allocs estimate:  31
  --------------
  minimum time:     77.480 μs (0.00% GC)
  median time:      101.667 μs (0.00% GC)
  mean time:        126.455 μs (0.00% GC)
  maximum time:     832.524 μs (0.00% GC)
  --------------
  samples:          10000
  evals/sample:     1
如您所见,它比@Julia Learner提出的
join
解决方案快3倍左右。
我在0.7上测试了上述内容,但没有任何弃用警告,所以我假设它在1.0上也可以正常工作。甚至当硬编码
n=3
时,您也可以使用:
join(c^3表示“123abc”中的c)`生成
“111222333aaabbbccc”`。可能一个用户只有小字符串,但是有很多小字符串。好建议。
@benchmark
表达式可以在变量之前使用一些
$
,是吗?你也应该比较最小值而不是中间值。我发现
字符串((c^n代表s中的c)…)
比我的机器上的
加入
解决方案快4倍。如何在你的机器上运行
@benchmark foldl((x,y)->x*string(y)^$n,“,$s)
它以2.242毫秒(分钟)的速度运行,中值为4.1ms。我还收到了一个弃用警告。啊,我仍然在使用Julia v0.6.4(没有弃用警告),这似乎是目前为止这里介绍的方法中速度最快的。当我安装v0.7/v1.0时,我会玩更多。@niczky12喜欢它!你的解决方案很酷。作为Julia的新手,我甚至没有想过使用带有
省略号操作符的字符串。实际上,我想我先尝试了字符串,但是使用
string([c^n表示s中的c])
的结果非常糟糕。我不习惯省略号运算符。你如何从概念上看待它?为什么它的输出效果比
string([c^n代表s中的c])
好得多?一般来说,当运行时元素的数量(这里是字符)发生变化时,最好避免使用省略号(splatting),因为函数必须为每个特定数量的元素重新编译。所以它很快,但只有在不考虑编译时间的情况下。对于像这样的非常短的操作来说尤其如此,对于长时间运行的函数来说更是如此。Python代码不应该是
''。join(…)
?@phg是的,我一定在想Julia!我还将
c^n
错误,即Julia,编辑成
c*n
即Python。多谢。