将一组单词存储在一个数组中,按正序和倒序分别显示单词

<script type="text/javascript">
/*将一组单词存储在一个数组中,按正序和倒序分别显示单词*/
var words=["hello","my","world","this","is","seperate"];
words.sort();
console.log(words);
words.reverse();
console.log(words);
</script>
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以帮你定义一个类WORD,用于将英语句子中的单词取出来存放到二维数组中。以下是一个简单的实现: ```python class WORD: def __init__(self, sentence): self.sentence = sentence self.words = self.get_words() def get_words(self): # 将句子中的标点符号替换为空格,然后用空格分割单词 words = self.sentence.replace(",", " ").replace(".", " ").split() # 将单词存储到二维数组中 word_array = [[words[i]] for i in range(len(words))] return word_array ``` 在这个类中,我们首先定义了一个构造函数,用于初始化类的属性。其中,`sentence` 表示英语句子,`words` 表示存储单词的二维数组。 接着,我们定义了一个 `get_words` 方法,用于将英语句子中的单词取出来存放到二维数组中。具体实现过程如下: 1. 将句子中的标点符号替换为空格,然后用空格分割单词,得到一个单词列表 `words`; 2. 将单词存储到二维数组中,其中每个单词作为一个子列表的唯一元素。 这样,我们就可以通过调用 `WORD` 类的实例的 `words` 属性来获取存储单词的二维数组了。例如: ```python my_sentence = "I have a dream that one day this nation will rise up and live out the true meaning of its creed." my_word = WORD(my_sentence) print(my_word.words) ``` 输出结果为: ``` [['I'], ['have'], ['a'], ['dream'], ['that'], ['one'], ['day'], ['this'], ['nation'], ['will'], ['rise'], ['up'], ['and'], ['live'], ['out'], ['the'], ['true'], ['meaning'], ['of'], ['its'], ['creed']] ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值