我正在写一个函数来保存列表“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中的列表。所以你不能对列表清单进行设置操作。 –