Tag: 数组

为什么array.index比array.include更快?

我正在处理一些大型数据集,并试图提高性能。 我需要确定对象是否包含在数组中。 我正在考虑使用index或include? 所以我对两者进行了基准测试。 require ‘benchmark’ a = (1..1_000_000).to_a num = 100_000 reps = 100 Benchmark.bmbm do |bm| bm.report(‘include?’) do reps.times { a.include? num } end bm.report(‘index’) do reps.times { a.index num } end end 令人惊讶的是(对我来说), index要快得多。 user system total real include? 0.330000 0.000000 0.330000 ( 0.334328) index 0.040000 0.000000 0.040000 ( 0.039812) 由于index提供的信息多于include? ,如果有的话,我原本预计它会稍慢一些,尽管事实并非如此。 […]

如何查找MASSIVE数组中的哪些项目不止一次出现?

这是一个非常简单的问题; 哪些项目不止一次出现在列表中? array = [“mike”, “mike”, “mike”, “john”, “john”, “peter”, “clark”] 正确答案是[“mike”, “john”] 。 好像我们可以这样做: array.select{ |e| ary.count(e) > 1 }.uniq 问题解决了。 可是等等! 如果arrays非常大,该怎么办: 1_000_000.times { array.concat(“1234567890abcdefghijklmnopqrstuvwxyz”.split(”)) } 碰巧我需要弄清楚如何在合理的时间内完成这项工作。 我们正在谈论数以百万计的记录。 对于它的价值,这个庞大的arrays实际上是10-20个较小arrays的总和。 如果比较那些比较容易,请告诉我 – 我很难过。 我们正在谈论每个文件10,000到10,000,000行,数百个文件。

如何返回具有重复元素的Ruby数组交集? (骰子系数中的双字母问题)

我正在尝试编写Dice的系数,但我对arrays交叉点有点问题。 def bigram(string) string.downcase! bgarray=[] bgstring=”%”+string+”#” bgslength = bgstring.length 0.upto(bgslength-2) do |i| bgarray << bgstring[i,2] end return bgarray end def approx_string_match(teststring, refstring) test_bigram = bigram(teststring) #.uniq ref_bigram = bigram(refstring) #.uniq bigram_overlay = test_bigram & ref_bigram result = (2*bigram_overlay.length.to_f)/(test_bigram.length.to_f+ref_bigram.length.to_f)*100 return result end 问题是,作为&删除重复,我得到这样的东西: string1=”Almirante Almeida Almada” string2=”Almirante Almeida Almada” puts approx_string_match(string1, string2) => 76.0% 它应该返回100。 uniq方法指出它,但有信息丢失,这可能会在我工作的特定数据集中带来不必要的匹配。 […]

如何在多维数组中找到最长的字符串?

我试图从多维数组中获取最长的字符串,但由于某种原因它不起作用。 这就是我所拥有的: a = [“MAKKGKPRPDHRPPAHNPHYAHDPPPYSQQQPPLQQQNYAQQMNRQHARPRPSPPSEVSDCVKYSLFLYNCIFWVSMHSS”], [“MHHGGGGGNRQHARPRPSPPSEVSDCVKYSLFLYNCIFWVSMHSS”], [“MTYINLGVTRTGDLMIGRHRP”], [“MRIYNRVCFQTAGCYLQNLVTTSIQPARVWTY”], [“MAACGLGRVFSAFKVDEMD”], [“MPSGFVGKRSGFCQFCCSHI”], [“MTYSTAKEGAYPFRNFN”]] p a.max_by{|v|v}[0] # => “MTYSTAKEGAYPFRNFN” 有人可以让我知道我做错了什么吗?

Ruby,创建没有现有键值的数组

我有一系列哈希; [{“price” => “123”, “amount” => “987”}, {“price” => “432”, “amount” => “13”}] 我想创建哈希数组; [[“123”, “987”], [“432”, “13”]] 我试过hash.map(&:first).map(&:last)和hash.map(&:first).map(&:last) 我无法实现我的目标。

如何通过段落或块将数据读入数组

我有一个文件,其中包含由空行分隔的文本块,如下所示: block 1 some text some text block 2 some text some text 如何将其读入数组?

如果它们具有相同的值,则在散列数组中求和值

我在这篇文章中看到了这段代码,因为我试图根据一些标准对哈希数组中的值求和。 Rails在哈希数组中求和值 array = [ {loading: 10, avg: 15, total: 25 }, {loading: 20, avg: 20, total: 40 }, {loading: 30, avg: 25, total: 55 } ] sum = Hash.new(0) array.each_with_object(sum) do |hash, sum| hash.each { |key, value| sum[key] += value } end # => {:loading=>60, :avg=>60, :total=>120} 我正在尝试做什么,我不知道如何,如果加载和avg在这个数组中出现多次相同的值,则总和为key。 例如。 array = [ {loading: 10, […]

有没有一种简单的方法可以在Ruby中生成有序的夫妻

我想知道是否有类似于Range的东西但不是整数但是有序的夫妻(x,y) 。 我想知道是否有一种简单的方法可以做这样的事情: ((1,2)..(5,6)).each {|tmp| puts tmp} #=> (1,2) (3,4) (5,6) 编辑 :也许我不是100%明确在我的问题:)我会尝试以不同的方式问它。 如果我有这些夫妇: (3,4)和(5,6)我正在寻找一种方法来帮助我产生: (3,4), (4,5), (5,6) 如果我不得不更好地开除它:如果夫妻是(x,y) – > (x0,y0), ((x0+1),(y0+1)), ((x0+2), (y0+2)) and so on .

如何将数组分解为字符串MVC?

如果current_user提交了一个习惯,那么他:committed这样做的日子就是这样: db t.text “committed”, default: [“sun”, “mon”, “tue”, “wed”, “thu”, “fri”, “sat”], array: true 习惯/ _form (用户选择天数) habit_controller habit_params :committed => [] 习惯/指数 如果一个nil用户提交了一个习惯(鼓励他在注册前创建一个习惯),那么他:committed这样做的日子是这样的: db t.text “committed”, default: [“sun”, “mon”, “tue”, “wed”, “thu”, “fri”, “sat”], array: true 习惯/ _form (用户选择天数) * habit_controller session[:habit_committed] = [params[“habit”][“committed”]] * users_controller committed = session.delete(:habit_committed) @user.habits.create(committed: committed) 习惯/指数 当两个习惯首次提交时,终端看起来像这样: Started […]

如何将整数转换为日期数组?

如果用户使用days_challenge: 5 & committed: [“mon”, “tue”, “wed”, “thu”, “fri”]创建挑战,那么我们如何从date_started: “2016-04-20″创建日期数组date_started: “2016-04-20″直到挑战的最后一天使用名为dates_challenged的模型方法? create_table “challenges”, force: true do |t| t.string “action” t.date “date_started” t.text “committed”, default: “—\n- sun\n- mon\n- tue\n- wed\n- thu\n- fri\n- sat\n” t.integer “days_challenged” end arrays看起来像这样: [“2016-04-20”, “2016-04-21”, “2016-04-22”, “2016-04-25”, “2016-04-26”] class Challenge = self.days_challenged 0 else self.days_challenged – ((date_started.to_date)..Date.yesterday).count do |date| committed_wdays.include? date.wday end […]