list1 = []
list2 = []
list3 = []
elecStorage = []
elective = ["a", "b", "c", "d"]
prereq = []
dict1 = {
1: SEM1period1, 2: SEM1period2,
3: SEM1period3, 4: SEM1period3,
5: SEM1period5, 6: SEM1period6,
7: SEM1period7, 8: SEM1period8
}
for key in dict1:
if len(dict1) < 1:
list1.append("Free Period")
elif key in list1:
break
elif key in elective:
elecStorage.append(key)
elif key in prereq:
list1.append(key)
print elecStorage
print list1
注意:SEM1period1到SEM1period8都是列表,早先作为参数传递。
这个代码片段应该做的是搜索字典dict1的每个列表中的每个元素,并将其与其他列表(如elective和prereq)的内容进行比较,然后将其附加到list1或elecStorage。虽然[“a”,“b”,“c”,“d”]应该全部附加到elecStorage,当我告诉它打印出来时它不会......为什么会这样?
提前致谢。有任何问题,我会澄清。我是一个新手,所以请放轻松。
答案 0 :(得分:0)
我对你的目标做了很多假设,但这至少有帮助:
for key, dlist in dict1.items():
if not dlist:
list1.append("Free Period")
continue
for item in dlist:
if item in list1:
continue
if item in elective:
elecStorage.append(item)
elif item in prereq:
list1.append(item)