Python 什么';将任意字符串转换为可读的掷骰结果的最佳方法是什么?
我对python编码相当陌生 我编写的代码只保留字符“0-9”、“+”、“-”、“*”、“/”和“d”。Python 什么';将任意字符串转换为可读的掷骰结果的最佳方法是什么?,python,string,dice,Python,String,Dice,我对python编码相当陌生 我编写的代码只保留字符“0-9”、“+”、“-”、“*”、“/”和“d”。 输入示例为“3d6+4d3+3d2-433 seffesfes” 这将分解为“3d6+4d3+3d2-433”。我被告知使用eval()函数是个坏主意,因此我不确定执行最终计算的最佳方法是 我考虑过在字符串中循环,直到它找到非数字的内容,然后向后查找上半部分,然后再查找下半部分,然后根据它找到的内容执行函数-但这似乎不正确。使用正则表达式,我们可以消除所有与您接受的字符列表不匹配的字符,并删
输入示例为“3d6+4d3+3d2-433 seffesfes”
这将分解为“3d6+4d3+3d2-433”。我被告知使用eval()函数是个坏主意,因此我不确定执行最终计算的最佳方法是
我考虑过在字符串中循环,直到它找到非数字的内容,然后向后查找上半部分,然后再查找下半部分,然后根据它找到的内容执行函数-但这似乎不正确。使用正则表达式,我们可以消除所有与您接受的字符列表不匹配的字符,并删除您的字符串:
import re
re.sub("[^\d+\-*\/d]", '', your_str)
例如:
import re
re.sub("[^\d+\-*\/d]", '', "3d6 + 4d3 + 3d2 - 433 seffesfes")
# >>> '3d6+4d3+3d2-433'
正则表达式解释:
Match a single character not present in the list below [^\d+\-*\/d]:
\d matches a digit (equal to [0-9])
+ matches the character + literally (case sensitive)
\- matches the character - literally (case sensitive)
* matches the character * literally (case sensitive)
\/ matches the character / literally (case sensitive)
d matches the character d literally (case sensitive)
您可以在此处了解有关正则表达式(并使用该正则表达式)的更多信息:
编辑:
your_parsed_string = '3d6+4d3+3d2-433'
re.sub('(\d+d\d+)', roll, your_parsed_string)
# >>> '8+9+2-433'
解析初始字符串后,要正确计算随机值,应使用re.sub
:
import re
import random
def roll(match):
a,b = match.group(1).split('d')
return str(random.randint(int(a), int(a)*int(b)))
re.sub('(\d+d\d+)', roll, your_parsed_string)
说明:
re.sub
是一个函数,它接收要在给定字符串中查找的正则表达式模式,然后用替代值替换它。但此函数还支持从回调进行替换,这是传递给re.sub
的roll
函数
回调函数作为参数接收re.Match
对象,因此可以访问其所有特性。其中之一是方法group
,它允许我们访问由正则表达式解析的组(正则表达式组由()
标记,并且允许我们轻松访问捕获的正则表达式中的部分)
因此,我们捕获掷骰子的每个外观,将其作为正则表达式匹配发送到回调函数roll
,然后将该字符串值解析为您希望获得的“掷骰子”
例如:
your_parsed_string = '3d6+4d3+3d2-433'
re.sub('(\d+d\d+)', roll, your_parsed_string)
# >>> '8+9+2-433'
现在,为了安全地评估最终字符串,请使用以下方法:
或者-自己编写一个简单的解析函数使用正则表达式,我们可以消除与接受的字符列表不匹配的所有字符,并删除字符串:
import re
re.sub("[^\d+\-*\/d]", '', your_str)
例如:
import re
re.sub("[^\d+\-*\/d]", '', "3d6 + 4d3 + 3d2 - 433 seffesfes")
# >>> '3d6+4d3+3d2-433'
正则表达式解释:
Match a single character not present in the list below [^\d+\-*\/d]:
\d matches a digit (equal to [0-9])
+ matches the character + literally (case sensitive)
\- matches the character - literally (case sensitive)
* matches the character * literally (case sensitive)
\/ matches the character / literally (case sensitive)
d matches the character d literally (case sensitive)
您可以在此处了解有关正则表达式(并使用该正则表达式)的更多信息:
编辑:
your_parsed_string = '3d6+4d3+3d2-433'
re.sub('(\d+d\d+)', roll, your_parsed_string)
# >>> '8+9+2-433'
解析初始字符串后,要正确计算随机值,应使用re.sub
:
import re
import random
def roll(match):
a,b = match.group(1).split('d')
return str(random.randint(int(a), int(a)*int(b)))
re.sub('(\d+d\d+)', roll, your_parsed_string)
说明:
re.sub
是一个函数,它接收要在给定字符串中查找的正则表达式模式,然后用替代值替换它。但此函数还支持从回调进行替换,这是传递给re.sub
的roll
函数
回调函数作为参数接收re.Match
对象,因此可以访问其所有特性。其中之一是方法group
,它允许我们访问由正则表达式解析的组(正则表达式组由()
标记,并且允许我们轻松访问捕获的正则表达式中的部分)
因此,我们捕获掷骰子的每个外观,将其作为正则表达式匹配发送到回调函数roll
,然后将该字符串值解析为您希望获得的“掷骰子”
例如:
your_parsed_string = '3d6+4d3+3d2-433'
re.sub('(\d+d\d+)', roll, your_parsed_string)
# >>> '8+9+2-433'
现在,为了安全地评估最终字符串,请使用以下方法:
或者-自己编写一个简单的解析函数您可以使用
re.sub
中的替换函数将骰子符号转换为数字
def convertDice(matchobj):
faceCount = int(matchobj.group('faces'))
return str(sum(random.randint(1, faceCount) for _ in int(matchobj.group('times')) ))
re.sub(r"(?P<times>\d+)d(?P<faces>\d+)", convertDice, your_str)
def convertDice(匹配对象):
faceCount=int(匹配对象组('faces'))
返回str(sum(random.randint(1,faceCount)表示uin int(matchobj.group('times')))
关于sub(r“(?P\d+)”d(?P\d+),convertDice,your_str)
现在您有了一个可以计算的字符串您可以使用
re.sub
中的替换函数将骰子符号转换为数字
def convertDice(matchobj):
faceCount = int(matchobj.group('faces'))
return str(sum(random.randint(1, faceCount) for _ in int(matchobj.group('times')) ))
re.sub(r"(?P<times>\d+)d(?P<faces>\d+)", convertDice, your_str)
def convertDice(匹配对象):
faceCount=int(匹配对象组('faces'))
返回str(sum(random.randint(1,faceCount)表示uin int(matchobj.group('times')))
关于sub(r“(?P\d+)”d(?P\d+),convertDice,your_str)
现在你有了一个可以计算的字符串你试过正则表达式吗?我想我不清楚,它使用了dice\u input=re.sub(“[^0-9d\+-*\/]”,“,dice\u input”),我想知道如何将“3d8”转换为random.randint(1,dice\u faces)作为字符串的每个部分。首先解释一下你用
3d8
的意思,而433
的意思是什么,“3d8”表示掷3个8面骰子。基本上,我想将“XdY”字符串转换为一个数字,然后对整个字符串执行计算,支持加法、减法(如减法433)等功能。您尝试过正则表达式吗?我想我不清楚,它使用dice\u input=re.sub(“^0-9d\+-*\/]”,dice\u input)已经,我想知道如何转换“3d8”随机。为字符串的每个部分设置randint(1,骰子面)。首先解释您使用3d8
表示什么,以及433
表示什么“3d8”表示掷3个8面骰子。基本上,我想将“XdY”字符串转换为一个数字,然后对整个字符串执行计算,支持加法、减法(如减法433)等功能。您不需要转义+
、*
和/
。我想我不清楚,它使用的是dice\u input=re.sub(“[^0-9d\+-*/]”、“,dice\u input)我已经想知道如何将“3d8”转换为字符串每个部分的random.randint(1,dice_faces)。@VisioN它可能不是,它是,在[]
中只有`具有特殊意义,需要转义
]`如果作为[]
中的第一个字符放置,则不需要转义<代码>[^]]应为valid@LewisMcMeekin我将在2分钟内用更多的正则表达式编辑我的答案magic@rioV8cha中的这些字符没有特殊意义