慕勒3428872
使用itertools.groupby().例如,我正在使用您的确切“数据结构”(stackoverflow 问题中的一堆文本):import itertoolsdata_structure = '''1 --- 1 --- 1002 --- 1 --- 2003 --- 1 --- 1001 --- 2 --- 3002 --- 2 --- 1003 --- 2 --- 400'''.splitlines()# create a key function able to extract the data you want to group:def _key(line): return line.strip().split(' --- ')[1] # the 1 here means second column#cleanup data:clean_data = (line.strip() for line in data_structure if line.strip())# then pass it to itertools.groupby:for key, lines in itertools.groupby(clean_data, key=_key): print("Lines that contain number", key, 'in second column:') print(', '.join(lines))结果:Lines that contain number 1 in second column:1 --- 1 --- 100, 2 --- 1 --- 200, 3 --- 1 --- 100Lines that contain number 2 in second column:1 --- 2 --- 300, 2 --- 2 --- 100, 3 --- 2 --- 400编辑:既然你编辑了问题,并说你有一个文本文件,那么你可以用它代替data_structure它,它会起作用:data_structure = open('myfile.txt')其余代码保持不变