0
votes

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
1
What have you tried so far? Where were you struggling? Please ask a question.Manfred

1 Answers

0
votes

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