【发布时间】:2020-09-06 09:24:13
【问题描述】:
我尝试将 SVG 中的路径从厘米单位更改为毫米单位。路径由字符串“d”定义,如下所示:
<path d="M317.3962167,-142.7 L317.3962167,-142.7 ...
我现在想将所有小数点移动一个增量 i(对于 cm 到 mm -> i=1)。结果应如下所示:
<path d="M3173.962167,-1427 L3173.962167,-1427
或
<path d="M3173.962167,-1427.0 L3173.962167,-1427.0
因此,如果小数点被移动并且以下列表条目为空,则应删除小数点或添加“0”。更容易的。我认为首先移动所有小数点,然后在第二个循环中擦除后面跟着空格的点是最好的选择。
到目前为止我得到了什么:
def cm_to_mm(chars, char, increment):
# Convert character sequence to list type.
char_list = list(chars)
if "." in chars:
# Get the current index of the target character.
old_index = char_list.index(char)
# Remove the target character from the character list.
char = char_list.pop(old_index)
# Insert target character at a new location.
new_index = old_index + increment
char_list.insert(new_index, char)
# Convert character list back to str type and return.
return ''.join(char_list)
但这只会移动第一次出现的“。”
我已经检查了很多帖子,但大多数都只是在谈论使用“追加”将角色移动到末尾,这对我的情况没有帮助。 感谢您的帮助,非常感谢!
【问题讨论】:
标签: python string character increment move