您當前的代碼有幾個問題:
listOne = ['33.325556', '59.8149016457', '51.1289412359']
itemsInListOne = int(len(listOne)) # len will always be an int
num = 4 # magic number - why 4?
varIncrement = 0
while itemsInListOne < num: # why test, given the break?
for i in listOne:
print a = ('%dfinalCoords{0}') % (varIncrement+1) # see below
print (str(listOne).strip('[]')) # prints list once for each item in list
break # why break on first iteration
一行特別是給你的麻煩:
print a = ('%dfinalCoords{0}') % (varIncrement+1)
此:
- 同時試圖
print
和分配a =
(因此SyntaxError
);
- 混合了兩種不同類型的字符串格式(
'%d'
和'{0}'
);和
- 從來沒有實際增加
varIncrement
,所以你總是會得到'1finalCoords{0}'
無論如何。
我建議如下:
listOne = ['33.325556', '59.8149016457', '51.1289412359']
a = list(map(float, listOne)) # convert to actual floats
您可以輕鬆地訪問或指數,例如編輯單個值你previous question
# edit one value
a[0] = 33.34
# print all values
for coord in a:
print(coord)
# double every value
for index, coord in enumerate(a):
a[index] = coord * 2
來看,似乎你可能想對座標從兩個列表,這也可以用2元組一個簡單的列表來完成:
listOne = ['33.325556', '59.8149016457', '51.1289412359']
listTwo = ['2.5929778', '1.57945488999', '8.57262235411']
coord_pairs = zip(map(float, listOne), map(float, listTwo))
哪給出:
coord_pairs == [(33.325556, 2.5929778),
(59.8149016457, 1.57945488999),
(51.1289412359, 8.57262235411)]
這不是一個好主意。相反,使'a'字典或列表,並把你的價值觀結構。 – BrenBarn
我知道這不是理想的,但我需要爲每個字符串儘快分配一個唯一的變量 – BubbleMonster
我懷疑它。你能解釋爲什麼你認爲你需要以'a0','a1'等等的方式來訪問這些值,而不是'a [0]','a [1]'等等。 – BrenBarn