English 中文(简体)
如何从阵列中删除[]括号?
原标题:Ruby how to remove [] brackets from array?
  • 时间:2012-01-16 01:04:56
  •  标签:
  • ruby

我有这个阵列:

[[16], [14], [13], [17], [18], [15, 16], [15, 14], [15, 13], [15, 17], [15, 18], [16, 14], [16, 13], [16, 17], [16, 18], [14, 13], [14, 17], [14, 18], [13, 17], [13, 18], [17, 18], [15, 16, 14], [15, 16, 13], [15, 16, 17], [15, 16, 18], [15, 14, 13], [15, 14, 17], [15, 14, 18], [15, 13, 17], [15, 13, 18], [15, 17, 18], [16, 14, 13], [16, 14, 17], [16, 14, 18], [16, 13, 17], [16, 13, 18], [16, 17, 18], [14, 13, 17], [14, 13, 18], [14, 17, 18]]

我如何去掉一些阵列的括号[],这样阵列就象:

[16, 14, 13, 17, 18, [15, 16], ..., [14, 13, 18], [14, 17, 18]]
最佳回答

这不是非常奇怪的,但你会想到:

b = [[16], [14], [13], [17], [18], [15, 16], [15, 14], [15, 13], [15, 17], [15, 18], [16, 14], [16, 13], [16, 17], [16, 18], [14, 13], [14, 17], [14, 18], [13, 17], [13, 18], [17, 18], [15, 16, 14], [15, 16, 13], [15, 16, 17], [15, 16, 18], [15, 14, 13], [15, 14, 17], [15, 14, 18], [15, 13, 17], [15, 13, 18], [15, 17, 18], [16, 14, 13], [16, 14, 17], [16, 14, 18], [16, 13, 17], [16, 13, 18], [16, 17, 18], [14, 13, 17], [14, 13, 18], [14, 17, 18]]

b.collect { |c| c.count() ==  1 ? c[0] : c }
问题回答
new_arr = arr.collect { |a| a.size == 1 ? a[0] : a }

或者,内部:

arr.collect! { |a| a.size == 1 ? a[0] : a }

投稿人:

[1] pry(main)> arr = [[16], [14], [15, 16], [15, 14], [15, 16, 17], [15, 16, 18]]
=> [[16], [14], [15, 16], [15, 14], [15, 16, 17], [15, 16, 18]]
[3] pry(main)> new_arr = arr.collect { |a| a.size == 1 ? a[0] : a }
=> [16, 14, [15, 16], [15, 14], [15, 16, 17], [15, 16, 18]]
# Note that arr is unchanged at this point.
[5] pry(main)> arr.collect! { |a| a.size == 1 ? a[0] : a }
=> [16, 14, [15, 16], [15, 14], [15, 16, 17], [15, 16, 18]]
[6] pry(main)> arr
=> [16, 14, [15, 16], [15, 14], [15, 16, 17], [15, 16, 18]]

这个问题不明确。 你们似乎有一系列阵列,是的? 如果是的话,你可以这样做。

count = 0
array.each { |x|
  if x.is_a?(Array)
    if x.length == 1
      array[count] = x[0]
    end
  end
  count = count + 1
}

如果你想要把一个单列元素阵列用于单纯的愤怒的话,那可以做些什么。 然而,我并不完全相信你是否能够在鲁比做到这一点,但有一个阵列,构成一系列的愤怒和阵列,但我认为你可以(在 Java,你不能这样做)。





相关问题
Ruby parser in Java

The project I m doing is written in Java and parsers source code files. (Java src up to now). Now I d like to enable parsing Ruby code as well. Therefore I am looking for a parser in Java that parses ...

rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

multiple ruby extension modules under one directory

Can sources for discrete ruby extension modules live in the same directory, controlled by the same extconf.rb script? Background: I ve a project with two extension modules, foo.so and bar.so which ...

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

热门标签