[Tutor] How to group data?

tee chwee liong tcl76 at hotmail.com
Mon Feb 14 15:05:59 CET 2011


thanks everyone for the advice. 
my code looks very messy... needs more practise.
 


From: waynejwerner at gmail.com
Date: Mon, 14 Feb 2011 05:15:26 -0600
Subject: Re: [Tutor] How to group data?
To: tcl76 at hotmail.com
CC: davea at ieee.org; tutor at python.org


On Sun, Feb 13, 2011 at 6:03 PM, tee chwee liong <tcl76 at hotmail.com> wrote:



> How about some clues as to what you're trying to accomplish? This code 
> is surprisingly verbose, and probably totally wrong. Anyway, each time 
> you pop an item from the list, all the following ones change their 
> index. So presumably there weren't still 79 items in the list by the 
> time you had removed and shuffled a bunch of them. You don't indicate 
> how many times it went around the outer loop, but in any case, there 
> won't be enough values the last time around.
> 

sorry for the confusion. i'm trying to get:
>>> 
ROW1=1XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
ROW2=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
ROW3=XX11111X11XXXXX11XXXXXX1XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
ROW4=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX0XXXXXXXXXX1XXXXXXXXXXXX111111XXXX
 
whereby it will group the last column in the data file into each row with 128 characters in reverse order. for eg: in data below and if i group it into 4 characters in a row in reverse order will be:



>From this example it appears that what you would like to do is get the last character from a grouping of four lines. Then you would like to reverse each group of four.


If this is true, what you're doing is most certainly the wrong way to go about it. You can easily break this problem down into several steps:


1) open a file and read four lines at a time.
2) Pull the last character from each line (you can use reverse indexing, i.e. '987654321'[-3] => '3')
3) group these characters into a string and reverse them - Dave Angel showed the most idiomatic way.
4) prepend "Row N=" to your string, replacing N with the number
5) Do something with all of those strings
6) Profit???


It appears that from the beginning you did *not* create an algorithm, or steps to solve your problem, but just started throwing code at it. This will usually produce terrible code that is both buggy and horrible to try to read. 


If it turns out that I misunderstood what your end goal was with the data, simply decide 

1) What do I need?
2) What is the pattern in my data?
3) What are the easiest tools to use to get what I need, considering my pattern?


And then you probably have your program.


HTH,
Wayne
  

row1=XX11
row2=XXXX
row3=XXXX
row4=00XX

 
0 BC_4 SYSCLK_DN observe_only 1
1 BC_4 SYSCLK_DP observe_only 1
2 BC_4 QPI3_DRX_DN 19 input X
3 BC_4 QPI3_DRX_DP 19 input X
4 BC_4 QPI3_DRX_DN 18 input X
5 BC_4 QPI3_DRX_DP 18 input X
6 BC_4 QPI3_DRX_DN 17 input X
7 BC_4 QPI3_DRX_DP 17 input X
8 BC_4 QPI3_DRX_DN 16 input X
9 BC_4 QPI3_DRX_DP 16 input X
10 BC_4 QPI3_DRX_DN 15 input X
11 BC_4 QPI3_DRX_DP 15 input X
12 BC_4 QPI3_DRX_DN 14 input X
13 BC_4 QPI3_DRX_DP 14 input X
14 BC_4 QPI3_DRX_DN 13 input 0
15 BC_4 QPI3_DRX_DP 13 input 0
 

_______________________________________________
Tutor maillist  -  Tutor at python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110214/3dac4965/attachment-0001.html>


More information about the Tutor mailing list