脾气暴躁

时间:2019-08-18 07:47:40

标签: python numpy

我只是想知道为什么我从下面的代码运行中出错。我正在尝试使用numpy来为基于文本的游戏做概率。下面的代码不是游戏本身中的代码。这仅用于测试目的和学习。谢谢您的提前答复,请对我轻松一点。

$computers = get-content C:\Users\Administrator\Desktop\Newfolder\new\input.txt
$Output = @()
foreach ($strComputer in $computers) {
$CimData = Get-CimInstance -ClassName SoftwareLicensingProduct -computer $strComputer | where PartialProductKey 

    if ($CimData) {

        $CimData | % {
        $Row = "" | Select Computer,Name,LicenseStatus
        $Row.Computer = $strComputer
        $Row.Name = $_.Name
        $Row.LicenseStatus = $_.LicenseStatus
        $Output += $Row
        }
    }
} 

$Output | Export-Csv -Path C:\Users\Administrator\Desktop\Newfolder\new\output.csv

错误:

from numpy.random import choice

class container:

    def __init__(self):
        self.inv = {'common': ['blunt sword', 'blunt axe'], 'uncommon': ['Dynasty bow', 'Axe', 'Sword'], 'rare': ['Sharp axe'], 'epic': ['Great Sword']}
        self.probabilities = {"common": 50, 'uncommon':25, 'rare': 10, 'epic': 3}
        self.item = choice(self.inv(choice(self.probabilities.keys(), p=self.probabilities.values())))

    def open(self):
        return f'You loot {self.item}'


loot = container().open()
print(loot)

1 个答案:

答案 0 :(得分:1)

根据np.random.choice的文档,它采用一维数组或整数a以及概率矢量p,其长度等于a的长度,并且总和为一。在您的情况下,您正在喂probabilities.keys()类型的inv.keys()dict_keys。同样,您的概率向量的总和不为1。将概率向量转换为所需格式的一种方法是将其除以总和。我已经对代码进行了必要的更改

from numpy.random import choice

class container:

def __init__(self):
    self.inv = {'common': ['blunt sword', 'blunt axe'], 'uncommon': ['Dynasty bow', 'Axe', 'Sword'], 'rare': ['Sharp axe'], 'epic': ['Great Sword']}
    self.probabilities = {"common": 50, 'uncommon':25, 'rare': 10, 'epic': 3}
    self.convert_prob_vector()
    self.item = choice(self.inv[choice(list(self.probabilities.keys()), p=self.probabilities_new)])

def convert_prob_vector(self):
    self.probabilities_new = [x / sum(self.probabilities.values()) for x in self.probabilities.values()]
def open(self):
    return f'You loot {self.item}'


loot = container().open()
print(loot)
  

示例输出

     

你抢朝代弓

希望这会有所帮助!