代码之家  ›  专栏  ›  技术社区  ›  Julia Learner anothershrubery

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

  •  2
  • Julia Learner anothershrubery  · 技术社区  · 7 年前

    这个 question

    >>> s = '123abc'
    >>> n = 3
    >>> ''.join([c*n for c in s])
    '111222333aaabbbccc'
    

    你在茱莉亚会怎么做?

    编辑

    join([c^n for c in s]) 可以说是更简单的,而且对于任何语言来说都可能达到最简单的程度。

    另一方面,@niczky12已经证明,在 string join 功能实现。

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

    感谢@rickhg12hs推荐基准点。我学到了很多。

    3 回复  |  直到 7 年前
        1
  •  2
  •   niczky12 AleÅ¡ Kotnik    7 年前

    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
    

    正如你所看到的,它比 join @Julia Learner提出的解决方案。 我在0.7上测试了上述内容,但没有任何弃用警告,所以我假设它在1.0上也可以正常工作。偶数TIO says so .

        2
  •  2
  •   Julia Learner anothershrubery    7 年前

    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"
    

    对于小弦,速度上应该没有什么实际差别。

    编辑

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

    @rickhg12hs建议最好有基准。

    n = the number of times to repeat each character

    s = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" in each case

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

    记忆的估计并没有很大的不同。

    1n=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
    

    4n=26000,C=667 vs.G=516 s,G更快

    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
  •  1
  •   woclass    7 年前

    在中测试的代码 Version 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"
    

    也许吧 repeat 更方便。

    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"
    
    推荐文章