swap in python

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • uaconnection
    New Member
    • Apr 2007
    • 1

    #1

    swap in python

    I can't figure out how to swap, so that amount and name will match after sort.
    Here what I have:
    Code:
    from numarray import *
    import numarray.strings as str
    
    n=input("Enter number of donors to be entered: ")
    if n<5:
        print "Please enter more then 5 donors."
        n=input("Enter number of donors to be entered: ")
    if n>=300:
        print "Please enter less then 300 donors."
        n=input("Enter number of donors to be entered: ")
    donorsName=str.array(itemsize=20,shape=n)
    amount=str.array(itemsize=20,shape=n)
    
    
    
    for i in range(0,n):
        print "Enter donors name:"
        donorsName[i]=raw_input()
        print "Enter amount donated $:"
        amount[i]=raw_input() 
    
    print donorsName,amount
    y=0
    while y < n-1:
        x=0
        while x<n-1:
            print amount
            if amount[x+1]<amount[x]:
                temp=amount[x+1]
                pos=x
                
                while amount[pos]>temp and pos>=0:
                    amount[pos+1]=amount[pos]
                    pos=pos-1
                amount[pos+1]=temp
            x=x+1
        y=y+1        
    print" \nThe donor's name and amount donated:"
    for i in range(0,n):
        print donorsName[i],"$",amount[i]
    
    print "\nProgram Completed......."
    raw_input("Press enter to exit.")
    Last edited by bartonc; Apr 17 '07, 08:42 PM. Reason: added [code][/code] tags
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    Originally posted by uaconnection
    I can't figure out how to swap, so that amount and name will match after sort.
    Here what I have:
    Code:
    from numarray import *
    import numarray.strings as str
    
    n=input("Enter number of donors to be entered: ")
    if n<5:
        print "Please enter more then 5 donors."
        n=input("Enter number of donors to be entered: ")
    if n>=300:
        print "Please enter less then 300 donors."
        n=input("Enter number of donors to be entered: ")
    donorsName=str.array(itemsize=20,shape=n)
    amount=str.array(itemsize=20,shape=n)
    
    
    
    for i in range(0,n):
        print "Enter donors name:"
        donorsName[i]=raw_input()
        print "Enter amount donated $:"
        amount[i]=raw_input() 
    
    print donorsName,amount
    y=0
    while y < n-1:
        x=0
        while x<n-1:
            print amount
            if amount[x+1]<amount[x]:
                temp=amount[x+1]
                pos=x
                
                while amount[pos]>temp and pos>=0:
                    amount[pos+1]=amount[pos]
                    pos=pos-1
                amount[pos+1]=temp
            x=x+1
        y=y+1        
    print" \nThe donor's name and amount donated:"
    for i in range(0,n):
        print donorsName[i],"$",amount[i]
    
    print "\nProgram Completed......."
    raw_input("Press enter to exit.")
    First, you'll want to start thinking "database" not "scientific ". Forget about numpy arrays and use native tuples and lists. Keep each entry in a tuple:
    Code:
    dataList = []   # an empty list
    name = "john"
    amount = 75.50
    dataList.append((name, amount,))   # append a tuple to the list.
    Sorting is done natively as well:
    Code:
    dataList.sort()
    I hope this make your life a little simpler (that's what Python is all about).

    Comment

    Working...