Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python中使用list[list.index(';';)]查询_Python_List_Indexing - Fatal编程技术网

如何在python中使用list[list.index(';';)]查询

如何在python中使用list[list.index(';';)]查询,python,list,indexing,Python,List,Indexing,我在pythonidle中尝试了以下代码。但我似乎没有发现元素交换 >>> a = [1,2,3,4,5,6,7] >>> if(a.index(2)<a.index(4)): ... a[a.index(2)],a[a.index(4)] = a[a.index(4)],a[a.index(2)] >a=[1,2,3,4,5,6,7] >>>如果(a.index(2)赋值时赋值列表表达式从左到右求值 发生的情况如下: 右侧表达式的求值结果为

我在pythonidle中尝试了以下代码。但我似乎没有发现元素交换

>>> a = [1,2,3,4,5,6,7]
>>> if(a.index(2)<a.index(4)):
...     a[a.index(2)],a[a.index(4)] = a[a.index(4)],a[a.index(2)]
>a=[1,2,3,4,5,6,7]

>>>如果(a.index(2)赋值时赋值列表表达式从左到右求值

发生的情况如下:

  • 右侧表达式的求值结果为
    (4,2)
  • a[a.index(2)]
    被赋值为
    4
    a[2]
    被修改,列表变成
    [1,4,3,4,5,6,7]
  • a[a.index(4)]
    被赋值为
    2
    a[2]
    再次被修改,因为这是第一个位置
    4
    回到
    [1,2,3,4,5,6,7]
您可以在反汇编的Python字节码中看到这一点:

>>> def foo():
...     a = [1,2,3,4,5,6,7]
...     a[a.index(2)],a[a.index(4)] = a[a.index(4)],a[a.index(2)]
... 
>>> import dis
>>> dis.dis(foo)
  2           0 LOAD_CONST               1 (1)
              3 LOAD_CONST               2 (2)
              6 LOAD_CONST               3 (3)
              9 LOAD_CONST               4 (4)
             12 LOAD_CONST               5 (5)
             15 LOAD_CONST               6 (6)
             18 LOAD_CONST               7 (7)
             21 BUILD_LIST               7
             24 STORE_FAST               0 (a)

  3          27 LOAD_FAST                0 (a)
             30 LOAD_FAST                0 (a)
             33 LOAD_ATTR                0 (index)
             36 LOAD_CONST               4 (4)
             39 CALL_FUNCTION            1
             42 BINARY_SUBSCR       
             43 LOAD_FAST                0 (a)
             46 LOAD_FAST                0 (a)
             49 LOAD_ATTR                0 (index)
             52 LOAD_CONST               2 (2)
             55 CALL_FUNCTION            1
             58 BINARY_SUBSCR       
             59 ROT_TWO             
             60 LOAD_FAST                0 (a)
             63 LOAD_FAST                0 (a)
             66 LOAD_ATTR                0 (index)
             69 LOAD_CONST               2 (2)
             72 CALL_FUNCTION            1
             75 STORE_SUBSCR        
             76 LOAD_FAST                0 (a)
             79 LOAD_FAST                0 (a)
             82 LOAD_ATTR                0 (index)
             85 LOAD_CONST               4 (4)
             88 CALL_FUNCTION            1
             91 STORE_SUBSCR        
             92 LOAD_CONST               0 (None)
             95 RETURN_VALUE        
通过指令索引59,Python计算了右侧表达式;接下来是赋值。您可以看到,首先计算
a.index(2)
(63-72),然后
STORE\u SUBSCR
存储
4
,然后才计算
a.index(4)
(指令79-85)

解决方法是为每个值调用一次
.index()
,并将索引存储在变量中:

index_two, index_four = a.index(2), a.index(4)
if index_two < index_four:
    a[index_two], a[index_four] = a[index_four], a[index_two]
索引2,索引4=a.index(2),a.index(4) 如果索引2<索引4: a[index_二]、a[index_四]=a[index_四]、a[index_二]
为了避免这种情况,您可以将
.index()
调用的结果存储在变量中,然后对它们进行交换:

>>> a = [1,2,3,4,5,6,7]
>>> i2 = a.index(2)
>>> i4 = a.index(4)
>>> if i2<i4:
...     a[i2], a[i4] = a[i4], a[i2]
>>> a
[1, 4, 3, 2, 5, 6, 7]
>a=[1,2,3,4,5,6,7]
>>>i2=a.指数(2)
>>>i4=a.指数(4)
>>>如果i2>>a
[1, 4, 3, 2, 5, 6, 7]

这样,您也可以避免在一次就足够的情况下调用该方法三次。

Martijn的答案已经完成,并解释了您遇到的问题。如果您仍然想知道如何编写符合您需要的代码,请尝试以下方法:

if (a.index(2) < a.index(4)):
    x,y = a.index(4),a.index(2)
    a[x],a[y] = a[y],a[x]
if(a.index(2)

这里的想法基本上只是将
索引
返回值存储在列表本身之外的内容中。这样单独保存索引可以避免您遇到的竞争条件。

谢谢您的回答!那么,如果我只能访问其索引值,我如何交换列表中的2和4?这仍然调用
.index()
对我的口味来说,我的目标是清晰而不是简洁