如何在Python中进行随机但部分洗牌?
我正在寻找 python 中的部分 shuffle
函数,而不是完整的shuffle
函数。
示例:“string”必须产生“stnrig”,但不能产生“nrsgit”
如果我可以定义必须重新排列的字符的特定“百分比”,那就更好了。
目的是测试字符串比较算法。我想确定“洗牌百分比”,超过该百分比,(我的)算法会将两个(洗牌后的)字符串标记为完全不同。
更新:
这是我的代码。欢迎改进!
import random
percent_to_shuffle = int(raw_input("Give the percent value to shuffle : "))
to_shuffle = list(raw_input("Give the string to be shuffled : "))
num_of_chars_to_shuffle = int((len(to_shuffle)*percent_to_shuffle)/100)
for i in range(0,num_of_chars_to_shuffle):
x=random.randint(0,(len(to_shuffle)-1))
y=random.randint(0,(len(to_shuffle)-1))
z=to_shuffle[x]
to_shuffle[x]=to_shuffle[y]
to_shuffle[y]=z
print ''.join(to_shuffle)
Instead of a complete shuffle
, I am looking for a partial shuffle
function in python.
Example : "string" must give rise to "stnrig", but not "nrsgit"
It would be better if I can define a specific "percentage" of characters that have to be rearranged.
Purpose is to test string comparison algorithms. I want to determine the "percentage of shuffle" beyond which an(my) algorithm will mark two (shuffled) strings as completely different.
Update :
Here is my code. Improvements are welcome !
import random
percent_to_shuffle = int(raw_input("Give the percent value to shuffle : "))
to_shuffle = list(raw_input("Give the string to be shuffled : "))
num_of_chars_to_shuffle = int((len(to_shuffle)*percent_to_shuffle)/100)
for i in range(0,num_of_chars_to_shuffle):
x=random.randint(0,(len(to_shuffle)-1))
y=random.randint(0,(len(to_shuffle)-1))
z=to_shuffle[x]
to_shuffle[x]=to_shuffle[y]
to_shuffle[y]=z
print ''.join(to_shuffle)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是一个比看起来更简单的问题。并且该语言拥有正确的工具,不会像往常一样阻碍您和想法:
This is a problem simpler than it looks. And the language has the right tools not to stay between you and the idea,as usual:
您的问题很棘手,因为需要考虑一些边缘情况:
无论如何,定义为将字符串打乱到一定百分比的指标可能与您在算法中使用的用于查看它们有多接近的指标相同。
我的洗牌
n
个字符的代码:基本上选择
n
个位置来随机交换,然后将每个位置与列表中的下一个交换...这样可以确保不会生成反向交换,并且恰好n
个字符被交换(如果有重复的字符,则运气不好)。解释以 'string', 3 作为输入运行:
此方法的缺点是它不会生成所有可能的变体,例如,它无法从 'string' 生成 'gnrits'。这可以通过对索引分区进行打乱来解决,如下所示:
Your problem is tricky, because there are some edge cases to think about:
In any case, the metric defined to shuffle strings up to a certain percentage is likely to be the same you are using in your algorithm to see how close they are.
My code to shuffle
n
characters:Basically chooses
n
positions to swap at random, and then exchanges each of them with the next in the list... This way it ensures that no inverse swaps are generated and exactlyn
characters are swapped (if there are characters repeated, bad luck).Explained run with 'string', 3 as input:
The bad thing about this method is that it does not generate all the possible variations, for example, it could not make 'gnrits' from 'string'. This could be fixed by making partitions of the indices to be shuffled, like this:
印刷
prints
邪恶并使用已弃用的 API:
Evil and using a deprecated API:
也许像这样:
借鉴 fortran 的想法,我将其添加到集合中。速度相当快:
maybe like so:
Taking from fortran's idea, i'm adding this to collection. It's pretty fast: