Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects, and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Developer articles
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
pythoningLearning
python3-cookbook
Repository navigation
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
python3-cookbook
/
basic
/
mycore
/
generate.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
57 lines (45 loc) · 1.52 KB
master
Breadcrumbs
python3-cookbook
/
basic
/
mycore
/
generate.py
Copy path
Top
File metadata and controls
Code
Blame
57 lines (45 loc) · 1.52 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# encoding: utf-8
"""
Topic: 生成器表达式
Desc:
生成器是特定的函数,允许你返回一个值,然后'暂停'代码执行,稍后恢复。
列表解析的一个不足就是要一次性生成所有数据,对大量数据的迭代器是不好的
生成器表达式:生成器 + 列表解析,解决了这个问题
列表解析:
[expr for var in iterable if cond]
生成器表达式:
(expr for var in iterable if cond)
"""
__author__
=
'Xiong Neng'
def
my_generate
():
# 继续前面的例子,统计文件中非空字符的个数,sum()参数可以是列表,还可以是可迭代对象
# 统计文件中非空字符个数
f
=
open
(
'readme.txt'
,
'r'
)
f
.
seek
(
0
)
print
(
sum
(
len
(
word
)
for
line
in
f
for
word
in
line
.
split
()))
# 我们只是把括号删除:少了两个字节,而且更省内存 ... 灰常地环保
rows
=
[
1
,
2
,
3
,
16
]
x_pairs
=
((
i
,
j
)
for
i
in
rows
for
j
in
cols
())
print
(
type
(
x_pairs
))
print
(
x_pairs
)
for
pair
in
x_pairs
:
print
(
type
(
pair
),
pair
)
# 获取文件中最长的行的长度
# print(max(len(x.strip()) for x in open('/etc/motd')))
a
=
input
(
'input... '
)
print
(
type
(
a
))
def
cols
():
print
(
"in cols..."
)
yield
44
print
(
"in cols..."
)
yield
34
print
(
"in cols..."
)
yield
7
def
my_next
():
aa
=
cols
()
print
(
"start to invoke __next__() method.."
)
print
(
aa
.
__next__
())
print
(
aa
.
__next__
())
print
(
aa
.
__next__
())
if
__name__
==
"__main__"
:
my_next
()
You can’t perform that action at this time.