0

Need to row name single time even if it's used multiple times in every row. the file contains:

x 1 asd
x 2 asd
x 3 asd
x 4 asd
x 5 asd
x 5 asd
x 7 asd
b 8 axy
b 9 axc

Output:

x 
asd
asd
asd
asd
asd
asd
asd

b
axy
axc
Captain-Robot
  • 71
  • 2
  • 8

1 Answers1

0

One option is to use groupby:

from itertools import groupby
from operator import itemgetter

text = """x 1 asd
x 2 asd
x 3 asd
x 4 asd
x 5 asd
x 5 asd
x 7 asd
b 8 axy
b 9 axc"""

for k, g in groupby(map(str.split, text.splitlines()), itemgetter(0)):
    print(k, *map(itemgetter(-1), g), sep="\n")

# x
# asd
# asd
# asd
# asd
# asd
# asd
# asd
# b
# axy
# axc
j1-lee
  • 13,764
  • 3
  • 14
  • 26