【问题标题】:List comprehension and converting an argument to something else just isn't my thing列出理解并将参数转换为其他东西不是我的事
【发布时间】:2021-06-06 15:03:12
【问题描述】:

我在这里有这个工作当前代码,它将接受用户的参数(例如:!modify [2, 2, 2, 2, 2, 2, 2.05, 2, 2, 2, 2, 2, 2] [3 , 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 4, 2] 将有机器人输出) 然后取所有集合中 [] 内的 13 个数字提供的 [] 并将它们相乘以输出一对新的 [],其格式与参数中提供的格式相同。

@client.command()
async def modify(ctx, *args):
    if ctx.channel.id != 850806578125864981:
        return
    else:
        sets = [] # stocking every sets found
        subset = [] #I call a 'set' a pair of brackets

        for arg in args:
            subset.append(float(''.join([car for car in arg if car.isnumeric() or car == '.']))) # just extracting the numerical part of the element
            if ']' in arg: #closing a set
                sets.append(subset)
                subset = [] # creating a new one

        output = [*sets[0]] # number of the first set

        for s in sets[1:]: # looping over every other sets
            for i in range(len(s)):
                output[i] *= s[i] # multiplying the numbers together

        embed=discord.Embed(title="g.stats helper", url="", description=f":white_check_mark: - Here is your new custom g.stat: \n\n **{output}**", color=0x77AC54)
        channel = client.get_channel(850806578125864981)
        await channel.send(embed=embed)

但后来我决定我也想让机器人识别一组特定的单词,比如如果在 !modify 命令的参数中提供了“g.basic”或“g.factory”,机器人会像你一样识别它输入“[18.25,1.4,.1,1,2,.2,1,4.5,1,1,1,15,1]”或“[72, 1, .1, .7, 2, .2, 1, 3, 1, 1, 1, .1, 1]”(例如:“!修改 g.basic [3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 4, 2]”就像说“!修改 [18.25,1.4,.1,1,2,.2,1,4.5,1,1,1,15,1] [3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 4, 2]”,通常机器人输出“[54.75, 2.8, 0.2, 2.0, 4.0, 0.4, 2.0, 9.0, 2.0, 2.0, 2.0, 60.0, 2.0]").

问题是我不知道如何让机器人识别某些单词,例如“g.basic”,下面是我失败的尝试(如果有人不仅给出了答案,而且还进行了深入的解释,那就太棒了他们做了什么来解决问题/他们实现的代码的含义,那太好了!):

@client.command()
async def modify(ctx, *args):
    if ctx.channel.id != 850828323135160341:
        return
    else:

        lookup = {
    "g.basic": "[18.25, 1.4, .1, 1, 2, .2, 1, 4.5, 1, 1, 1, 15, 1]",
    "g.factory": "[72, 1, .1, .7, 2, .2, 1, 3, 1, 1, 1, .1, 1]"
}
        new_args = [lookup.get(arg, arg) for arg in args]

        sets = [] # stocking every sets found
        subset = [] #I call a 'set' a pair of brackets

        for arg in new_args:
            subset.append(float(''.join([car for car in arg if car.isnumeric() or car == '.']))) # just extracting the numerical part of the element
            if ']' in arg: #closing a set
                sets.append(subset)
                subset = [] # creating a new one

        output = [*sets[0]] # number of the first set

        for s in sets[1:]: # looping over every other sets
            for i in range(len(s)):
                output[i] *= s[i] # multiplying the numbers together

        embed=discord.Embed(title="g.stats helper", url="", description=f":white_check_mark: - Here is your new custom g.stat: \n\n **{output}**", color=0x77AC54)
        channel = client.get_channel(850806578125864981)
        await channel.send(embed=embed)

我知道了。 对于任何花时间阅读本文并帮助我的人,你太棒了!我已经寻找解决方案超过一天了,我的大脑真的开始发麻了。

【问题讨论】:

  • 你能试着把不同的输入和相关的期望输出更清楚吗?我看到了一个例子,但我没有得到完整的图片
  • 要修复这个特定的错误,请在[car for car in arg if car.isnumeric() or car == '.'] 处更改您的行,因为即使您得到一个点'.',您也会尝试转换为浮点数。虽然您应该将数字的两个部分放在一起(18.25 不应被点分开)
  • 有没有机会将其提炼成最小 minimal reproducible example 专注于该问题?不仅仅是代码转储

标签: python python-3.x discord discord.py


【解决方案1】:

所以我没有时间设置不和谐频道并设置一个项目来使用它,但我可以给你一些应该可以工作的代码。你只需要适应你的情况。

好的,所以您从命令!modify 获取输入作为字符串。而且您希望使用快捷方式来节省打字时间。例如g.basic 应该与输入[18.25, 1.4, .1, 1, 2, .2, 1, 4.5, 1, 1, 1, 15, 1] 相同。酷是有道理的。

这就是我要做的。

lookup = {
    "g.basic": "[18.25, 1.4, .1, 1, 2, .2, 1, 4.5, 1, 1, 1, 15, 1]",
    "g.factory": "[72, 1, .1, .7, 2, .2, 1, 3, 1, 1, 1, .1, 1]"
}

# Using a mock input that would be similar to what you're getting as input
mock_input = ["g.basic", "[3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 4, 2]"]


# First lets replace any shortcuts with the actual value they point to
# This takes mock_input and maps it to a lambda that if 'arg' is a key in lookup it replaces it with the value
# if it's not in lookup then just return the original input
processed_input = list(map(lambda arg: arg if arg not in lookup else lookup[arg], mock_input))

args_values = []

# Now lets get all numbers in args and convert it to a list of floats
for arg in processed_input:
    # List comprehension cause this is python
    # Split the string by ', ' cause thats how they are seperated in the string
    # arg[1: -1] will remove the leading and trailing '[' and ']' so we dont have to worry about them
    # Then we convert all strings in this list to floats and add it to the 'args_values' list
    args_values.append([float(value) for value in arg[1: -1].split(', ')])

# Now zip the inputs together to match first elements together then second elements and so on
# Then map them to multiple them together and return the new list of multiplied values
print (list(map(lambda values: values[0] * values[1], zip(*args_values))))

【讨论】:

  • 无论我在修改命令中输入什么作为参数,现在我总是收到“[54.75, 2.8, 0.2, 2.0, 4.0, 0.4, 2.0, 9.0, 2.0, 2.0, 2.0 , 60.0, 2.0]"
  • 请记住,我的代码中有模拟数据。您需要将 mock_input 替换为您从不和谐中获得的输入。如果你能弄清楚,请告诉我你是如何在脚本中使用我的代码的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-05
  • 2019-11-29
  • 2016-07-03
  • 1970-01-01
  • 1970-01-01
  • 2023-03-08
相关资源
最近更新 更多