Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Knowing an item's location in an array [duplicate]

I want to switch J1's position with the card under it after the deck array is shuffled. Is there a way to reference J1 without knowing its position in the array? Thank you.

import random

deck = ['AC', '2C', '3C', '4C', '5C', '6C', '7C', '8C', '9C', 'TC', 'JC', 'QC', 'KC',
        'AS', '2S', '3S', '4S', '5S', '6S', '7S', '8S', '9S', 'TS', 'JS', 'QS', 'KS',
        'AH', '2H', '3H', '4H', '5H', '6H', '7H', '8H', '9H', 'TH', 'JH', 'QH', 'KH',
        'AD', '2D', '3D', '4D', '5D', '6D', '7D', '8D', '9D', 'TD', 'JD', 'QD', 'KD',
        'J1', 'J2']

# shuffle deck
random.shuffle(deck)

#switch Joker1 with card under it
like image 887
Carm Avatar asked Jan 30 '14 04:01

Carm


People also ask

How do you find if there is duplicate in array?

Using the indexOf() method In this method, what we do is that we compare the index of all the items of an array with the index of the first time that number occurs. If they don't match, that implies that the element is a duplicate.

How do you find duplicate elements in an array using maps?

How To Find Duplicate Elements In Array In Java Using HashMap? In this method, We use HashMap to find duplicates in array in java. We store the elements of input array as keys of the HashMap and their occurrences as values of the HashMap. If the value of any key is more than one (>1) then that key is duplicate element.

How do you find the index of a repeated element in a list?

Find all indices of an item in list using list. As list. index() returns the index of first occurrence of an item in list. So, to find other occurrences of item in list, we will call list. index() repeatedly with range arguments.

How do you check if there is a duplicate in an array Javascript?

To check if an array contains duplicates: Use the Array. some() method to iterate over the array. Check if the index of the first occurrence of the current value is NOT equal to the index of its last occurrence. If the condition is met, then the array contains duplicates.


1 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
like image 148
Colin D Bennett Avatar answered Nov 15 '22 22:11

Colin D Bennett