在使用Excel多年并学习VBA之后,我现在尝试学习Python。这是场景:
我问了7个夏令营辅导员,他们希望负责哪些活动。每个学生的回答数量都是随机的,选择的活动数量没有上限。但是,每项活动都是唯一的,一旦被学生“要求”,其他任何辅导员都无法要求。结果是:
Adam: archery, canoeing
Bob: frisbee, golf, painting, trampoline
Carol: tennis, dance, skating
Denise: cycling
Eddie: horseback, fencing, soccer
Fiona: painting
George: basketball, football
我对VB最熟悉(我是一个老家伙),过去我将上述信息存储在一个锯齿状的数组中。但是由于我是Python的新手,所以我对如何执行此操作感到困惑。我认为列表列表对我有用,这是我的代码。假设我有一个顾问列表,每个顾问活动的列表都是单独的。如何合并它们或将它们放在一个数据结构中?我在下面做什么错了?谢谢。
counselors = []
counselors = ['Adam','Bob','Carol','Denise','Eddie','Fiona','George']
#create a list of Carol's activities
activities = []
activities = ['tennis','dance','skating']
counselors[2].append[(activities)]
答案 0 :(得分:3)
Python中的锯齿状数组几乎就像您提到的列表列表一样。
我将使用字典来存储咨询员的活动信息,其中键是咨询员的姓名,值是咨询员将负责的活动列表。
counselors_activities = {"Adam": ["archery", "canoeing"],
"Bob": ["frisbee", "golf", "painting", "trampoline"],
"Carol": ["tennis", "dance", "skating"],
"Denise": ["cycling"],
"Eddie": ["horseback", "fencing", "soccer"],
"Fiona": ["painting"],
"George": ["basketball", "football"]}
并按以下方式访问字典中的每个顾问:
counselors_activites["Adam"] # when printed will display the result => ['archery', 'canoeing']
关于这个问题,我会将可用的活动列表存储在一个列表中,并且每当选择了一个活动时,就将其从列表中删除,并按如下方式将其添加到词典的咨询员中:
list_of_available_activities.remove("archery")
counselors_activities["Adam"].append("archery")
如果辅导员不再负责该活动,请将其从他们中删除,并将其重新添加到可用活动列表中。
更新:根据您的评论要求,我在下面提供了更为完善的解决方案。
文本文件activites.txt:
Adam: archery, canoeing
Bob: frisbee, golf, painting, trampoline
Carol: tennis, dance, skating
Denise: cycling
Eddie: horseback, fencing, soccer
Fiona: painting
George: basketball, football
代码:
#Set of activities available for counselors to choose from
set_of_activities = {"archery",
"canoeing",
"frisbee",
"golf",
"painting",
"trampoline",
"tennis",
"dance",
"skating",
"cycling",
"horseback",
"fencing",
"soccer",
"painting",
"basketball",
"football"}
with open('activities.txt', 'r') as f:
for line in f:
# Iterate over the file and pull out the counselor's names
# and insert their activities into a list
counselor_and_activities = line.split(':')
counselor = counselor_and_activities[0]
activities = counselor_and_activities[1].strip().split(', ')
# Iterate over the list of activities chosen by the counselor and
# see if that activity is free to choose from and if the activity
# is free to choose, remove it from the set of available activities
# and if it is not free remove it from the counselor's activity list
for activity in activities:
if activity in set_of_activities:
set_of_activities.remove(activity)
else:
activities.remove(activity)
# Insert the counselor and their chosen activities into the dictionary
counselors_activities[counselor] = activities
# print(counselors_activities)
在这个新示例中,我做出了一个假设,那就是您已经有了一系列可以从已经可用的活动中选择的活动:
我使文本文件与问题中列出的咨询员及其活动的格式相同,但是该逻辑可以应用于其他存储方式。
作为旁注和之前第二个示例的更正,我使用了一个集合来表示活动列表,而不是本示例中的列表。该集合仅用于验证没有辅导员负责已经分配给其他人的活动;也就是说,在最坏的情况下,从集合中删除活动要比从列表中删除活动快。
辅导员可以从记事本文件插入字典中,而不必将其插入列表中。
当打印字典时,将产生结果:
{"Adam": ["archery", "canoeing"],
"Bob": ["frisbee", "golf", "painting", "trampoline"],
"Carol": ["tennis", "dance", "skating"],
"Denise": ["cycling"],
"Eddie": ["horseback", "fencing", "soccer"],
"Fiona": [], # Empty activity list as the painting activity was already chosen by Bob
"George": ["basketball", "football"]}