2).words = ["apple", "banana", "cherry", "date", "elephant", "grape"],编写一个列表生成式,生成一个新的列表,其中包含words中长度超过5个字符的单词
时间: 2023-11-01 18:07:19 浏览: 258
Laravel下生成验证码的类
答案:
你可以使用列表生成式来生成一个新的列表,其中包含words中长度超过5个字符的单词。以下是相应的代码:
```python
words = ["apple", "banana", "cherry", "date", "elephant", "grape"]
new_list = [word for word in words if len(word) > 5]
print(new_list)
```
输出结果为:['banana', 'cherry', 'elephant']
阅读全文