我有一个数组word_array
,我想在其中添加句子的所有单词.例如:
word_array = [] sentence_1 = "my first sentence" sentence_2 = "my second sentence"
然后有:
word_array = ["my", "first", "sentence", "my", "second", "sentence"]
如果我使用split()
:
word_array << sentence_1.split word_array << sentence_2.split
我明白了:
word_array = [["my", "first", "sentence"], ["my", "second", "sentence"]]
我怎样才能避免在这里使用2D阵列?
使用concat
.
word_array.concat(sentence_1.split) word_array.concat(sentence_2.split)
它比使用更有效+
,这使得一个新的数组.