Solved

# Parallel Lists Help

Posted on 2013-10-26
308 Views
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)
``````

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)
``````

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

Thanks you and best regards,
Zajac
New-Text-Document.txt
PoC-employees-n-.py
0
Question by:b_zajac
[X]
###### 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
• 4
• 4

LVL 29

Accepted Solution

pepr earned 500 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.
0

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)
``````

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])
``````
0

LVL 29

Assisted Solution

pepr earned 500 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?
0

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])
print(mn,employees[j])
``````

do i have to count the index like

``````index = 0
index = index + 1
``````

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.
0

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.
0

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.
0

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.
0

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
``````#!python3

# 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.
try:
print('index for nobody:', employees.index('nobody'))
except ValueError as e:
# 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.
try:
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])
``````
It prints on my console:
``````c:\tmp\___python\b_zajac\Q_28278159>a.py
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
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
``````
0

## Featured Post

Question has a verified solution.

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

### Suggested Solutions

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
###### Suggested Courses
Course of the Month7 days, 4 hours left to enroll