1.Python练习之生成内推码并提取两个字符(sample)
-------->题目要求:
生成1000个内推码,并随机提取出两个字符
-------->代码如下:
import random
import string
code_str = string.ascii_letters + string.digits
# print(code_str)
def gen_code(len=4):
return ''.join(random.sample(code_str,len))
print([gen_code(len=6) for i in range(1000)])
print(random.sample(code_str,2))
#默认参数,传个参数,sample表示随机取出来几个
-------->结果如下:
2.Python练习之将str类型转换成float类型(使用reduce函数)
from functools import reduce
li = '123.456'
print('原数据:',li,type(li))
def Fun(x, y):
return x * 10 + y
def Float(s):
n = s.index('.')
s1 = list(map(int,[x for x in s[:n]]))
s2 = list(map(int,[x for x in s[n+1:]]))
num = reduce(Fun,s1) + reduce(Fun,s2)/(10 ** len(s2))
return num
a = Float(li)
print('转化后:',a,type(a))