简体   繁体   English

Pythons最快的随机化字符串的方式

[英]Pythons fastest way of randomising case of a string

I want to randomise the case of a string, heres what I have: 我想随机化一个字符串的情况,继承我所拥有的:

word="This is a MixeD cAse stRing"
word_cap=''
for x in word:
        if random.randint(0,1):
                word_cap += x.upper()
        else:
                word_cap += x.lower()
        word = word_cap

print word

Im wondering if you could use list comprehension to make it faster. 我想知道你是否可以使用列表理解来加快速度。 I couldnt seem to use the lower() and upper() functions in randomchoice i tried to do something like 我似乎无法使用randomchoice中的lower()和upper()函数,我试图做类似的事情

''.join(randomchoice(x.upper(),x.lower()) for x in word)

but i think thats wrong. 但我认为那是错的。 something like that though is possible? 这样的事情虽然可能吗?

import random
s = 'this is a lower case string'

''.join(random.choice((str.upper,str.lower))(x) for x in s)

random.choice randomly selects one from two functions str.upper , str.lower . random.choice从两个函数str.upperstr.lower随机选择一个。

Then this function is applied to x for each letter in the input string s . 然后,此函数将应用于输入字符串s每个字母的x

If initial string has all the letters in lowercase, that I would use this code: 如果初始字符串包含所有小写字母,我将使用此代码:

''.join(x.upper() if random.randint(0,1) else x for x in s)

because the initial code would use redundant str.lowercase on half of the letters in the case of lowercase initial string. 因为在小写初始字符串的情况下,初始代码将在一半字母上使用冗余str.lowercase

By the way, look at the other answer by Michael J. Barber. 顺便说一下,请看Michael J. Barber的另一个答案。 Python have hefty charges for function calls. Python对函数调用收费很高。 In his code he calls str.upper only once. 在他的代码中,他只调用str.upper一次。 In my code str.upper is called for about half the symbols of the initial string. 在我的代码中, str.upper被调用大约一半的初始字符串符号。 So still a temporary upper-cased string is created in memory, the time efficiency of his code may be much greater. 因此,在内存中仍然会创建一个临时的大写字符串,他的代码的时间效率可能会更高。


Lo and behold: 瞧,看哪:

Code timing comparisons: https://ideone.com/eLygn 代码时序比较: https//ideone.com/eLygn

Try this: 尝试这个:

word="this is a lower case string"
caps = word.upper()
''.join(x[random.randint(0,1)] for x in zip(word, caps))

This should outperform your version because it makes many fewer calls to upper and because, more importantly, it avoids the O(N^2) successive appends you used in the version with the loops. 这应该胜过您的版本,因为它对upper的调用次数要少得多,因为更重要的是,它避免了在循环版本中使用的O(N ^ 2)连续追加。

With the modification to the question, you'll need to create both the lowercase and uppercase versions: 通过修改问题,您需要创建小写和大写版本:

word="This is a MixeD cAse stRing"
caps = word.upper()
lowers = word.lower()
''.join(random.choice(x) for x in zip(caps, lowers))

As suggested by Tim Pietzcker in the comments, I've used random.choice to select the letters from the tuples created by the zip call. 正如Tim Pietzcker在评论中所建议的那样,我使用了random.choice来选择zip调用创建的元组中的字母。

Since the question has been changed to focus more on speed, the fastest approach is likely to be using Numpy: 由于问题已经改变,更多地关注速度,最快的方法可能是使用Numpy:

''.join(numpy.where(numpy.random.randint(2, size=len(caps)), list(caps), list(lowers)))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM