我有5列的列表,第5列是数字列表,第1列是组标识符。总共有500行,但只有24组
我想要的是从第5列中具有最小数字的每个组标识符中只选择一行
例如
sheet= """
cmn1\tcmn2\tcmn3\tcmn4\tcmn5
rob\t45\tfoo\tbar\t0.0001
Steve\t32\tfoo\tspam\t0.01
rob\t45\tbar\tfoo\t0.0000001
Steve\t32\tfoo\tbar\t0.1"""
这是理想的结果:
cmn1\tcmn2\tcmn3\tcmn4\tcmn5
Steve\t32\tfoo\tspam\t.01
rob\t45\tbar\tfoo\t0.0000001
我在每行的列表中都有我的字段,但我一直在思考如何选择部分中数字最小的行[4]
for line in sheet:
line = sheet.strip().split("\n")
parts = []
for part in line:
parts = []
parts = part.split("\t")
print parts [0], parts [1], parts[2], parts[3], parts[4]
您可以使用
itertools.groupby
根据第一项对拆分行进行分组,然后使用min
函数和适当的key
来选择所需行:如果订单很重要,您可以使用和订购信息通信技术广告,同时检查:
使用您编辑的线,您可以看到输出未更改,它将与原来的完全相同:
您可以使用字典存储每个唯一列1的所有行:
相关问题 更多 >
编程相关推荐