列表中随机数的索引?

时间:2017-10-27 22:34:57

标签: python random position indexof

所以我在其他地方寻求帮助,但找不到任何解决方案。我创建了一个包含51个元素的列表,并希望在该列表中选择10个随机的不同位置,并能够更改这些位置的值(您可以在assignCard(person)函数下看到这一点)。由于我使用的是数字索引,因此无法使用random.sample等方法或将随机数放入set(),因为我收到错误消息。我已经在下面发布了我的代码,随意复制并运行它来查看输出,除了重复的数字之外一切正常,所以如果可能请不要在你的答案中彻底改变代码。

""" cardGame.py
    basic card game framework
    keeps track of card locations for as many hands as needed
"""
from random import *
import random

NUMCARDS = 52
DECK = 0
PLAYER = 1
COMP = 2

cardLoc = [0] * NUMCARDS
suitName = ("hearts", "diamonds", "spades", "clubs")
rankName = ("Ace", "Two", "Three", "Four", "Five", "Six", "Seven", 
            "Eight", "Nine", "Ten", "Jack", "Queen", "King")
playerName = ("deck", "player", "computer")

def main():
  clearDeck()

  for i in range(5):
    assignCard(PLAYER)
    assignCard(COMP)

  print(cardLoc)

  print("#  " + " card      " + " location")
  showDeck()
  print("\nDisplaying player hand:")
  showHand(PLAYER)
  print("\nDisplaying computer hand:")
  showHand(COMP)

def clearDeck():
    cardLoc = [0] * NUMCARDS

def assignCard(person):
  x = random.randint(0, 51)
  cardLoc[x] = person

def showDeck():
  for i in range(NUMCARDS):
    y = rankName[i % 13]
    z = suitName[int(i / 13)]
    a = cardLoc[i]
    b = playerName[a]
    print("{:<4}{:<4} of {:<14}{:<7}".format(str(i), y, z, b))

def showHand(person):
  for i in range(NUMCARDS):
    if cardLoc[i] == person:
      print(rankName[i % 13] + suitName[int(i / 13)])

main()

1 个答案:

答案 0 :(得分:3)

  

由于我使用的是数字索引,因此无法使用random.sample等方法

当然可以:

>>> import random
>>> my_list = [0]*10
>>> my_list
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
>>> idx = list(range(len(my_list)))
>>> idx
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> s = random.sample(idx, 5)
>>> s
[4, 0, 5, 7, 1]
>>> for i in s:
...     my_list[i] = 99
...
>>> my_list
[99, 99, 0, 0, 99, 99, 0, 99, 0, 0]

在这里,只需执行以下操作:

>>> NUMCARDS = 52
>>> cardLoc = [0]*52
>>> s = random.sample(range(NUMCARDS), 10)
>>> for i in s[:5]:
...     cardLoc[i] = 1
...
>>> for i in s[5:]:
...     cardLoc[i] = 2
...

现在我只是打印这些职位:

>>> for i in range(NUMCARDS):
...     if cardLoc[i] == 1:
...         print(i)
...
10
24
25
34
41
>>> for i in range(NUMCARDS):
...     if cardLoc[i] == 2:
...         print(i)
...
14
23
28
49
50
>>>

或者,加载你的常数:

>>> NUMCARDS = 52
>>> DECK = 0
>>> PLAYER = 1
>>> COMP = 2
>>>
>>> cardLoc = [0] * NUMCARDS
>>> suitName = ("hearts", "diamonds", "spades", "clubs")
>>> rankName = ("Ace", "Two", "Three", "Four", "Five", "Six", "Seven",
...             "Eight", "Nine", "Ten", "Jack", "Queen", "King")
>>> playerName = ("deck", "player", "computer")
>>>
>>> s = random.sample(range(NUMCARDS), 10)
>>> for i in s[:5]:
...     cardLoc[i] = PLAYER
...
>>> for i in s[5:]:
...     cardLoc[i] = COMP
...
>>> def showHand(person):
...   for i in range(NUMCARDS):
...     if cardLoc[i] == person:
...       print(rankName[i % 13] + suitName[int(i / 13)])
...
>>> showHand(PLAYER)
Threehearts
Ninehearts
Sixdiamonds
Ninediamonds
Twoclubs
>>> showHand(COMP)
Sevenhearts
Sevendiamonds
Sixspades
Queenspades
Kingclubs
相关问题