如果我可以重新表述您的问题,您想要的是一本以年份为键的字典,以及一个包含每年与该年份相关的值列表的数组,对吗?这是我的处理方式:
years_dict = dict()for line in list: if line[0] in years_dict: # append the new number to the existing array at this slot years_dict[line[0]].append(line[1]) else: # create a new array in this slot years_dict[line[0]] = [line[1]]
您应该在years_dict中最终得到的是一本类似于以下内容的字典:
{ "2010": [2], "2009": [4,7], "1989": [8]}
通常,创建“并行数组”是不好的编程习惯,在这种情况下,项目具有相同的索引而不是包含它们的容器的适当子代,从而使项目彼此隐式关联。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)