我正在嘗試輸出一個字典而不是多個。
為了澄清,我想輸出如下內容:
{28.0: (0.994772, 0.023164), 87.0: (0.97249, 0.024187), 144.0: (0.435119, 0.012234), 315.0: (0.998442, 0.033711), 352.0: (0.998448, 0.035746), 358.0: (0.873265, 0.031968), 432.0: (0.512379, 0.020508), 448.0: (0.949168, 0.037455), 493.0: (0.882312, 0.036017), 496.0: (0.69724, 0.028641)}
但相反,我得到了這個:
{28.0: (248.69299999999998, 5.791)}
{87.0: (243.1225, 6.04675)}
{144.0: (108.77974999999999, 3.0585)}
{315.0: (249.6105, 8.42775)}
{352.0: (249.612, 8.9365)}
{358.0: (218.31625, 7.992000000000001)}
{432.0: (128.09475, 5.127)}
{448.0: (237.292, 9.363750000000001)}
{493.0: (220.578, 9.00425)}
{496.0: (174.31, 7.16025)}
澄清一下,stars.txt 如下:
0.994772 0.023164 -0.099456 28 4.61 3
0.972490 0.024187 0.231685 87 5.55 4
0.435119 0.012234 0.900290 144 5.57 7
0.998442 0.033711 -0.044468 315 6.43 11
0.998448 0.035746 -0.042707 352 6.18 14
0.873265 0.031968 0.486196 358 2.07 15 ALPHERATZ
0.512379 0.020508 0.858515 432 2.28 21 CAPH; CAS BETA
0.949168 0.037455 0.312534 448 5.57 22
0.882312 0.036017 -0.469285 493 5.42 24
0.697240 0.028641 -0.716265 496 3.88 25
這是我的代碼:
import turtle as t
def reading_data(filename):
'''Reads a file, modifies it'''
input_file = open(filename, "r")
dict1 = {}
dict2 = {}
for line in input_file:
text = line.strip(" ")
text_list = text.split(" ")
x = text_list[0]
y = text_list[1]
draper = float(text_list[3])
values = (float(x), float(y))
dict1[draper] = (values)
magnitude = float(text_list[4])
dict2[draper] = (magnitude)
return(dict1, dict2)
def translating(x, y, size):
'''Given x, y coordinates and a size in pixels,
this function translates the coordinates into pixel coordinates for an image of size pixels.
Returns tuple with translated coordinates'''
pixel_values = float(-size/2), float(size/2)
new_x = x * pixel_values[1]
new_y = y * pixel_values[1]
return(new_x, new_y)
def main():
return_values = reading_data('stars.txt')
coordinates = return_values[0]
mags = return_values[1]
pic_size = 500
for value in coordinates:
coordinates_list = coordinates[value]
x_value = coordinates_list[0]
y_value = coordinates_list[1]
(new_x, new_y) = translating(x_value, y_value, pic_size)
translated_coordinates = {}
translated_coordinates[value] = (new_x, new_y)
print(translated_coordinates)
if __name__=="__main__":
main()
我被告知要仔細檢查我的 for 回圈,并將 read_file() 中的那個與我正在 main 中處理的 for 回圈進行比較,這是查找和修復一行代碼的問題。
我被困住了。除其他外,我嘗試縮進 print 陳述句(出現錯誤)并將“translated_coordinate={}”放在 for 回圈上方。
uj5u.com熱心網友回復:
只是translated_coordinates放出print(translated_coordinates)回圈:
def main():
return_values = reading_data('stars.txt')
coordinates = return_values[0]
mags = return_values[1]
translated_coordinates = {}
pic_size = 500
for value in coordinates:
coordinates_list = coordinates[value]
x_value = coordinates_list[0]
y_value = coordinates_list[1]
(new_x, new_y) = translating(x_value, y_value, pic_size)
translated_coordinates[value] = (new_x, new_y)
print(translated_coordinates)
輸出:
{28.0: (248.69299999999998, 5.791), 87.0: (243.1225, 6.04675), 144.0: (108.77974999999999, 3.0585), 315.0: (249.6105, 8.42775), 352.0: (249.612, 8.9365), 358.0: (218.31625, 7.992000000000001), 432.0: (128.09475, 5.127), 448.0: (237.292, 9.363750000000001), 493.0: (220.578, 9.00425), 496.0: (174.31, 7.16025)}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/444690.html
