提交 bd0e413c 编写于 作者: HansBug's avatar HansBug 😆

dev(hansbug): refactor tuple generator as iter generator

上级 c1417c8d
......@@ -2,10 +2,10 @@
"export": [
"list01.json",
"list02.json",
"tuple.json",
"iter.json",
"dict.json"
],
"keywords": [],
"children": [],
"node_id": "python-3-17"
}
\ No newline at end of file
}
{
"author": "huanhuilong, HansBug",
"source": "iter.md",
"depends": [],
"exercise_id": 190,
"type": "code_options"
}
\ No newline at end of file
# Python 生成器推导式
Python 独步天下的推导式表达式,使用元表推式过滤长度小于等于4的书籍
```python
def test():
books = ('程序员修炼之道', '构建之法', '代码大全', 'TCP/IP协议详解')
# 此处请为reading进行正确的赋值
print("太长的书就不看了,只读短的:")
for book in reading:
print(" ->《{}》".format(book))
print("可是发现书的名字短,内容也可能很长啊!")
if __name__ == '__main__':
test()
```
请选出下列能**正确**实现这一功能的选项。
## 答案
```python
reading (book for book in books if len(book) <= 4)
```
## 选项
### A
```python
reading = (books[i] for book in books: if len(book) < 4)
```
### B
```python
reading = (book for book in books if len(book) < 4)
```
### C
```python
reading = (book for book in books: if len(book) <= 4)
```
{
"one_line": {
"if len(book) <= 4": [
"if len(book) < 4"
],
"for book in books": [
"for book in books:"
],
"book for book": [
"books[i] for book"
]
},
"source": "tuple.py",
"depends": [],
"exercise_id": 194,
"type": "code_options"
}
\ No newline at end of file
# -*- coding: UTF-8 -*-
# 作者:huanhuilong
# 标题:Python 元组推导式
# 描述:Python 独步天下的推导式表达式,使用元表推式过滤长度小于等于4的书籍
def test():
books = ('程序员修炼之道', '构建之法', '代码大全', 'TCP/IP协议详解')
reading = (book for book in books if len(book) <= 4)
print("太长的书就不看了,只读短的:")
for book in reading:
print(" ->《{}》".format(book))
print("可是发现书的名字短,内容也可能很长啊!")
if __name__ == '__main__':
test()
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册