# ['A', 36], # ['B', 85], # ['C', 15], # ['B', 65], # ['B', 52], # ['A', 89] dataFile = open("data.txt") l = [] for line in dataFile: if line.strip(): l.append(line.strip().split(',')) dataFile.close() res = {} for item in l: #k = item[0] #if not res.has_key(k): # res[item[0]] = [] #res[item[0]].append(item[1]) res.setdefault(item[0], []).append(item[1]) # 利用python这个api setdefault,它会帮助设定一个default值 print res