gpt4 book ai didi

python - 将字符串的每个字母随机大写的代码(代码清理帮助)

转载 作者:行者123 更新时间:2023-12-03 15:27:01 25 4
gpt4 key购买 nike

关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。












想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。

10 个月前关闭。




Improve this question




我是一名初级编码员,我最近创建了一个函数,该函数接受一个字符串并随机将每个字母大写。

def rand_upper(string):
import random
strList = [l for l in string.lower()]
newList = []
for i in strList:
j = random.randrange(2)
if j == 1:
letter = i.upper()
newList.append(letter)
else:
newList.append(i)
return "".join(newList)
该代码按我的意图工作,但是有什么方法可以使此代码更清晰吗?我个人认为这很难阅读。有没有其他方法可以压缩代码以使其更具可读性/效率?谢谢!

最佳答案

使用 choice相反,并调用 lowerupper只有一次。

from random import choice

def rand_upper(string):
return ''.join(map(choice, zip(string.lower(), string.upper())))
更好的是,如 Peter commented :
def rand_upper(string):
return ''.join(map(choice, zip(string, string.swapcase())))
另一个,基于 Olvin_Roght's :
def rand_upper(string):
return ''.join([c if getrandbits(1) else c.swapcase() for c in string])
还有两个,混合我们迄今为止最快的解决方案:
def rand_upper(string):
return ''.join([c if getrandbits(1) else d
for c, d in zip(string, string.swapcase())])
def rand_upper(string):
return ''.join([z[getrandbits(1)] for z in zip(string, string.swapcase())])
使用 string = rand_upper('a' * 1000) 的基准测试:
739 μs  797 μs  725 μs  original
764 μs 787 μs 693 μs original_2
713 μs 691 μs 680 μs Samwise
699 μs 657 μs 682 μs theCoder
477 μs 486 μs 490 μs superb_rain
520 μs 476 μs 489 μs Peter_Wood
135 μs 131 μs 141 μs based_on_Olvin_Roght
120 μs 113 μs 121 μs superb_Peter_Olvin
125 μs 117 μs 118 μs superb_Peter_Olvin_2
(不包括 Olvin 的原始版本,因为它是唯一一个具有二次而非线性时间的版本,因此与单一尺寸的比较会产生误导。)
代码:
from timeit import repeat
from random import randrange, choice, getrandbits

def original(string):
import random
strList = [l for l in string.lower()]
newList = []
for i in strList:
j = random.randrange(2)
if j == 1:
letter = i.upper()
newList.append(letter)
else:
newList.append(i)
return "".join(newList)

def original_2(string):
strList = [l for l in string.lower()]
newList = []
for i in strList:
j = randrange(2)
if j == 1:
letter = i.upper()
newList.append(letter)
else:
newList.append(i)
return "".join(newList)

def Samwise(string: str) -> str:
return "".join(
c.upper() if randrange(2) else c.lower()
for c in string
)

def theCoder(string):
return ''.join(choice((str.upper, str.lower))(c) for c in string)

def superb_rain(string):
return ''.join(map(choice, zip(string.lower(), string.upper())))

def Peter_Wood(string):
return ''.join(map(choice, zip(string, string.swapcase())))

def based_on_Olvin_Roght(string):
return ''.join([c if getrandbits(1) else c.swapcase() for c in string])

def superb_Peter_Olvin(string):
return ''.join([c if getrandbits(1) else d for c, d in zip(string, string.swapcase())])

def superb_Peter_Olvin_2(string):
return ''.join([z[getrandbits(1)] for z in zip(string, string.swapcase())])

funcs = original, original_2, Samwise, theCoder, superb_rain, Peter_Wood, based_on_Olvin_Roght, superb_Peter_Olvin, superb_Peter_Olvin_2

string = original('a' * 1000)
number = 1000

tss = [[] for _ in funcs]
for _ in range(4):
for func, ts in zip(funcs, tss):
t = min(repeat(lambda: func(string), number=number)) / number
ts.append(t)
print(*('%d μs ' % (1e6 * t) for t in ts[1:]), func.__name__)
print()

关于python - 将字符串的每个字母随机大写的代码(代码清理帮助),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65186066/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com