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
Answer
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
No comments:
Post a Comment