开发者

How can I run a loop against 2 random elements from a list at a time?

开发者 https://www.devze.com 2022-12-18 17:34 出处:网络
Let\'s say I have a list开发者_高级运维 in python with several strings in it.I do not know the size.How can I run a loop to do an operation on 2 random elements of this string?

Let's say I have a list开发者_高级运维 in python with several strings in it. I do not know the size. How can I run a loop to do an operation on 2 random elements of this string?

What if I wanted to favour a certain subset of the strings in this randomization, to be selected more often, but still make it possible for them to not be chosen?


you need to look into random module. It has for example a random.choice function that lets you select a random element from a sequence or a random.sample that selects given number of samples, it's easy to account for different weights too.


explain better your problem, what operations and what elements you're focusing?

regarding the problem with the elements beeing chosen more often, give each string an "chance multiplier", each comparison you multiply a number between 1 and 10 and the chance multiplyer of the string, if the result is higher than X (say... 5), so it select the string, if not, it searches for another string. this way, strings with higher multipliers will have more chance to be selected

0

精彩评论

暂无评论...
验证码 换一张
取 消