我制作了以下脚本,以读取字符串缓冲区并将数字分配到6个不同的变量中。我发现了一个使用switch-case方法在C#中执行相同操作的示例,当我在python中尝试类似的方法时(如下所示),我获得了所需的结果,但是读取缓冲区的时间过多(超过一秒钟) 。该脚本只是测试该方法的一种方式,它将是更大的开环控制代码的一部分,因此循环时间确实很重要。有什么更快的方法可以在python中做吗?我使用python 2.7。先感谢您。
Julio = '123.5,407.4,21.6,9.7,489.2,45.9/\n'
letter = ''
x_c = ''
y_c = ''
z_c = ''
theta_c = ''
ux_c = ''
uy_c = ''
variable_number = 1
def one():
global x_c
x_c += letter
def two():
global y_c
y_c += letter
def three():
global z_c
z_c += letter
def four():
global theta_c
theta_c += letter
def five():
global ux_c
ux_c += letter
def six():
global uy_c
uy_c += letter
def string_reader(variable_number):
switcher = {
1: one,
2: two,
3: three,
4: four,
5: five,
6: six
}
# Get the function from switcher dictionary
func = switcher.get(variable_number, lambda: 'Invalid variable number')
# Execute the function
print func()
for letter in Julio:
if (letter != '/') and (letter != ',') and (letter != '\n'):
string_reader(variable_number)
elif (letter == '/'):
break
elif (letter == '\n'):
break
else:
variable_number = variable_number + 1
print x_c, y_c, z_c, theta_c, ux_c, uy_c
答案 0 :(得分:0)
Err ...你不是在使事情变得复杂吗?
>>> Julio = '123.5,407.4,21.6,9.7,489.2,45.9/\n'
>>> x_c, y_c, z_c, theta_c, ux_c, uy_c = Julio.strip().rstrip("/").split(",")[:6]