在Python中查找介于0到100之间的值范围内的索引 [英] Finding an index in range of values between 0-100 in Python

查看:548
本文介绍了在Python中查找介于0到100之间的值范围内的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个分为两部分的问题,我必须通过列表中任意数量的整数的随机范围来选择2个索引.如果它们都在相同范围内,也无法返回

Selection1 = random.randint(0,100)
Selection2 = random.randint(0,100)

为了这个论点,请说:

Selection1 = 10
Selection2 = 17
And the list would be like so [25, 50, 75, 100]

两者都将返回索引0,因为它们介于0-25之间

因此两者都将落入第一个索引范围,问题是我在尝试将其调整到此范围(IE:0-25)时遇到了一些问题,该范围将返回该第一个索引(返回列表[0])/p>

在python中这种逻辑的语法是什么?

我敢肯定,如果它们属于同一范围,我可以弄清楚如何返回不同的索引,可能只是将循环重置为循环,但是如果我能得到一些建议的话,那将不会受到损害.

我将给出我现在正在使用的代码作为准则.主要是在我苦苦挣扎的地方.

此处编码

def roulette_selection(decimal_list, chromosome_fitness, population):
    percentages = []
    for i in range(population):
        result = decimal_list[i]/chromosome_fitness
        result = result * 100
        percentages.append(result)

    print(percentages)
    range_in_fitness = []
    current_percent = 0

    for i in range(population):
        current_percent = percentages[i] + current_percent
        range_in_fitness.append(current_percent)
    parent1 = random.randint(0, 100)
    parent2 = random.randint(0, 100)

    for i in range(population):
        if parent1 >= range_in_fitness[i] and parent1<=range_in_fitness[i+1]:


    print(parent1, parent2)
    print(range_in_fitness)

解决方案

如果您对范围列表进行了排序,或者可以对其进行排序,并且是连续的(没有空格),则可以使用Python的

链接的文档包括一组配方,这些配方用于使用bisect在排序列表中进行搜索.

This is a two part question, I have to make a selection of 2 indexes via a random range of any number of integers in a list. Can't return both if they're both in the same range as well

Selection1 = random.randint(0,100)
Selection2 = random.randint(0,100)

For the sake of this argument, say:

Selection1 = 10
Selection2 = 17
And the list would be like so [25, 50, 75, 100]

Both would return the index of 0 because they fall between 0-25

So both would fall into the first index range, the problem is i'm having some issues trying to fit it into this range (IE: 0-25) which will return this first index (return list[0])

What is the syntax for this type of logic in python?

I'm sure I can figure out how to return different indexes if they fall in the same range, probably just loop reset to the loop but if I can get some advice on that it wouldn't hurt.

I'll give the code i'm working with right now as a guideline. Mostly at the bottom is where i'm struggling.

Code Here

def roulette_selection(decimal_list, chromosome_fitness, population):
    percentages = []
    for i in range(population):
        result = decimal_list[i]/chromosome_fitness
        result = result * 100
        percentages.append(result)

    print(percentages)
    range_in_fitness = []
    current_percent = 0

    for i in range(population):
        current_percent = percentages[i] + current_percent
        range_in_fitness.append(current_percent)
    parent1 = random.randint(0, 100)
    parent2 = random.randint(0, 100)

    for i in range(population):
        if parent1 >= range_in_fitness[i] and parent1<=range_in_fitness[i+1]:


    print(parent1, parent2)
    print(range_in_fitness)

解决方案

If your list of ranges is sorted, or it is acceptable to sort it, and is contiguous (no gaps), you can use Python's bisect module to do this in an efficient manner. Example:

>>> l = [25, 50, 75, 100]
>>> import bisect
>>> bisect.bisect(l, 10)
0
>>> bisect.bisect(l, 17)
0
>>> bisect.bisect(l, 55)
2
>>> bisect.bisect(l, 25)
1

Bisect returns the index of where the input number should fall into the list to maintain sort order. Note that this is a little confusing to think about at first; In the case of 55 above, it returns 2 because it should be inserted at index 2 as it falls between the current values at indices 1 and 2. If you give it a number exactly on a range boundary, it will 'fall to the right', as evidenced by the bisect(l,25) example.

The linked documentation includes a set of recipes for searching through sorted lists using bisect.

这篇关于在Python中查找介于0到100之间的值范围内的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆