Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/309.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 列表中随机数的索引?_Python_Random_Position_Indexof - Fatal编程技术网

Python 列表中随机数的索引?

Python 列表中随机数的索引?,python,random,position,indexof,Python,Random,Position,Indexof,所以我在别处寻求帮助,但找不到任何解决办法。我创建了一个包含51个元素的列表,希望在该列表中随机选择10个不同的位置,并能够更改这些位置的值(您可以在assignCard(person)函数下看到这一点)。由于我使用的是数字索引,因此无法使用诸如random.sample之类的方法,也无法将随机数放入set()中,因为我收到了错误消息。我在下面发布了我的代码,请随意复制并运行它以查看输出,除了重复的数字外,其他一切都正常,因此如果可能,请不要在您的答案中大幅更改代码 """ cardGame.p

所以我在别处寻求帮助,但找不到任何解决办法。我创建了一个包含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()
“cardGame.py
基本纸牌游戏框架
根据需要为尽可能多的手记录卡的位置
"""
从随机导入*
随机输入
NUMCARDS=52
甲板=0
玩家=1
COMP=2
cardLoc=[0]*NUMCARDS
suitName=(“红心”、“钻石”、“黑桃”、“梅花”)
兰克南=(“王牌”,“二”,“三”,“四”,“五”,“六”,“七”,
“八”、“九”、“十”、“杰克”、“女王”、“国王”)
玩家名称=(“牌组”、“玩家”、“计算机”)
def main():
clearDeck()
对于范围(5)中的i:
分配卡(玩家)
转让卡(公司)
打印(cardLoc)
打印(“#”+”卡“+”位置)
showDeck()
打印(“\n显示玩家手:”)
表演者(玩家)
打印(“\n显示计算机手:”)
表演手(综合)
def clearDeck():
cardLoc=[0]*NUMCARDS
信用卡(人):
x=random.randint(0,51)
cardLoc[x]=人
def showDeck():
对于范围内的i(NUMCARDS):
y=rankName[i%13]
z=suitName[int(i/13)]
a=cardLoc[i]
b=玩家名称[a]
打印(“{”:
因为我使用的是数字索引,所以不能使用诸如
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
因为我使用的是数字索引,所以不能使用诸如
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

您好,谢谢您的帮助。在实现了您对我的程序的解决方案后,它有时仍会在玩家手上输出5张牌,在计算机手上输出4张牌,反之亦然。我还缺少什么吗?@Matteo我不确定这与您的问题有何关系。但如果您只是尝试将牌随机分配给两个玩家,
random.shuffle
你的牌组,然后像你在现实生活中可能做的那样,给每一张牌。我的程序差不多完成了,我只需要修复这一个错误,这就是为什么我希望保持我的程序相对不变,而不添加随机。shuffleperhaps问题在于函数assignCard()的重复这可能会导致列表中的一个数字随机出现。sample等于分配给玩家或计算机的同一列表中的一个数字。@Matteo是的,很明显。这就是为什么你应该洗牌和发牌的原因……您好,谢谢您的帮助。在我的程序中实现您的解决方案后,它有时仍然会在玩家手中输出5张牌,在计算机中输出4张牌手牌,反之亦然。我还缺什么吗?@Matteo我不知道这和你的问题有什么关系。但如果你只是想随机分配两个玩家的牌,
random。洗牌你的牌组,然后像现实生活中一样,每隔一张就给一张。我的程序差不多完成了,我只需要这一个错误to已修复,这就是为什么我希望在不添加random.shuffleperhaps的情况下保持程序相对相同的原因问题在于函数assignCard()的重复这可能会导致列表中的一个数字随机。sample等于分配给玩家或计算机的同一列表中的一个数字。@Matteo是的,显然。这就是为什么你应该洗牌并处理。。。