Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
假设我有一个格式为$(Integer) - $(Integer)或$(Integer). 将它们分解并将它们转换为整数值的最简单方法是什么?如果字符串采用格式,则$(Integer) - $(Integer)取两个数字的平均值。
$(Integer) - $(Integer)
$(Integer)
示例字符串:$20 - $40将转换为 30(这是一个范围)
$20 - $40
>>> string = '$20 - $40' #'$20' will also work >>> x = re.findall(r'\$(\d+)', string) >>> 1. * sum(map(int, x)) / len(x) 30.0 #convert to int if you want