[Tutor] renumbering a sequence

Alan Gauld alan.gauld at btinternet.com
Wed Aug 27 09:53:01 CEST 2008


"Christopher Spears" <cspears2002 at yahoo.com> wrote 

> ordered_list = sorted(unordered_list)
> 
> test_frame_1 = ordered_list[0].split('.')[1]
> test_frame_2 = ordered_list[1].split('.')[1]
> 
> if test_frame_1 == "0000":
>    if test_frame_2 =! "0001":
>        print "Sequence needs to be renumbered"
> for frame in ordered_list:
>     new_frame = renumber_frame(frame)
>     print new_frame
> elif test_frame_1 != "0001"
>    print "Sequence needs to be renumbered"
>    for frame in ordered_list:
>     new_frame = renumber_frame(frame)
>     print new_frame
> else:
>    print "Sequence is fine"

You can change the test sequence to avoid the 
code duplication

if tf1 == '0000' and tf2 == '0001':
    print 'sequence fine'
else
    print 'renumber sequence'
    for frame in ....etc

> As you can see, the only part I haven't figured out is the 
> actual renumbering.  Can't figure out how to do the following:
> 0017 convert to -> 0001

Instead of splitting and reconstructing you could use a 
regex to locate the number section and usere.sub() to replace 
the old number with the new sequence number

Not sure if that would be any faster but it might be more 
concise.


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld



More information about the Tutor mailing list