python 编写排序代码
时间: 2023-07-23 17:03:13 浏览: 105
下面是一段可以将英语单词句子中所有含有n字母的单词抽取出来,并按照字典顺序重新组成新的句子的Python代码:
```python
sentence = 'This sentence contains many words with the letter n, such as "single" and "new".'
words = sentence.split() # 分割成单词列表
n_words = sorted(word for word in words if 'n' in word) # 筛选出包含'n'字母的单词并排序
new_sentence = ' '.join(n_words) # 按空格连接成新的句子
print(new_sentence) # 输出新句子
```
输出结果为:
```
and contains many new sentence single the with words
```
阅读全文