[英]Create an empty list for each item in a list
I have a list that came from an excel doc: 我有一个来自excel doc的列表:
["GT", "FSU", "Duke"]
I am trying to make an empty list for each item in this list (to look like this): 我正在尝试为此列表中的每个项目创建一个空列表(看起来像这样):
GTList = []
FSUList = []
Duke = []
Is there an easy way to go from step 1 to step 2. I need to use loops too because there is a possibility of there being an extra school in the first list. 有没有一种简单的方法可以从第1步到第2步。我也需要使用循环,因为第一个列表中可能有额外的学校。 If this is the case, there needs to be a corresponding empty list for that new school. 如果是这种情况,则需要为该新学校提供相应的空列表。 Please let me know what advice you have. 请告诉我你有什么建议。
I would use a dictionary comprehension: 我会用词典理解:
schools = ["GT", "FSU", "Duke", "Stanford"]
schools_list = {school: [] for school in schools}
>>> schools_list
{'Duke': [], 'FSU': [], 'GT': [], 'Stanford': []}
schools_list['Stanford'].append('sucks')
>>> schools_list['Stanford']
['sucks']
I answered similar question earlier today. 我今天早些时候回答了类似的问 Try this: 尝试这个:
a = ["GT", "FSU", "Duke"]
for i in a:
locals()[i] = []
print GT, FSU, Duke
Output: 输出:
[ ] [ ] [ ] [] [] []
Also, here's why you shoudn't be creating dynamic variables which is why I recommend Alexander's answer 此外, 这就是为什么你不应该创建动态变量,这就是为什么我推荐亚历山大的答案
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.