@sevenup233
2017-08-04T04:16:45.000000Z
字数 3067
阅读 1016
PY
网址
https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000
1.
def normalize(name):
return str.capitalize(name)
2.
def cheng(x,y):
return x*y
return reduce(cheng,L)
3.
n = len(s)
a = -1
for i in range(n):
a += 1
if s[i] == '.':
break
#‘a’为小数点位置
def fn(x, y):
return x * 10 + y
def char2num(s):
return {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}[s]
#基本函数
s1 = ''
s2 = ''
for i in range(a):
s1 = s1 + s[i]
for i in range(n-a-1):
s2 = s2 + s[i+a+1]
#重建无小数点数
S = s1 + s2
return reduce(fn, map(char2num, S))/(10**a)
1.
L = str(n)
a = len(L)
b = 0
for i in range(a):
if L[i] == L[a-i-1]:
b += 1
return a == b
#原来切片能这么玩
a = str(n)
b = a[::-1]
return a == b
1.
for i in t:
if isinstance(i,str):
return i.lower()
2.
def by_score(t):
for i in t:
if isinstance(i,int):
return(i)
L2 = sorted(L, key=by_score,reverse=True)
@property
def width(self):
return self._width
@width.setter
def width(self,value):
self._width = value
@property
def height(self):
return self._height
@height.setter
def height(self,value):
self._height = value
@property
def resolution(self):
return self._width*self._height
dt_dt = datetime.strptime(dt_str, '%Y-%m-%d %H:%M:%S')
tz_match = re.match('^UTC([+-])(\d+):(\d+)', tz_str)
tz = int(tz_match.group(1)+str(int(tz_match.group(2))))
tz_utc_aim = timezone(timedelta(hours=tz))
aim_dt = dt_dt.replace(tzinfo=tz_utc_aim)
dt_ts = aim_dt.timestamp()
return dt_ts
if isinstance(s,str) == True:
a = len(s) % 4
if a == 0:
return base64.b64decode(s)
else:
return base64.b64decode(s + '='*(4-a))
else:
s_str = s.decode('ascii')
b = len(s_str) % 4
if b == 0:
return base64.b64decode(s_str)
else:
return base64.b64decode(s_str + '='*(4-b))
#原来bytes可以这么玩
return base64.b64decode(s + b'=' * (4 - len(s) % 4))
#原来join可以这么玩
d = [s]
m = len(s) % 4
for i in range(m):
d.append(b'=')
return base64.b64decode(b''.join(d))
1.密码 -> MD5
import hashlib
def calc_md5(password):
md5 = hashlib.md5()
md5.update(password.encode('utf-8'))
return md5.hexdigest()
2.验证密码
import hashlib
db = {
'michael': 'e10adc3949ba59abbe56e057f20f883e',
'bob': '878ef96e86145580c38c87f0410ad153',
'alice': '99b1c2188db85afee403b1536010c2c9'
}
def login(user,password):
md5 = hashlib.md5()
md5.update(password.encode('utf-8'))
pw_md5 = md5.hexdigest()
if pw_md5 == db[user]:
print('Ture')
else:
print('False')
3.加盐加用户名
import hashlib
db = {}
def register(user, password):
safe_password = password + user + 'the-Salt'
md5 = hashlib.md5()
md5.update(safe_password.encode('utf-8'))
get_md5 = md5.hexdigest()
db[user] = get_md5
def login(user,password):
safe_password = password + user + 'the-Salt'
md5 = hashlib.md5()
md5.update(safe_password.encode('utf-8'))
pw_md5 = md5.hexdigest()
if pw_md5 == db[user]:
print('Ture')
else:
print('False')
conn = sqlite3.connect(db_file)
cursor = conn.cursor()
cursor.execute('select * from user')
values = cursor.fetchall()
cursor.close()
conn.close()
name = []
def by_score(t):
for i in t:
if isinstance(i,int):
return(i)
L = sorted(values, key=by_score)
for i in range(len(L)):
if isinstance(i,int) and low <= L[i][2] <= high:
name.append(L[i][1])
return name
#原来SQL有这种操作
connect = sqlite3.connect(db_file)
cursor_score = connect.cursor()
cursor_score.execute('select name from user where score>=? and score<=? order by score', (low, high))
values = cursor_score.fetchall()
cursor_score.close()
connect.close()
return [value[0] for value in values]