哪裏mystring = mystring = "'34.894127 29.761515', '32.323574 30.166336', '32.677296 31.961439', '35.298668 31.559237', '34.894127 29.761515"
你可以得到對的列表,像這樣:
x = [pair.lstrip().strip("'").split(' ') for pair in mystring.split(',')]
# gives: [['34.894127', '29.761515'], ['32.323574', '30.166336'], ['32.677296', '31.961439'], ['35.298668', '31.559237'], ['34.894127', '29.761515']]
,或者如果你真的想要的元組:
x = tuple([tuple(pair.lstrip().strip("'").split(' ')) for pair in mystring.split(',')])
# gives: (('34.894127', '29.761515'), ('32.323574', '30.166336'), ('32.677296', '31.961439'), ('35.298668', '31.559237'), ('34.894127', '29.761515'))
米奇的解決方案是好的,但如果'tuple'是你真的希望,這會更好: '[元組(圖(浮動,coord.split()))的字符串座標]' –