我有一根绳子:
50 5 10 4 48 34
其中每个值对应于以下记法方案:
x1 y1 x2 y2 x3 y3
也就是说,它的意思是:
x1 = 50
y1 = 5
etc.
在输入端,一个用空格分隔的数字字符串被写入我的变量,我想从中获得一个列表,其中存储坐标的元组以逗号分隔,即:
[(50, 5), (10, 4), (48, 34)]
请告诉我怎么做好吗?
我有一根绳子:
50 5 10 4 48 34
其中每个值对应于以下记法方案:
x1 y1 x2 y2 x3 y3
也就是说,它的意思是:
x1 = 50
y1 = 5
etc.
在输入端,一个用空格分隔的数字字符串被写入我的变量,我想从中获得一个列表,其中存储坐标的元组以逗号分隔,即:
[(50, 5), (10, 4), (48, 34)]
请告诉我怎么做好吗?
试试这个-
input_string = "50 5 10 4 48 34"
numeric_values = input_string.split()
coordinates = [(int(numeric_values[i]), int(numeric_values[i + 1])) for i in range(0, len(numeric_values), 2)]
print(coordinates)