Knowing an item's location in an array

This question already has an answer here:

  • Finding the index of an item given a list containing it in Python 23 answers

  • Use the index() method on list.

    See Finding the index of an item given a list containing it in Python:

    >>> ["foo","bar","baz"].index('bar')
    1
    

    index(arg) returns the index of arg within a list. So you would do:

    from random import shuffle
    deck = [value + suit for value in range(1, 11) + list ("AJQK") for suit in "HCDS"] + ["J1", "J2"]
    
    shuffle(deck)
    
    idx = deck.index('J1')
    deck[idx: idx+2] = deck[idx: idx+2].reverse()
    
    链接地址: http://www.djcxy.com/p/28118.html

    上一篇: 查找元素在列表中的索引

    下一篇: 了解项目在阵列中的位置