首页 > 解决方案 > List.insert() 插入错误的索引

问题描述

我正在编写一个程序,我将在其中生成不同的数字并将它们组织到字典中的不同列表中。这是代码:

import random

Key = {}

for NumberOfTimesRanThroughTheMainLoop in range(0, 5): #Runs x times
    print("Number of times ran through main loop (EF): " + str(NumberOfTimesRanThroughTheMainLoop + 1) + "/" + str(5))
    for SeparatedContent in " Hohoho ": #SeparatedContent = the content i gave the def
        rn = random.randint(0, 3000-1) # Random number from 1 to 2999
        print("Number generated: " + str(rn))
        if SeparatedContent in Key: #I can't use insert if the list doesn't exist, so it verifies whether it exists and if it doesn't, create it
            #NumberOfTimesRanThroughTheMainLoop is correct
            print("This is _i: " + str(NumberOfTimesRanThroughTheMainLoop))
            Key[SeparatedContent].insert(NumberOfTimesRanThroughTheMainLoop, rn)
        else:
            Key[SeparatedContent] = [ ]
            Key[SeparatedContent].insert(NumberOfTimesRanThroughTheMainLoop, rn)
        print("Length of list of the key that's being generated (Comparison with what it should): " + str(len(Key[SeparatedContent])) + " = " + str(5) )
print("Generated key = " + str(Key))

这是输出

Number of times ran through main loop (EF): 1/5
Number generated: 1814
Length of list of the key that's being generated (Comparison with what it should): 1 = 5
Number generated: 281
Length of list of the key that's being generated (Comparison with what it should): 1 = 5
Number generated: 1011
Length of list of the key that's being generated (Comparison with what it should): 1 = 5
Number generated: 2641
Length of list of the key that's being generated (Comparison with what it should): 1 = 5
Number generated: 2532
This is _i: 0
Length of list of the key that's being generated (Comparison with what it should): 2 = 5
Number generated: 695
This is _i: 0
Length of list of the key that's being generated (Comparison with what it should): 2 = 5
Number generated: 2497
This is _i: 0
Length of list of the key that's being generated (Comparison with what it should): 3 = 5
Number generated: 955
This is _i: 0
Length of list of the key that's being generated (Comparison with what it should): 2 = 5
Number of times ran through main loop (EF): 2/5
Number generated: 1042
This is _i: 1
Length of list of the key that's being generated (Comparison with what it should): 3 = 5
Number generated: 1542
This is _i: 1
Length of list of the key that's being generated (Comparison with what it should): 2 = 5
Number generated: 720
This is _i: 1
Length of list of the key that's being generated (Comparison with what it should): 4 = 5
Number generated: 1168
This is _i: 1
Length of list of the key that's being generated (Comparison with what it should): 3 = 5
Number generated: 2367
This is _i: 1
Length of list of the key that's being generated (Comparison with what it should): 5 = 5
Number generated: 1207
This is _i: 1
Length of list of the key that's being generated (Comparison with what it should): 4 = 5
Number generated: 1384
This is _i: 1
Length of list of the key that's being generated (Comparison with what it should): 6 = 5
Number generated: 2811
This is _i: 1
Length of list of the key that's being generated (Comparison with what it should): 4 = 5
Number of times ran through main loop (EF): 3/5
Number generated: 1830
This is _i: 2
Length of list of the key that's being generated (Comparison with what it should): 5 = 5
Number generated: 2710
This is _i: 2
Length of list of the key that's being generated (Comparison with what it should): 3 = 5
Number generated: 339
This is _i: 2
Length of list of the key that's being generated (Comparison with what it should): 7 = 5
Number generated: 775
This is _i: 2
Length of list of the key that's being generated (Comparison with what it should): 5 = 5
Number generated: 1728
This is _i: 2
Length of list of the key that's being generated (Comparison with what it should): 8 = 5
Number generated: 2011
This is _i: 2
Length of list of the key that's being generated (Comparison with what it should): 6 = 5
Number generated: 88
This is _i: 2
Length of list of the key that's being generated (Comparison with what it should): 9 = 5
Number generated: 442
This is _i: 2
Length of list of the key that's being generated (Comparison with what it should): 6 = 5
Number of times ran through main loop (EF): 4/5
Number generated: 350
This is _i: 3
Length of list of the key that's being generated (Comparison with what it should): 7 = 5
Number generated: 516
This is _i: 3
Length of list of the key that's being generated (Comparison with what it should): 4 = 5
Number generated: 1885
This is _i: 3
Length of list of the key that's being generated (Comparison with what it should): 10 = 5
Number generated: 2176
This is _i: 3
Length of list of the key that's being generated (Comparison with what it should): 7 = 5
Number generated: 1242
This is _i: 3
Length of list of the key that's being generated (Comparison with what it should): 11 = 5
Number generated: 1369
This is _i: 3
Length of list of the key that's being generated (Comparison with what it should): 8 = 5
Number generated: 1332
This is _i: 3
Length of list of the key that's being generated (Comparison with what it should): 12 = 5
Number generated: 705
This is _i: 3
Length of list of the key that's being generated (Comparison with what it should): 8 = 5
Number of times ran through main loop (EF): 5/5
Number generated: 1091
This is _i: 4
Length of list of the key that's being generated (Comparison with what it should): 9 = 5
Number generated: 34
This is _i: 4
Length of list of the key that's being generated (Comparison with what it should): 5 = 5
Number generated: 1325
This is _i: 4
Length of list of the key that's being generated (Comparison with what it should): 13 = 5
Number generated: 377
This is _i: 4
Length of list of the key that's being generated (Comparison with what it should): 9 = 5
Number generated: 1861
This is _i: 4
Length of list of the key that's being generated (Comparison with what it should): 14 = 5
Number generated: 1593
This is _i: 4
Length of list of the key that's being generated (Comparison with what it should): 10 = 5
Number generated: 1758
This is _i: 4
Length of list of the key that's being generated (Comparison with what it should): 15 = 5
Number generated: 2248
This is _i: 4
Length of list of the key that's being generated (Comparison with what it should): 10 = 5
Generated key = {' ': [955, 2811, 442, 705, 2248, 1091, 350, 1830, 1042, 1814], 'H': [281, 1542, 2710, 516, 34], 'o': [2497, 1384, 88, 1332, 1758, 1861, 1325, 1242, 1885, 1728, 339, 2367, 720, 2532, 1011], 'h': [695, 1207, 2011, 1369, 1593, 377, 2176, 775, 1168, 2641]}

正如你所看到的,'h'、'o' 和 '' 在他们的列表中有 10 到 15 个数字,这是他们应该的数字数量的 2 到 3 倍,但是 _i(我忘了改变它!_i 代表变量 NumberOfTimesRanThroughTheMainLoop,对不起!),插入函数的索引,没有超过 4,如图所示,那么为什么他们在索引 9 和 14 上得到数字,我该如何解决呢?

提前致谢

注意:我编辑了完整的帖子以使其更具可读性

标签: pythonlistdictionary

解决方案


代码正常工作。

'h' 有 10 个字符的原因是因为你有两个 'h' 字符。因为它们具有相同的键,所以这些数字被附加到同一个列表中。'o' 和 ' ' 也是如此。

这是一个循环如何在一次迭代中工作的示例,给定字符串“ Hohoho ”(我正在添加一些随机数):

' ' -> [354]
'H' -> [234]
'o' -> [453]
'h' -> [342]
'o' (again) -> [453, 123] # now it has two elements when it should have 1
'h' -> [342, 633] # 2 elements as well now
'o' -> [453, 123, 1423] # now it has 3 elements
' ' -> [354, 562] # this has 3 elements now

如您所见,预期的附加数字是字母数乘以循环运行的次数。

如果您只想为唯一字符创建数字,您可以首先确保content变量是唯一的(即仅使用唯一字母)。在循环之前替换内容变量:

content = ''.join(dict.fromkeys(content))
# content is now ' Hoh' which will give correct results

如果您不关心字母的顺序,您也可以简化:

content = list(set(content))

推荐阅读