473,396 Members | 1,827 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

swap in python

I can't figure out how to swap, so that amount and name will match after sort.
Here what I have:
Expand|Select|Wrap|Line Numbers
  1. from numarray import *
  2. import numarray.strings as str
  3.  
  4. n=input("Enter number of donors to be entered: ")
  5. if n<5:
  6.     print "Please enter more then 5 donors."
  7.     n=input("Enter number of donors to be entered: ")
  8. if n>=300:
  9.     print "Please enter less then 300 donors."
  10.     n=input("Enter number of donors to be entered: ")
  11. donorsName=str.array(itemsize=20,shape=n)
  12. amount=str.array(itemsize=20,shape=n)
  13.  
  14.  
  15.  
  16. for i in range(0,n):
  17.     print "Enter donors name:"
  18.     donorsName[i]=raw_input()
  19.     print "Enter amount donated $:"
  20.     amount[i]=raw_input() 
  21.  
  22. print donorsName,amount
  23. y=0
  24. while y < n-1:
  25.     x=0
  26.     while x<n-1:
  27.         print amount
  28.         if amount[x+1]<amount[x]:
  29.             temp=amount[x+1]
  30.             pos=x
  31.  
  32.             while amount[pos]>temp and pos>=0:
  33.                 amount[pos+1]=amount[pos]
  34.                 pos=pos-1
  35.             amount[pos+1]=temp
  36.         x=x+1
  37.     y=y+1        
  38. print" \nThe donor's name and amount donated:"
  39. for i in range(0,n):
  40.     print donorsName[i],"$",amount[i]
  41.  
  42. print "\nProgram Completed......."
  43. raw_input("Press enter to exit.")
Apr 17 '07 #1
1 3133
bartonc
6,596 Expert 4TB
I can't figure out how to swap, so that amount and name will match after sort.
Here what I have:
Expand|Select|Wrap|Line Numbers
  1. from numarray import *
  2. import numarray.strings as str
  3.  
  4. n=input("Enter number of donors to be entered: ")
  5. if n<5:
  6.     print "Please enter more then 5 donors."
  7.     n=input("Enter number of donors to be entered: ")
  8. if n>=300:
  9.     print "Please enter less then 300 donors."
  10.     n=input("Enter number of donors to be entered: ")
  11. donorsName=str.array(itemsize=20,shape=n)
  12. amount=str.array(itemsize=20,shape=n)
  13.  
  14.  
  15.  
  16. for i in range(0,n):
  17.     print "Enter donors name:"
  18.     donorsName[i]=raw_input()
  19.     print "Enter amount donated $:"
  20.     amount[i]=raw_input() 
  21.  
  22. print donorsName,amount
  23. y=0
  24. while y < n-1:
  25.     x=0
  26.     while x<n-1:
  27.         print amount
  28.         if amount[x+1]<amount[x]:
  29.             temp=amount[x+1]
  30.             pos=x
  31.  
  32.             while amount[pos]>temp and pos>=0:
  33.                 amount[pos+1]=amount[pos]
  34.                 pos=pos-1
  35.             amount[pos+1]=temp
  36.         x=x+1
  37.     y=y+1        
  38. print" \nThe donor's name and amount donated:"
  39. for i in range(0,n):
  40.     print donorsName[i],"$",amount[i]
  41.  
  42. print "\nProgram Completed......."
  43. 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:
Expand|Select|Wrap|Line Numbers
  1. dataList = []   # an empty list
  2. name = "john"
  3. amount = 75.50
  4. dataList.append((name, amount,))   # append a tuple to the list.
Sorting is done natively as well:
Expand|Select|Wrap|Line Numbers
  1. dataList.sort()
I hope this make your life a little simpler (that's what Python is all about).
Apr 17 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Benoit St-Jean | last post by:
I am wondering if the "swap bug" is present on my kernel/version of RedHat 9 as described at: http://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=89226 It looks like the OS prefers to fill the...
2
by: ma740988 | last post by:
So I'm reading the C++ coding standards(Shutter & Andrei), more specifically item 56. There's a statement: "Prefer to provide a nonmember swap function in the same namespace as your type when...
7
by: Kai-Uwe Bux | last post by:
Hi folks, I am still struggling with the rules for name lookup. Please consider: namespace xxx {
9
by: Jongmin Lee | last post by:
Hi Everybody, I have very simple code snippet to explain my problem. Class "Swap" is construncted in "Main" with two initial variables. Later, "Swap" class is going to swap those two...
4
by: Niels Dekker (no reply address) | last post by:
When calling swap as follows (as recommanded in Effective C++, 3rd Edition, by Scott Meyers), what swap is chosen to be called? using std::swap; swap(a, b); Suppose there is a global ::swap...
9
by: ma740988 | last post by:
Consider: # include <vector> # include <iostream> # include <cstdlib> # include <ctime> bool ispow2i ( double n ) {
28
by: Jess | last post by:
Hello, It is said that if I implement a "swap" member function, then it should never throw any exception. However, if I implement "swap" non- member function, then the restriction doesn't...
4
by: George2 | last post by:
Hello everyone, The following swap technique is used to make assignment operator exception safe (means even if there is exception, the current object instance's state is invariant). It used...
4
by: Robert LaMarca | last post by:
Hi, I am using numpy and wish to create very large arrays. My system is AMD 64 x 2 Ubuntu 8.04. Ubuntu should be 64 bit. I have 3gb RAM and a 15 GB swap drive. The command I have been trying...
0
by: Terry Reedy | last post by:
Robert LaMarca wrote: I do not know what numpy does, but constant arrays only need to store the dimensions and the constant value and have a getitem method that returns that constant value for...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.