map reduce 用法 str处理lower() capitalize()

本文介绍了Python中的一些实用编程技巧,包括字符串处理、列表操作以及数学计算等。通过具体示例展示了如何使用内置函数进行字符串大小写规范化,如何利用列表推导式进行数值计算,以及如何实现字符串到浮点数的转换。

--

1 s='123456'
2 l={'0':0,'1':1,'2':2,'3':3,'4':4,'5':5,'6':6,'7':7,'8':8,'9':9}[s[0]]
3 print(l)

取出dic里面key的元素

 

1 def normalize(name):
2     tempn=name.lower().capitalize()
3     return tempn
4 L1 = ['adam', 'LISA', 'barT']
5 L2 = list(map(normalize, L1))
6 print(L2)

只大写第一个字母,其余小写  其中用到 字符串 lower()  capitalize()  函数

 

1 ##sum
2 num1=[20,30,50]
3 print(sum(num1[1:]))
4 ##multiply
5 def prod(L):
6     return  reduce(lambda x,y:x*y ,L)
7 L3=[1,2,3,4,6]
8 print(prod(L3[:2]))
9 print(1*2)

 

List 求和和求积函数    其中用到  reduce lambda表达式

1 def str2float(s):
2     tempstr=s.split('.')
3     ustr=tempstr[0]+tempstr[1]
4     def str2num(r):
5         return {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}[r]
6     ws=len(tempstr[1])
7     return reduce(lambda x, y: x * 10 + y, map(str2num, ustr))/(10**ws)

 

--str转换float函数。 思路去掉小数点,转换成整数,然后除以小数点的位数。

转载于:https://www.cnblogs.com/anloveslife/p/7504746.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值