小编Par*_*dia的帖子

如何在 Python 中按特定数字自动增加重复值?

我希望对列表进行排序,然后重复项将以 0.1 的增量间隔。为什么我下面的代码不起作用?这是我期望得到的与我的程序返回的相比:

预期输出[11, 15, 15.1, 20, 20.1, 20.2, 20.3, 20.4, 30, 30.1, 40, 40.1, 50, 50.1]

实际输出[11, 15, 15.1, 20, 20.1, 20.1, 20.1, 20.1, 30, 30.1, 40, 40.1, 50, 50.1]

蟒蛇代码:

my_list = [20,20,20,30,20,30,40,50,15,11,20,40,50,15]
my_list.sort()
dup_list = []


for i in range (len(my_list)):
    if my_list[i] not in dup_list:
        dup_list.append(my_list[i])
    else:
        my_list[i] = my_list[i] + 0.10

    dup_list.append(my_list[i])
Run Code Online (Sandbox Code Playgroud)

python algorithm math python-2.7 python-3.x

2
推荐指数
1
解决办法
755
查看次数

标签 统计

algorithm ×1

math ×1

python ×1

python-2.7 ×1

python-3.x ×1