我正在寫一個函數來保存列表「list_accepted_car」返回給集合「unique_accepted_ant」的唯一值。 list_car_id是值爲['12','18','3','7']的列表。當我運行代碼時出現錯誤,「不可用列表」。任何人都可以告訴我什麼是錯誤?將列表中的唯一元素排序到集合
list_accepted_car = [] #set to store the value list_accepted_car
unique_accepted_car = set() #set to store the value unique_accepted_car
num_accepted = 2 #predifined value for the number of cars allowed to enter
def DoIOpenTheDoor(list_car_id): #list_ant_id is a list of cars allowed to enter
if len(list_accepted_car) < num_accepted:
if len(list_car_id) > 0:
list_accepted_car.append(list_car_id[0:min(len(list_car_id),num_accepted-len(list_accepted_car))])
unique_accepted_list = set(list_accepted_car)
print unique_accepted_list
return list_accepted_car
unique_accepted_list =集(list_accepted_car) 打印unique_accepted_list 回報list_accepted_car 代碼的這部分也應縮進 – crazyglasses
你能解決這個缺口嗎? ('def'外的'return') – Nuageux
因爲list_accepted_car.append(list_car_id [0:min(len(list_car_id),num_accepted-len(list_accepted_car))]])語句附加list中的列表。所以你不能對列表清單進行設置操作。 –