新手python求助,逐行比较,第二行和第一行的第二列相同,则对第二行计数
求大佬帮忙,第一次发帖,急急急。。。。数据如下,第一列是顺序排列的
scaffold_1..12548466 6
scaffold_1..12548467 6
scaffold_1..12548468 6
scaffold_1..12548469 6
scaffold_1..12548470 6
scaffold_1..12548471 6
scaffold_1..12548472 6
scaffold_1..12548473 5
scaffold_1..12548474 5
scaffold_1..12548475 5
scaffold_1..12548476 5
scaffold_1..12548477 5
scaffold_1..12548478 5
scaffold_1..12548479 4
scaffold_1..12548480 4
scaffold_1..12548481 4
scaffold_1..12548482 4
scaffold_1..12548483 4
想要的结果输出格式
scaffold_1..12548466 scaffold_1..12548472 6 7
scaffold_1..12548473 scaffold_1..12548478 5 6
scaffold_1..12548479 scaffold_1..12548483 4 5
如果第二行的第二列和第一行的第二列相同,则输出第二列的值和频次,以及第一列开头和结尾(第一列是按顺序排列的)
本帖最后由 suchocolate 于 2021-11-12 12:24 编辑
s = '''scaffold_1..12548466 6
scaffold_1..12548467 6
scaffold_1..12548468 6
scaffold_1..12548469 6
scaffold_1..12548470 6
scaffold_1..12548471 6
scaffold_1..12548472 6
scaffold_1..12548473 5
scaffold_1..12548474 5
scaffold_1..12548475 5
scaffold_1..12548476 5
scaffold_1..12548477 5
scaffold_1..12548478 5
scaffold_1..12548479 4
scaffold_1..12548480 4
scaffold_1..12548481 4
scaffold_1..12548482 4
scaffold_1..12548483 4'''
d = dict()# 创建空字典,第二列作key,第一列作为value
for line in s.split('\n'):
k, v = line.split()
d.setdefault(v, [])# setdefault检测key是否存在,不存在就设置key并设置默认值为空列表,用于存储同值的第一列
d.append(k)
for k, v in d.items():# 打印
print(f'{v}\t{v[-1]}\t{k}\t{len(v)}')
本帖最后由 xingkong0214 于 2021-11-12 12:10 编辑
# coding:utf-8
if __name__ == '__main__':
output_text = dict()
# 假设数据从文件中读取
with open(r'test_data.txt', 'r') as f:
for each_line in f:
serial_number = each_line.split()
value = each_line.split()
# 统计数据,存储到一个字典
if value not in output_text.keys():
output_text = {'start': serial_number, 'stop': serial_number, 'count': 1}
else:
count = output_text['count'] + 1
output_text['stop'], output_text['count'] = serial_number, count
# 按照格式打印内容
for each_item in output_text:
print(output_text['start'], output_text['stop'], each_item, output_text['count'])
suchocolate 发表于 2021-11-12 11:59
非常感谢,祝好 suchocolate 发表于 2021-11-12 11:59
非常感谢,祝好
页:
[1]