Python help - Bubblecomparison

Soldato
Joined
1 Mar 2003
Posts
5,508
Location
Cotham, Bristol
Hi folks having a slight brain fart here :(

Say I have a list of the following elements

['','','a','b','c']

Now my aim is to compare two adjacent elements, if the first one is blank and the next in the list isn't then swap the elements, otherwise do nothing. So at the end of the processing the list should look like

['a','b','c','','']

I'm using a bubble sort type comparison thing, but i'm not entirely sure if this is correct. Can a kind person have a look at the following for me and say if i'm getting it right?

Code:
import sys
import string
import techrtime

address_fields = 0

def format_address:
    full_address = techvars.full_address
    address_list = full_address.split()
    address_fields = len(address_list)
    for current in range(len(address_list)):
        for record in range(len(address_list)):        
            if ((address_list[current] == "")&&(address_list[record] == "")):
            
            else:
                address_list[current] = address_list[record]
                address_list[record] = ""
                break
 
Soldato
OP
Joined
1 Mar 2003
Posts
5,508
Location
Cotham, Bristol
Updated code, still having a bit of trouble with it :(

Code:
##########################################################
# Common code to despace address lines
# Author : Paul Statham
# Date : 14/07/2006
#
# Version: 1.0
##########################################################
import sys
import string
import techrtime

def format_address:
    # Get tab delimited address string from Openprint
    full_address = techvars.full_address
    # Split the address into a list
    address_list = full_address.split()
    # Loop through the list swapping list items where necessary
    for current in range(len(address_list)):
        for record in range(len(address_list)):        
            if ((address_list[current] == "")&&(address_list[record] == "")):
            
            else:
                if address_list[current] != "":
                    address_list[current] = address_list[record]
                    address_list[record] = ""
                break

    # Set all blank items in the list to a tab delimiter                
    for current in address_list:
        if current  == "":
            current == "\t"
    
    # Reset the full address        
    full_address = ""
    # Loop through the list appending to the full_address
    # string as it continues
    for current in address_list:
        full_address += current
        
    # Finally set the Openprint address string to be
    # correctly formatted 
    techvars.full_address = full_address
 
Back
Top Bottom