gpt4 book ai didi

ruby - 拆分由子字符串列表分隔的字符串

转载 作者:太空宇宙 更新时间:2023-11-03 17:13:46 26 4
gpt4 key购买 nike

我有这样的数据:

str = "CODEA text for first item CODEB text for next item CODEB2 some"\
"more text CODEC yet more text"

和一个列表:

arr = ["CODEA", "CODEB", "CODEB2", "CODEC", ... ]

我想把这个字符串分成一个散列。散列的键将是 CODEACODEB 等。散列的值将是后面的文本,直到下一个 CODE。输出应如下所示:

"CODEA" => "text for first item",
"CODEB" => "text for next item",
"CODEB2" => "some more text",
"CODEC" => "yet more text"

最佳答案

我们得到了一个字符串和一个数组。

str = "CODEA text for first item CODEB text for next item " + 
"CODEB2 some more text CODEC yet more text"

arr= %w|CODEC CODEB2 CODEA CODEB|
#=> ["CODEC", "CODEB2", "CODEA", "CODEB"]

这是获得所需哈希值的一种方法。

 str.split.
slice_before { |word| arr.include?(word) }.
map { |word, *rest| [word, rest.join(' ')] }.
to_h
#=> {"CODEA" =>"text for first item",
# "CODEB" =>"text for next item",
# "CODEB2"=>"some more text",
# "CODEC" =>"yet more text"}

参见 Enumerable#slice_before .

步骤如下。

a = str.split
#=> ["CODEA", "text", "for", "first", "item", "CODEB",
# "text", "for", "next", "item", "CODEB2", "some",
# "more", "text", "CODEC", "yet", "more", "text"]
b = a.slice_before { |word| arr.include?(word) }
#=> #<Enumerator:
# #<Enumerator::Generator:0x00005cbdec2b5eb0>:each>

我们可以看到将由该枚举器生成并通过将其转换为数组传递给 each_with_object 的 (4) 个元素(数组)。

b.to_a
#=> [["CODEA", "text", "for", "first", "item"],
# ["CODEB", "text", "for", "next", "item"],
# ["CODEB2", "some", "more", "text"],
# ["CODEC", "yet", "more", "text"]]

继续,

c = b.map { |word, *rest| [word, rest.join(' ')] }
#=> [["CODEA", ["text for first item"]],
# ["CODEB", ["text for next item"]],
# ["CODEB2", ["some more text"]],
# ["CODEC", ["yet more text"]]]
c.to_h
#=> {"CODEA"=>"text for first item",
# "CODEB"=>"text for next item",
# "CODEB2"=>"some more text",
# "CODEC"=>"yet more text"}

以下也许是更好的方法。

 str.split.
slice_before { |word| arr.include?(word) }.
each_with_object({}) { |(word, *rest),h|
h[word] = rest.join(' ') }

当我还是个 child 的时候,这可能是这样完成的。

last_word = ''
str.split.each_with_object({}) do |word,h|
if arr.include?(word)
h[word]=''
last_word = word
else
h[last_word] << ' ' unless h[last_word].empty?
h[last_word] << word
end
end

last_word 必须设置为 block 外的任何内容。

关于ruby - 拆分由子字符串列表分隔的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54559850/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com