简体   繁体   English

在列表中除以多个数字

[英]Dividing multiple numbers in a list

Sorry if this has been asked before but I couldn't find it. 抱歉,以前是否有人问过我,但我找不到。 For my discord bot, I'm making a calculator aspect and currently I made it so you can only divide 2 numbers but I want to make it unlimited. 对于我的不和谐机器人,我正在做一个计算器,目前已经做到了,因此您只能除以2,但是我想使其不受限制。 eg [10, 5, 2] = 10 / 5 / 2 Here is my code so hopefully someone can help transform it into unlimited numbers. eg [10, 5, 2] = 10 / 5 / 2 10/5/2这是我的代码,因此希望有人可以帮助将其转换为无限数量。

elif operation.clean_content == "/":
        await client.send_message(message.channel, "Please enter your sum.")
        usersum = await client.wait_for_message(author=message.author)
        ssum = usersum.clean_content.replace("/", " ")
        c = ssum.split(" ")
        if len(c) > 2:
            await client.send_message(message.channel, "Sorry, I can only divide 2 numbers.")
        else: 
            results = list(map(int, c))
            await client.send_message(message.channel, "Result = "+str(results[0] / results[1]))

You can use reduce for this. 您可以为此使用reduce

>>> import functools, operator
>>> functools.reduce(operator.truediv, [10, 5, 2]) 
1.0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM