简体   繁体   中英

python group by and count Columns for each line

I have a txt file which contains n numbers of row and each row has n number of columns with one delimiter.

File :

x|x|x|x
x|x|x|x|x|x
x|x|x|x|x|x|x|x|x|x|x
x|x|x
x|x|x|x
x|x|x

i want to take like below output

out:

group by Columns (same number of columns ) - count of Columns - line no

2 - 4 -  line 1, line 5
1 - 6 - line 2
1 - 11 - line 3
2 - 3 - line 4,line 6

can you help? i tried with pandas but i couldn't succeed.

Sure. You definitely don't need Pandas; collections.defaultdict is your friend.

import io
from collections import defaultdict

# Could be a `open(...)` instead, but we're using a
# StringIO to make this a self-contained program.

data = io.StringIO("""
x|x|x|x
x|x|x|x|x|x
x|x|x|x|x|x|x|x|x|x|x
x|x|x
x|x|x|x
x|x|x
""".strip())

linenos_by_count = defaultdict(set)

for lineno, line in enumerate(data, 1):
    count = line.count("|") + 1  # Count delimiters, add 1
    linenos_by_count[count].add(lineno)

for count, linenos in sorted(linenos_by_count.items()):
    lines_desc = ", ".join(f"line {lineno}" for lineno in sorted(linenos))
    print(f"{len(linenos)} - {count} - {lines_desc}")

outputs

2 - 3 - line 4, line 6
2 - 4 - line 1, line 5
1 - 6 - line 2
1 - 11 - line 3

Here is an alternative using itertools.groupby based on @AKX approach:

from itertools import groupby
print('\n'.join([f'{len(G)} - {k} - '+', '.join([f'line {x[0]+1}' for x in G])
                 for k, g in groupby(sorted(enumerate([s.count('x')
                                                       for s in data.split('\n')
                                                       ]),
                                            key=lambda x: x[1]),
                                     lambda x: x[1]
                                     )
                 for G in [list(g)]
                 ]))

output:

2 - 3 - line 4, line 6
2 - 4 - line 1, line 5
1 - 6 - line 2
1 - 11 - line 3

Below is a less barbaric formatting:

from itertools import groupby
counts = [s.count('x') for s in data.split('\n')]
for k, g in groupby(sorted(enumerate(counts),
                           key=lambda x: x[1]),
                    lambda x: x[1]):
    G = list(g)
    print(f'{len(G)} - {k} - '+', '.join([f'line {x[0]+1}' for x in G]))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM