nested grouping in Python


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting nested grouping in Python
# 1  
Old 05-25-2011
nested grouping in Python

HI All

Is there a way of nested grouping in python
I wish to format a file as given below
Input File:

Item: 1
sub1
A,10,x,5
B,345,v,7
C,67,u,9
sub2
A,09,9,.09
B,65,3..34
C,8,8,1

Item: 2
sub1
A,10,x,5
B,345,v,7
C,67,u,9
sub2
A,09,9,.23
B,65,3..2
C,8,8,.5

Item: 3
sub1
A,10,x,5
B,345,v,7
C,67,u,9
sub2
A,09,9,.5
B,65,3..4
C,8,8,6

group by Item and then group by sub so that I retain the sub1 and append the last element in sub2 to the output

Required output:
Item: 1
A,10,x,5,.09
B,345,v,7,.34
C,67,u,9,1
Item: 2
A,10,x,5,.23
B,345,v,7,2
C,67,u,9,.5
Item: 3
A,10,x,5,.5
B,345,v,7,.4
C,67,u,9,6

Any suggestion will be immensely appreciated
Thanks
Abid
# 2  
Old 05-26-2011
Your output example contradicts your text, no trace of sub2.
# 3  
Old 05-26-2011
Thanks for looking into it .

I meant the last element of sub2 which is in this case .09 gets appended to whole of sub1

Item: 1
Example
sub1
A,10,x,5
sub2
A,09,9,.09

.09 of sub2 is appended to sub1

output:
Item: 1
A,10,x,5,.09
# 4  
Old 05-26-2011
Well, that is a bit messy: parse and hold. You need to hold sub1 in variables until you get to sub2 to collect the additions to A and whatever that will go on the new, merged sub's output.
# 5  
Old 05-26-2011
do you know any module which I can use here in python
# 6  
Old 05-26-2011
Well, you will have to write to your i/o files' schema. You might leverage any CSV modules Python might have, or other delimited text parsers.
# 7  
Old 05-26-2011
Thanks but I was hoping for some grouping modules it might have was trying with itertools but not sure if that is the way to do it ...
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Programming

Create a C source and compile inside Python 1.4.0 to 3.7.0 in Python for ALL? platforms...

Hi all... As you know I like making code backwards compatible for as many platforms as possible. This Python script was in fact dedicated for the AMIGA A1200 using Pythons 1.4.0, 1.5.2, 1.6.0, 2.0.1, and 2.4.6 as that is all we have for varying levels of upgrades from a HDD and 4MB FastRam... (1 Reply)
Discussion started by: wisecracker
1 Replies

2. Windows & DOS: Issues & Discussions

How to execute python script on remote with python way..?

Hi all, I am trying to run below python code for connecting remote windows machine from unix to run an python file exist on that remote windows machine.. Below is the code I am trying: #!/usr/bin/env python import wmi c = wmi.WMI("xxxxx", user="xxxx", password="xxxxxxx")... (1 Reply)
Discussion started by: onenessboy
1 Replies

3. Shell Programming and Scripting

Grouping and counting

Hello, I would like to group/sort a file of records by a particular field and then count how many records belong in that grouping. For example say I have the following data: 1234|"ZZZ"|"Date"|"1"|"Y"|"ABC"|""|AA ABCD|"ZZZ"|"Date"|"1"|"Y"|"ABC"|""|AA EFGH|"ZZZ"|"Date"|"1"|"Y"|"ABC"|""|BB... (14 Replies)
Discussion started by: Nik44
14 Replies

4. Shell Programming and Scripting

Grouping and Calculating

Hi All, I want to read the input file and store the output in the Output file. I pasted the sample Input and Output file below. Help me with this. Input file ================================= ITEM1 AAAAA 1 ITEM1 BBBBB 1 ITEM1 CCCCC 1 ITEM2 AAAAA 5 ITEM2 CCCCC 4... (1 Reply)
Discussion started by: humaemo
1 Replies

5. Shell Programming and Scripting

Name grouping

awk 'FNR==NR {a; next} $NF in a' genes.txt refseq_exons.txt > output.txt I can not figure out how to group the same name in $4 together. Basically, all the SKI together in separate rows and all the TGFB2. Thank you :). chr1 2160133 2161174 SKI chr1 218518675 218520389 TGFB2... (1 Reply)
Discussion started by: cmccabe
1 Replies

6. Shell Programming and Scripting

**python** unable to read the background color in python

I am working on requirement on spreadsheet in python scripting. I have a spreadsheet containing cell values and with background color. I am able to read the value value but unable to get the background color of that particular cell. Actually my requirement is to read the cell value along... (1 Reply)
Discussion started by: giridhar276
1 Replies

7. Shell Programming and Scripting

Grouping

Hi all, I am using following command: perl program.pl input.txt output.txt CUTOFF 3 > groups_3.txt containing program.pl, two files (input.txt, output.txt) and getting output in groups_3.txt: But, I wish to have 30 files corresponding to each CUTOFF ranging from 0 to 30 using the same... (1 Reply)
Discussion started by: bioinfo
1 Replies

8. Shell Programming and Scripting

Selective grouping

I have a text file in this format. Group: AAA Notes: IP : 11.11.11.11 #User xxxxxxxxx #Password aaaaaaaaaaaaaaaa Group: AAA Notes: IP : 11.11.11.22 #User yyyyyyyyyyyyy #Password bbbbbbbbbbbbb (8 Replies)
Discussion started by: anil510
8 Replies

9. UNIX for Advanced & Expert Users

Regarding the user grouping

I am new to the unix server. My question is: i have 10 users a b c d e f g h i j Among the 10 5 users are (normal) user and remainging 5 users are (qad) users How can i sepearte this one? How can i give the dlc access rights? Please provide the clear cut idea. (0 Replies)
Discussion started by: kingganesh04
0 Replies
Login or Register to Ask a Question