查找 ruby​​ 数组中的整数 (Fixnum) 值

发布于 2024-11-03 11:54:07 字数 1228 浏览 1 评论 0原文

我有一个带有

  • 纯整数的数组 [1, 2, "3", "4", "1a", "abc", "a"] (1, < code>2)、
  • 字符串格式的整数 ("1""2")、
  • 字符串 ("a"、<代码>“b”),以及
  • 混合字符串数字(<代码>“1a”,<代码>“2s”)。

由此,我只需要选取整数(包括字符串格式) 12"3""4 “

首先,我尝试使用 to_i

arr = [1, 2, "3", "4", "1a", "abc", "a"]
arr.map {|x| x.to_i}
# => [1, 2, 3, 4, 1, 0, 0]

但是这个将 "1a" 转换为 1,这是我不期望的。

然后我尝试了Integer(item)

arr.map {|x| Integer(x) }  # and it turned out to be
# => ArgumentError: invalid value for Integer(): "1a"

现在我在这里没有直接转换选项。最后,我决定这样做,将值 to_ito_s 转换。所以 "1" == "1".to_i.to_s 是一个整数,但不是 "1a" == "1a".to_i.to_s" a" == "a".to_i.to_s

arr  = arr.map do |x|
  if (x == x.to_i.to_s)
    x.to_i
  else
    x
  end
end

现在

ids, names= arr.partition { |item| item.kind_of? Fixnum }

我得到了整数和字符串数组。有没有一种简单的方法可以做到这一点?

I have an array [1, 2, "3", "4", "1a", "abc", "a"] with

  • pure integers (1, 2),
  • string formatted integers ("1", "2"),
  • strings ("a", "b"), and
  • mixed string numbers ("1a", "2s").

From this, I need to pick up only the integers (including string formatted) 1, 2, "3", "4".

First I tried with to_i:

arr = [1, 2, "3", "4", "1a", "abc", "a"]
arr.map {|x| x.to_i}
# => [1, 2, 3, 4, 1, 0, 0]

but this one converts "1a" to 1, which I don't expect.

Then I tried Integer(item):

arr.map {|x| Integer(x) }  # and it turned out to be
# => ArgumentError: invalid value for Integer(): "1a"

Now I am out of straight conversion options here. Finally, I decided to do this way, which converts the value to_i and to_s. So "1" == "1".to_i.to_s is an integer, but not "1a" == "1a".to_i.to_s and "a" == "a".to_i.to_s

arr  = arr.map do |x|
  if (x == x.to_i.to_s)
    x.to_i
  else
    x
  end
end

and

ids, names= arr.partition { |item| item.kind_of? Fixnum }

Now I got the arrays of integers and strings. Is there a simple way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

清旖 2024-11-10 11:54:07

与 @maerics 提供的类似解决方案,但更精简:

arr.map {|x| Integer(x) rescue nil }.compact

Similar solution as provided by @maerics, but a bit slimmer:

arr.map {|x| Integer(x) rescue nil }.compact
怀中猫帐中妖 2024-11-10 11:54:07
class Array
  def to_i
    self.map {|x| begin; Integer(x); rescue; nil; end}.compact
  end
end

arr = [1, 2, "3", "4", "1a", "abc", "a"]
arr.to_i # => [1, 2, 3, 4]
class Array
  def to_i
    self.map {|x| begin; Integer(x); rescue; nil; end}.compact
  end
end

arr = [1, 2, "3", "4", "1a", "abc", "a"]
arr.to_i # => [1, 2, 3, 4]
相权↑美人 2024-11-10 11:54:07

像这样的东西:

a = [1,2,"3","4","1a","abc","a"]



irb(main):005:0> a.find_all { |e| e.to_s =~ /^\d+$/ }.map(&:to_i)
=> [1, 2, 3, 4]

something like this:

a = [1,2,"3","4","1a","abc","a"]



irb(main):005:0> a.find_all { |e| e.to_s =~ /^\d+$/ }.map(&:to_i)
=> [1, 2, 3, 4]
匿名。 2024-11-10 11:54:07

嘿,谢谢唤醒我的红宝石。这是我解决这个问题的方法:

arr=[1,2,"3","4","1a","abc","a"]
arr.map {|i| i.to_s}.select {|s| s =~ /^[0-9]+$/}.map {|i| i.to_i}
//=> [1, 2, 3, 4]

Hey, thanks awakening my ruby. Here is my go at this problem:

arr=[1,2,"3","4","1a","abc","a"]
arr.map {|i| i.to_s}.select {|s| s =~ /^[0-9]+$/}.map {|i| i.to_i}
//=> [1, 2, 3, 4]
蓝眼睛不忧郁 2024-11-10 11:54:07

我注意到到目前为止,大多数答案都将“3”和“4”的值更改为实际整数。

>> array=[1, 2, "3", "4", "1a", "abc", "a", "a13344a" , 10001, 3321]
=> [1, 2, "3", "4", "1a", "abc", "a", "a13344a", 10001, 3321]
>> array.reject{|x| x.to_s[/[^0-9]/] }
=> [1, 2, "3", "4", 10001, 3321]

@OP,我还没有彻底测试我的解决方案,但到目前为止它似乎有效(当然它是根据提供的示例完成的),所以请自己彻底测试。

I noticed most of the answer so far changes the value of "3" and "4" to actual integers.

>> array=[1, 2, "3", "4", "1a", "abc", "a", "a13344a" , 10001, 3321]
=> [1, 2, "3", "4", "1a", "abc", "a", "a13344a", 10001, 3321]
>> array.reject{|x| x.to_s[/[^0-9]/] }
=> [1, 2, "3", "4", 10001, 3321]

@OP, I have not tested my solution exhaustively, but so far it seems to work (of course its done according to provided sample ), so please test thoroughly yourself.

甜味拾荒者 2024-11-10 11:54:07

这个怎么样?

[1,2,"3","4","1a","abc","a"].select{|x| x.to_i.to_s == x.to_s}
# => [1, 2, "3", "4"]

How about this?

[1,2,"3","4","1a","abc","a"].select{|x| x.to_i.to_s == x.to_s}
# => [1, 2, "3", "4"]
梦中楼上月下 2024-11-10 11:54:07

看起来很简单

arr.select{ |b| b.to_s =~ /\d+$/ }
# or
arr.select{ |b| b.to_s[/\d+$/] }
#=> [1, 2, "3", "4"]

Looks pretty simple

arr.select{ |b| b.to_s =~ /\d+$/ }
# or
arr.select{ |b| b.to_s[/\d+$/] }
#=> [1, 2, "3", "4"]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文