[Tutor] How to group data?

Dave Angel davea at ieee.org
Sun Feb 13 23:52:03 CET 2011


On 01/-10/-28163 02:59 PM, tee chwee liong wrote:
>
> hi,
>
> i'm using Python 2.5 and Win XP.
> i want to extract the last column of the attached text file and group 128 characters to each row. want the result to look like:
> Row1=XXXXX.............................1XXX  (total of 128 char)
> Row2=11XX...............................X1XX (total of 128 char)
>
> ###code
> from __future__ import with_statement
> def main():
>      new_rows = {}
>      values = []
>      with open('test2.txt') as f:
>          for line in f:
>              values.append(line.rstrip('\n')[-1])
>
>      x = []          # making a new list here, not only
>      x += values     # a new reference to the same object
>
>      for count in xrange(0,len(values),128):
>
>          temp_values1 = (x.pop(9))+(x.pop(8))+(x.pop(7))+(x.pop(6))+(x.pop(5))+(x.pop(4))+(x.pop(3))+(x.pop(2))+(x.pop(1))+(x.pop(0))
>          temp_values2 = (x.pop(19))+(x.pop(18))+(x.pop(17))+(x.pop(16))+(x.pop(15))+(x.pop(14))+(x.pop(13))+(x.pop(12))+(x.pop(11))+(x.pop(10))
>          temp_values3 = (x.pop(29))+(x.pop(28))+(x.pop(27))+(x.pop(26))+(x.pop(25))+(x.pop(24))+(x.pop(23))+(x.pop(22))+(x.pop(21))+(x.pop(20))
>          temp_values4 = (x.pop(39))+(x.pop(38))+(x.pop(37))+(x.pop(36))+(x.pop(35))+(x.pop(34))+(x.pop(33))+(x.pop(32))+(x.pop(31))+(x.pop(30))
>          temp_values5 = (x.pop(49))+(x.pop(48))+(x.pop(47))+(x.pop(46))+(x.pop(45))+(x.pop(44))+(x.pop(43))+(x.pop(42))+(x.pop(41))+(x.pop(40))
>          temp_values6 = (x.pop(59))+(x.pop(58))+(x.pop(57))+(x.pop(56))+(x.pop(55))+(x.pop(54))+(x.pop(53))+(x.pop(52))+(x.pop(51))+(x.pop(50))
>          temp_values7 = (x.pop(69))+(x.pop(68))+(x.pop(67))+(x.pop(66))+(x.pop(65))+(x.pop(64))+(x.pop(63))+(x.pop(62))+(x.pop(61))+(x.pop(60))
>          temp_values8 = (x.pop(79))+(x.pop(78))+(x.pop(77))+(x.pop(76))+(x.pop(75))+(x.pop(74))+(x.pop(73))+(x.pop(72))+(x.pop(71))+(x.pop(70))
>          temp_values9 = (x.pop(89))+(x.pop(88))+(x.pop(87))+(x.pop(86))+(x.pop(85))+(x.pop(84))+(x.pop(83))+(x.pop(82))+(x.pop(81))+(x.pop(80))
>          temp_values10 = (x.pop(99))+(x.pop(98))+(x.pop(97))+(x.pop(96))+(x.pop(95))+(x.pop(94))+(x.pop(93))+(x.pop(92))+(x.pop(91))+(x.pop(90))
>          temp_values11 = (x.pop(109))+(x.pop(108))+(x.pop(107))+(x.pop(106))+(x.pop(105))+(x.pop(104))+(x.pop(103))+(x.pop(102))+(x.pop(101))+(x.pop(100))
>          temp_values12 = (x.pop(119))+(x.pop(118))+(x.pop(117))+(x.pop(116))+(x.pop(115))+(x.pop(114))+(x.pop(113))+(x.pop(112))+(x.pop(111))+(x.pop(110))
>          temp_values13 = (x.pop(127))+(x.pop(126))+(x.pop(125))+(x.pop(124))+(x.pop(123))+(x.pop(122))+(x.pop(121))+(x.pop(120))
>
>
>          temp_values=temp_values1+temp_values2+temp_values3+temp_values4+temp_values5+temp_values6+temp_values7+temp_values8+temp_values9+temp_values10+temp_values11+temp_values12+temp_values13
>
>          new_rows['ROW'+str(count+1)] = temp_values
>      print new_rows
> if __name__ == '__main__':
>      main()
> ######
>
> but getting error:
>>>>
> Traceback (most recent call last):
>    File "C:/Python25/myscript/group/group5.py", line 43, in<module>
>      main()
>    File "C:/Python25/myscript/group/group5.py", line 26, in main
>      temp_values8 = (x.pop(79))+(x.pop(78))+(x.pop(77))+(x.pop(76))+(x.pop(75))+(x.pop(74))+(x.pop(73))+(x.pop(72))+(x.pop(71))+(x.pop(70))
> IndexError: pop index out of range
>
> pls advise. tq 		 	   		

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.

There are also a few surprises in the code.

x +=   isn't the simplest way to build a new list from the old.  Use the 
following:

    x = values[:]

You have lots of redundant parentheses on all those similar lines.

Are you really trying to process only the last character of each line?

Are you assuming the file has an exact multiple of 128 lines?

DaveA


More information about the Tutor mailing list