【问题标题】:Why does using list.index(element) to update element at index not return proper index value?为什么使用 list.index(element) 更新索引处的元素不会返回正确的索引值?
【发布时间】:2021-03-31 03:46:55
【问题描述】:
test = [1,2,3,4,5,6,7,8,9,10]
for t in test:
    index = test.index(t)
    print(str(t)+ ' Index: '+ str(test.index(t)))
    print('\t\t'+str(test[test.index(t)]) + ' Index: ' + str(test.index(t)))
    test[index] = test[index] + 1
print(test)
Outputs:
1 Index: 0
        1 Index: 0
2 Index: 0
        2 Index: 0
3 Index: 0
        3 Index: 0
4 Index: 0
        4 Index: 0
5 Index: 0
        5 Index: 0
6 Index: 0
        6 Index: 0
7 Index: 0
        7 Index: 0
8 Index: 0
        8 Index: 0
9 Index: 0
        9 Index: 0
10 Index: 0
        10 Index: 0
[11, 2, 3, 4, 5, 6, 7, 8, 9, 10]

由于某种原因,索引值 test.index(t) 每次遍历测试列表时都是 0? 但如果我要创建自己的索引跟踪变量:

test = [1,2,3,4,5,6,7,8,9,10]
index = 0
for t in test:
    print(str(t)+ ' Index: '+ str(test.index(t)))
    print('\t\t'+str(test[index]) + ' Index: ' + str(index))
    test[index] = test[index] + 1
    index += 1
print(test)
1 Index: 0
        1 Index: 0
2 Index: 0
        2 Index: 1
3 Index: 1
        3 Index: 2
4 Index: 2
        4 Index: 3
5 Index: 3
        5 Index: 4
6 Index: 4
        6 Index: 5
7 Index: 5
        7 Index: 6
8 Index: 6
        8 Index: 7
9 Index: 7
        9 Index: 8
10 Index: 8
        10 Index: 9
[2, 3, 4, 5, 6, 7, 8, 9, 10, 11]

为什么 list.index(element) 不能正常工作。我完全不知道为什么这不起作用。意思是看起来合乎逻辑,我抓取元素的索引,然后将其更新 1。

【问题讨论】:

    标签: python python-3.x list indexing


    【解决方案1】:

    它返回正确的索引值,正如明确记录的那样,list.index 返回元素的第一个索引。因为您的列表是一系列增加 一个 的数字,并且您将值增加一个 ,所以您看到了这种模式。也许在你的循环中打印test 会更清楚地证明这一点:

    In [2]: test = [1,2,3,4,5,6,7,8,9,10]
       ...: for t in test:
       ...:     index = test.index(t)
       ...:     print(str(t)+ ' Index: '+ str(test.index(t)))
       ...:     print('\t\t'+str(test[test.index(t)]) + ' Index: ' + str(test.index(t)))
       ...:     print('\t\t\t' + str(test))
       ...:     test[index] = test[index] + 1
       ...: print(test)
    1 Index: 0
            1 Index: 0
                [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    2 Index: 0
            2 Index: 0
                [2, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    3 Index: 0
            3 Index: 0
                [3, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    4 Index: 0
            4 Index: 0
                [4, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    5 Index: 0
            5 Index: 0
                [5, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    6 Index: 0
            6 Index: 0
                [6, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    7 Index: 0
            7 Index: 0
                [7, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    8 Index: 0
            8 Index: 0
                [8, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    9 Index: 0
            9 Index: 0
                [9, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    10 Index: 0
            10 Index: 0
                [10, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    [11, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    

    在第一次迭代中,您增加了 test[0] = test[0] + 1。所以test[0] 的值为 2. On your second iteration, then test.index(1)*returns0* because the 2is at index0. You then increment this again, and now test[0] == 3@ 987654332@test.index(3)`,又是第 0 个索引……等等……

    一开始就不要这样做。在循环中使用.index 效率低下,它将创建一个二次时间算法,您可以在其中轻松地在线性时间内完成它,例如但是,通过使用第二种方法,使用起来会更加pythonic:

    for index, t in enumerate(test):
        test[index] = t + 1
    

    【讨论】:

      【解决方案2】:

      循环遍历将自动遍历列表中的每个项目。相反,删除声明 test[index] = test[index] + 1 并且您的代码应该可以正常工作

      test = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
      for t in test:
          index = test.index(t)
          print(str(t) + ' Index: ' + str(test.index(t)))
          print('\t\t' + str(test[test.index(t)]) + ' Index: ' + str(test.index(t)))
      print(test)
      

      【讨论】:

        【解决方案3】:

        您每次将测试列表修改一个,这意味着(给定您的列表)每个元素都等于下一个元素。

        如果您的列表与 [10, 20, 30, 40] 不同,您将看不到(看似不正确的)行为。

        无论如何,您最好使用for index, num in enumerate(test) 来达到您的目的。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-09-03
          • 1970-01-01
          • 2021-11-19
          • 2020-10-02
          • 1970-01-01
          • 2018-03-07
          • 2017-05-22
          • 1970-01-01
          相关资源
          最近更新 更多