我想删除重音,将所有字符都改为小写,并删除任何数字和特殊字符.
示例:
Frédér8ic@ - >弗雷德里克
提案:
def remove_accents(data):
return ''.join(x for x in unicodedata.normalize('NFKD', data) if \
unicodedata.category(x)[0] == 'L').lower()
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法来做到这一点?
目标:将二进制转换为字符串
示例:0111010001100101011100110111010001100011011011110110010001100101 - > testCode(不含空格)
我使用字典和我的功能,我搜索更好的方式,更有效率
from textwrap import wrap
DICO = {'\x00': '00', '\x04': '0100', '\x08': '01000', '\x0c': '01100',
'\x10': '010000', '\x14': '010100', '\x18': '011000', '\x1c': '011100',
' ': '0100000', '$': '0100100', '(': '0101000', ',': '0101100', '0': '0110000',
'4': '0110100', '8': '0111000', '<': '0111100', '@': '01000000',
'D': '01000100', 'H': '01001000', 'L': '01001100', 'P': '01010000',
'T': '01010100', 'X': '01011000', '\\': '01011100', '`': '01100000',
'd': '01100100', 'h': '01101000', 'l': '01101100', 'p': '01110000',
't': '01110100', 'x': '01111000', '|': '01111100', '\x03': '011',
'\x07': …Run Code Online (Sandbox Code Playgroud) 为什么ctypes在我的代码中比纯python更慢以增加变量?
from ctypes import *
import timeit
def f1():
global t
t += 1
def f2():
p[0] += 1
t = 0
n = c_int(0)
p = pointer(n)
print(timeit.timeit("f1()", setup="from __main__ import f1")) # 0.3417885800008662
print(timeit.timeit("f2()", setup="from __main__ import f2")) # 0.5280102270189673
print(t) # 1000000
print(n.value) # 1000000
Run Code Online (Sandbox Code Playgroud)
如何用ctypes模块提高速度?
如何正确删除函数bin(x)结果中的'b'
例:
bin(ord("\'"))
-> '0b100111'
Run Code Online (Sandbox Code Playgroud)
但我想要这个结果
-> '0100111'
Run Code Online (Sandbox Code Playgroud)
提前谢谢,