Skip to content
Navigation Menu
登录
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
操作
Automate any workflow
代码空间
Instant dev environments
问题
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced 安全
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
市场
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
安全
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub 赞助
Fund open source developers
PROGRAMS
安全 Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced 安全
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
搜索
/
登录
注册
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 }}
Uh oh!
There was an error while loading.
Please reload this page
.
python-cloud
/
python_test
Public
forked from
SergioJune/python_test
通知
You must be signed in to change notification settings
复刻
2
Star
0
Code
拉取请求
0
操作
项目
安全 and quality
0
洞察
Additional navigation options
Code
拉取请求
操作
项目
安全 and quality
洞察
Files
Expand file tree
master
Breadcrumbs
python_test
/
test8.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
76 lines (50 loc) · 1.79 KB
master
Breadcrumbs
python_test
/
test8.py
Copy path
Top
File metadata and controls
Code
Blame
76 lines (50 loc) · 1.79 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# 练习函数式编程的map和reduce
from functools import reduce
l = [x for x in range(-10, 10, 2)]
print(l)
l = list(map(abs, l))
print(l)
# reduce是将列表做累计运算
# lambda是匿名函数
num = reduce(lambda x, y: x + y, l)
print(num)
# 将一个字符串转换成整型
def str2int(s):
def char2int(ch):
c = {str(x): x for x in range(10)}
return c[ch]
def fun(n1, n2):
return n1*10 + n2
return reduce(fun, map(char2int, s))
num = str2int('12345')
print(type(num), num)
# 这是使用匿名函数的简便版
def str2int2(s):
ch = {str(x): x for x in range(10)}
return reduce(lambda x, y: x*10+y, map(lambda x: ch[x], s))
num = str2int2('12345')
print(type(num), num)
# 作业:利用map()函数,把用户输入的不规范的英文名字,变为首字母大写,其他小写的规范名字
def normalize(name):
# capitalize()函数是将字符串的首字母大写
return list(map(lambda x: x.capitalize(), name))
L = ['adam', 'LISA', 'barT']
print(normalize(L))
# 作业2:Python提供的sum()函数可以接受一个list并求和,请编写一个prod()函数,可以接受一个list并利用reduce()求积
def prod(x, y):
return x*y
num = reduce(prod, [3, 5, 7, 9])
print(num)
# 作业3:利用map和reduce编写一个str2float函数,把字符串'123.456'转换成浮点数123.456
def str2float(s):
ch = {str(x): x for x in range(10)}
# 先将字符串分割
L = s.split('.')
# 整数部分
n1 = reduce(lambda x, y: x*10 + y, map(lambda x: ch[x], L[0]))
# 小数部分
n2 = reduce(lambda x, y: x * 10 + y, map(lambda x: ch[x], L[1]))
n2 *= 0.1**len(L[1])
return n1+n2
num = str2float('123.456')
print(num)
You can’t perform that action at this time.