Parallel Lists Help

Posted on 2013-10-26
Medium Priority
Last Modified: 2013-10-29
Hello Experts,

I am having a bit of trouble again with my homework. the basic premise is I am supposed to write a program that uses parallel lists that will hold the name of a person and then their salary, then display the average salary the highest salary with persons name and then the lowest salary with the persons name. where I am stuck is the output of highest salary with person name and lowest salary with person name.

so this is what I have thus far:

numemployees = 0

numemployees = int(input("how many employees do you have? "))
if numemployees <= 0:
    print(" you have entered an invalid number!")

employees = [" "] * numemployees
salaries = [0] * numemployees
n = 0
sum = 0.0
average = 0.0

for n in range(0,numemployees):
    print("eneter the name", (n + 1))
    employees[n] = str(input())
for n in range(0,numemployees):
    print("enter the employees salary", (n + 1))
    salaries[n] = int(input())
    sum = (sum + salaries[n])
    if salaries[n] <=0 or salaries[n] >= 200000:
        print("you have entered an invalid amount try again")
        sum = (sum - salaries[n])
        print("enter the employees salary", (n - 1))
        salaries[n] = int(input())
average = (sum/numemployees)
print(" the average salary is",average)

Open in new window

It function the way i expect it to i think... maybe it's all wrong.

anyway the problem I am running into is here:
print("the highest salary is",max(salaries),employees)
print("the Lowest salary is", min(salaries),employees)

Open in new window

this is where it displays the highest and lowest salaries and the entire list of names, like so:

the average salary is 666.6666666666666
the highest salary is 900 ['bob', 'jim', 'joe']
the Lowest salary is 500 ['bob', 'jim', 'joe']

I have include the input I used to get the above output in the attachment:

the out put should look something like:
the average salary is 666.6666666666666
the highest salary is 900 joe
the Lowest salary is 500 jim

I appreciate any bit of help you can give.

Thanks you and best regards,
Question by:b_zajac
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
LVL 29

Accepted Solution

pepr earned 2000 total points
ID: 39603709
The employees is a list; hence, the list representation is printed. Think as if having paper with one column of names and second column of numbers (salaries for the names). The max gives you the maximum of the salary values, but it will not tell you on which line it was found.

There are two principal solutions: 1) you will remember index of the minimum and maximum values (and update the dedicated index variable) during input, or 2) you will find the index of the list element with the wanted value later. The name is stored at the same index in the parallel list.

There is the .index() method for sequence types -- i.e. also for lists. See http://docs.python.org/3/library/stdtypes.html#index-20

In my opinion, the second approach is more general. The same way you can postpone calculation of the average to the time after the input.

Author Comment

ID: 39603933
I guess I just don't understand the documentation, I look at this:

s.index(x[, i[, j]])	index of the first occurrence of x in s (at or after index i and before index j)	(8)

Open in new window

and it means nothing to me and the book is just as cryptic. I just don't understand I guess.

When you say index at the end like finding the value do you mean something like.

index = index + 1 (#-> in each for statement) 

#Output like this
print("the highest salary is",max(salaries),employees[index])
print("the Lowest salary is", min(salaries),employees[index])

Open in new window

LVL 29

Assisted Solution

pepr earned 2000 total points
ID: 39606790
The list type or the string type or bytearray or similar are considered sequence types. The .index() method is available to each of them. It has nothing to do with your index variable.

The [ ] brackets in the doc are used to express optional usage of the extra arguments.

If you save say max salary value like mx = max(salaries), then i = salaries.index(mx) return the position of the value in the list, and the employees[ i ] is the name of the person with the maximum salary. Is it more understandable?
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 39606856
so would it look something like this?

mx = max(salaries)
mn = min(salaries)
i = salaries.index(mx)
j = salaries.index(mn)

print(mx, employees[i])

Open in new window

do i have to count the index like

index = 0
index = index + 1

Open in new window

I did not mean to sound belligerent in my last reply, I am very frustrated that i am haveing such a difficult time wrapping my head around this concept. Thank you for trying to help me understand this concept.
LVL 29

Expert Comment

ID: 39607242
Yes, the first part of your last comment should be OK. There also is a standard built-in function named sum() that can be user for summing the salaries. That function contain a hidden loop inside. Basically you can do it using the for loop. You can also use the for loop when searching the index of the max value element. Using the .index() method is just easier, more brief, and less error prone.

Just ask what makes you trouble. You definitely need to understand all details if you want to understand more complex things later.

Author Comment

ID: 39607376
I will defiantly be seeking more info on this topic both from my instructor, corse workbook and the python cookbook i just purchased. I really appreciate the time you have taken out of your schedule to explain it to me.

Author Closing Comment

ID: 39607393
I think i get it now. .index() is built in and will keep track of all list indexes if they are parallel. and using a variable like in the examples above can be call at the end of one list and then used to coordinate that same indexed place in the adjacent list.
LVL 29

Expert Comment

ID: 39608029
I think i get it now. .index() is built in and will keep track of all list indexes if they are parallel.

No. This is not the case. Study the comments and the code below

# Here are the parallel lists.
employees = ['bob', 'jim', 'joe']
salaries =  [600, 500, 900]

# You can index each list separately. When using the same index, you get
# the related values. But the lists are not bound technically. It is only
# the agreement that the elements on the same index value. The index value
# is zero based.
print(employees[0], salaries[0])
print(employees[1], salaries[1])
print(employees[2], salaries[2])

# You can loop through the lists. The enumerate() function will add the index
# value.
print('-' * 50)
for i, name in enumerate(employees):
    print('index:', i, '\tname:', name)

# You can do the same with the salaries.
print('-' * 50)
for i, salary in enumerate(salaries):
    print('index:', i, '\tsalary:', salary)

print('-' * 50)

# The .index() method just return the index value of the element that contains
# the given value.
print('index for bob:', employees.index('bob'))
print('index for joe:', employees.index('joe'))

# Let's try .index() with the value that is not in the list. It causes
# exception to indicate the case. The exception can be caught so that
# the programmer can react to the situation.
   print('index for nobody:', employees.index('nobody'))
except ValueError as e:
   print('The name was not found in the list.')
   # If you do not use the try-except construct in such case, the program
   # stops and displays the message like... (the .__str__() method does that).
   print('Python would tell you:', e)

# Whe the exception does not happen, it behaves as if only the commands between
# try and except existed. This way, you can always used the try-except when you
# know that something wrong can happen (but not neccessarily). Here for jim.
   print('index for jim:', employees.index('jim'))
except ValueError as e:
   print('Some problem.', e)

# You tried and you have learned things. Because of that I am showing
# the alternative solution. The sum() built-in function is capable to
# sum the elements. You are already familiar with the max() and min().
# The len() function returns the length of a sequence (number of elements).
print('sum:', sum(salaries))
print('len:', len(salaries))
print('avg:', sum(salaries)/len(salaries))

min_salary = min(salaries)
max_salary = max(salaries)

print('min:', min_salary)
print('max:', max_salary)

# Now search for the index of the element with minimum salary.
imin = salaries.index(min_salary)
print('i min:', imin)

# The same for maximum.
imax = salaries.index(max_salary)
print('i max:', imax)

# The question for you: Why the try-except construct need not to be used
# in this case?

# There is the agreement that the element with the same index contains
# the name in the other list.
print('Employee with the min salary:', employees[imin])
print('Employee with the max salary:', employees[imax])

Open in new window

It prints on my console:
bob 600
jim 500
joe 900
index: 0        name: bob
index: 1        name: jim
index: 2        name: joe
index: 0        salary: 600
index: 1        salary: 500
index: 2        salary: 900
index for bob: 0
index for joe: 2
The name was not found in the list.
Python would tell you: 'nobody' is not in list
index for jim: 1
sum: 2000
len: 3
avg: 666.6666666666666
min: 500
max: 900
i min: 1
i max: 2
Employee with the min salary: jim
Employee with the max salary: joe

Open in new window


Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is about my first experience with programming Arduino.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Introduction to Processes

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question