在 string/list 中查找特定元素并按增量移动它们
find specific elements in string/list and move them by increment
我尝试将 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”。什么都容易。我假设首先移动所有小数点,然后在第二个循环中擦除后面跟着空 space 的点是最好的选择。
到目前为止我得到了什么:
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)
但这只会移动第一次出现的“.”
我已经检查了很多帖子,但大多数帖子只是在谈论用 "append" 将角色移动到末尾,这对我的情况没有帮助。
感谢您的帮助,非常感谢!
创建这样的列表不是更好的方法吗?
path_list = [ 317.3962167, -142.7]
new_path_list = []
for i in path_list:
new_path_list.append(i*10)
现在您将得到一个列表,其中的数字已移至小数点后一位。然后你可以把你的路径改成这样
path_string = "M" + str(new_path_list[0]) + ", " + str(new_path_list[1]) + ", L" + str(new_path_list[0]) + ", " + str(new_path_list[1])
那会给你一个你想要的字符串,不是吗?
试试这个,
import re
input = '<path d="M317.3962167,-142.7 L317.3962167,-142.7"></path>'
# extract all the values inside the tag
values = re.search('d="(.*)"', input)
parsed_values = []
for value in values.group(1).split(","):
tmp = []
for v in value.split():
search_ = re.search("([\w|-])(\d+.\d+)", v)
# 317.3962167 * 10 -> 3173.962167
prefix, num = search_.group(1), float(search_.group(2)) * 10
tmp.append(prefix + str(num))
parsed_values.append(" ".join(tmp))
print('<path d="%s"></path>' % ",".join(parsed_values))
输出
<path d="M3173.962167,-1427.0 L3173.962167,-1427.0"></path>
您可以使用正则表达式。您可以保存以管理字符串位置。
import re
def rpl(mo):
v= float(mo[0])*10
return str(v)
s= '<path d="M317.3962167,-142.7 L317.3962167,-142.7" >'
re.sub(r"[-]?\d+[\.]?\d*",rpl,s)
'<path d="M3173.962167,-1427.0 L3173.962167,-1427.0" >'
在re.sub中,r"[-]?\d+[\.]?\d*"
是float的模式,rpl
是一个以匹配对象为参数的例程,m[0]
是第一个匹配,即浮动。
我尝试将 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”。什么都容易。我假设首先移动所有小数点,然后在第二个循环中擦除后面跟着空 space 的点是最好的选择。
到目前为止我得到了什么:
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)
但这只会移动第一次出现的“.”
我已经检查了很多帖子,但大多数帖子只是在谈论用 "append" 将角色移动到末尾,这对我的情况没有帮助。 感谢您的帮助,非常感谢!
创建这样的列表不是更好的方法吗?
path_list = [ 317.3962167, -142.7]
new_path_list = []
for i in path_list:
new_path_list.append(i*10)
现在您将得到一个列表,其中的数字已移至小数点后一位。然后你可以把你的路径改成这样
path_string = "M" + str(new_path_list[0]) + ", " + str(new_path_list[1]) + ", L" + str(new_path_list[0]) + ", " + str(new_path_list[1])
那会给你一个你想要的字符串,不是吗?
试试这个,
import re
input = '<path d="M317.3962167,-142.7 L317.3962167,-142.7"></path>'
# extract all the values inside the tag
values = re.search('d="(.*)"', input)
parsed_values = []
for value in values.group(1).split(","):
tmp = []
for v in value.split():
search_ = re.search("([\w|-])(\d+.\d+)", v)
# 317.3962167 * 10 -> 3173.962167
prefix, num = search_.group(1), float(search_.group(2)) * 10
tmp.append(prefix + str(num))
parsed_values.append(" ".join(tmp))
print('<path d="%s"></path>' % ",".join(parsed_values))
输出
<path d="M3173.962167,-1427.0 L3173.962167,-1427.0"></path>
您可以使用正则表达式。您可以保存以管理字符串位置。
import re
def rpl(mo):
v= float(mo[0])*10
return str(v)
s= '<path d="M317.3962167,-142.7 L317.3962167,-142.7" >'
re.sub(r"[-]?\d+[\.]?\d*",rpl,s)
'<path d="M3173.962167,-1427.0 L3173.962167,-1427.0" >'
在re.sub中,r"[-]?\d+[\.]?\d*"
是float的模式,rpl
是一个以匹配对象为参数的例程,m[0]
是第一个匹配,即浮动。