gpt4 book ai didi

ruby - 在 Ruby 中读取两个文件并输出结果

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

我有一个 name.txt 文件和 last.txt 文件。我想生成所有可能的名字和姓氏的组合。例如:

$cat name.txt
Jack
Jamie
James
Jarred
Josh
John
Jane


$cat last.txt
doe
smith

我试过这样做:

File.open("name.txt", "r") do |n|


File.open("last.txt", "r") do |l|
n.each_line do |first|
l.each_line do |last|
full_name = first.chomp + " " + last.chomp
puts full_name
end
end
end
end

输出仅显示它只处理名称文件的第一行:

Jack doe 
Jack smith

我怎样才能让它遍历整个第一个文件,为 name.txt 中的所有名称提供全名

最佳答案

考虑一下:

first = %w[jane john]
last = %w[doe smith]

first.product(last)
# => [["jane", "doe"], ["jane", "smith"], ["john", "doe"], ["john", "smith"]]

你可以这样做:

first = File.readlines('name.txt').map(&:rstrip)
last = File.readlines('last.txt').map(&:rstrip)
first.product(last)

product是 Array 的方法之一。另请参阅 permutationcombination .

我们可以使用chomp而不是 rstrip删除尾随的换行符,它将由 readlines 返回,但是 chomp 只修剪换行符,而 rstrip 将删除尾随的空格, 如果有任何尾随空格,稍微清理一下名称。 (根据我的经验,我们更有可能在文本之后看到空白,而不是在文本之前,因为它更容易在行首时看到。)


基准:

require 'fruity'

FIRST_NAME = [*'a'..'z']
LAST_NAME = [*'a'..'z']

FIRST_NAME.size # => 26
LAST_NAME.size # => 26

def use_product
FIRST_NAME.product(LAST_NAME)
end

def use_loops
output = []
FIRST_NAME.each do |fn|
LAST_NAME.each do |ln|
output << [fn, ln]
end
end
output
end

result = use_product
result.size # => 676
result.first # => ["a", "a"]
result.last # => ["z", "z"]

result = use_loops
result.size # => 676
result.first # => ["a", "a"]
result.last # => ["z", "z"]

运行结果:

compare :use_product, :use_loops
# >> Running each test 64 times. Test will take about 1 second.
# >> use_product is faster than use_loops by 50.0% ± 10.0%

如果源数组的大小增加:

require 'fruity'

FIRST_NAME = [*'a1'..'z9']
LAST_NAME = [*'a1'..'z9']

FIRST_NAME.size # => 259
LAST_NAME.size # => 259

def use_product
FIRST_NAME.product(LAST_NAME)
end

def use_loops
output = []
FIRST_NAME.each do |fn|
LAST_NAME.each do |ln|
output << [fn, ln]
end
end
output
end

result = use_product
result.size # => 67081
result.first # => ["a1", "a1"]
result.last # => ["z9", "z9"]

result = use_loops
result.size # => 67081
result.first # => ["a1", "a1"]
result.last # => ["z9", "z9"]

运行返回:

compare :use_product, :use_loops
# >> Running each test once. Test will take about 1 second.
# >> use_product is faster than use_loops by 60.00000000000001% ± 10.0%

虽然我们可以在不利用内置方法的情况下编写算法,但这些方法是用 C 语言编写的,因此请利用它们来提高速度。

有一段时间我会在内置的 product 上使用单独数组的迭代:如果我有两个巨大的列表,并且由于 RAM 限制导致可扩展性问题,将它们拉入内存是令人望而却步的,那么处理它的唯一方法就是嵌套循环。 Ruby's foreach is extremely fast ,因此围绕它编写代码将是一个很好的替代方案:

File.foreach('name.txt') do |first|
File.foreach('last.txt') do |last|
full_name = first.chomp + " " + last.chomp
puts full_name
end
end

关于ruby - 在 Ruby 中读取两个文件并输出结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33640999/

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