How would I have my code remove the lowest integer? - python

def calc_average(scores)
x = scores[1:]
return (sum(x) / float(len(x)))
Here is some code that is supposed to find the average of a list of numbers, but not count the lowest. I'm not sure how to find the lowest number in the list, remove it from it, and then find the average of that. Can anyone help? Thanks! (The lowest number isn't always the first number in the list, that's the mistake I made...)

You don't have to literally remove the item from the list; you can just remove it from the calculation.
def calc_average(scores)
return (sum(scores) - min(scores)) / float(len(x) - 1)

I would sort the list then romove the first element.
a = [ 3,2,6,3,8,7,2,1]
a.sort()
a = a[1:]
print a
print float(sum(a))/len(a)
The benefit of doing it this way, even though it is more work for just removing the single lowest value is that it is salable. Let's say you wanted to remove the lowest 2 or three values, that would be easy once you have the list sorted.

You can use the min() function to find the smallest value of a list, and then you can remove the value with the list.remove() function.
Check this out for reference https://docs.python.org/2/library/functions.html#min or https://docs.python.org/3.6/library/functions.html#min

You could use
return float((sum(scores) - min(scores))) / (len(scores) - 1)
That finds the sum of all the scores, then removes the smallest score, before dividing by the number of scores after the removal.
Note there is no error-checking here, so this will not work properly for a list of length zero or one. That float() is there to ensure floating point division in Python 2.x. This uses the full speed of Python functions, but it does loop over the scores twice. A hand-coded function would be faster in looping only once but would slow down again in executing multiple commands.

Related

Finding the middle of a list

So I am trying to find the median of the list "revenues" which will be named "base_revenue".
Comments:
#Assume that revenues always has an odd number of members
#Write code to set base_revenue = midpoint of the revenue list
#Hint: Use the int and len functions
revenues = [280.00, 382.50, 500.00, 632.50, 780.00]
{def findMiddle(revenues):
middle = float(len(revenues))/2
if middle % 2 != 0:
return revenues[int(middle - .5)]
else:
return (revenues[int(middle)], revenues[int(middle-1)])}
I'm getting invalid syntax. The median function itself works, but maybe there is a more efficient way to do it.
Hint: the answer to this is far simpler than you've made it. You can even do it in a single line, unless your instructor specifically requires you to define a function.
You're told the list will always have an odd number of items; all you need is the index of the middle item. Remember that in Python, indices start at 0. So, for instance, a list of length 5 will have its middle element at index 2. A list of length 7 will have its middle element at index 3. Notice a pattern?
Your assignment also reminds you about len(), which finds the length of something (such as a list), and int(), which turns things (if possible) into integers. Notably, it turns a floating-point number into the the closest integer at or below it (a "floor" function); for instance it turns 2.5 into 2.
Can you see how you might put those together to programmatically find the midpoint index?

Python disregarding zeros when sorting numbers in a list

I'm trying to sort a list of dollar amounts from lowest to highest using python's built in sort ability, but when I call on it, it sorts the numbers super screwy. It starts at $10,000 then goes up to $19,0000 (which is the highest) then jumps down to $2,000 and counts up from there ostensibly because 2 is bigger than 1. I don't know how to correct for this. The code I've used is below.
numbers=[['$10014.710000000001'], ['$10014.83'],['$11853.300000000001'],
['$19060.010000000006'],['$2159.1099999999997'],['$3411.1400000000003']]
print(sorted(numbers))
The key insight here is that the values in your list are actually strings, and strings are compared lexically: each character in the string is compared one at a time until the first non-matching character. So "aa" sorts before "ab", but that also means that "a1000" sorts before "a2". If you want to sort in a different way, you need to tell the sort method (or the sorted function) what it is you want to sort by.
In this case, you probably should use the decimal module. And you want the key attribute of the sort method. This will sort the existing list you have, only using the converted values during the sorting process.
import decimal
def extract_sortable_value(value):
# value is a list, so take the first element
first_value = value[0]
return decimal.Decimal(first_value.lstrip('$'))
numbers.sort(key=extract_sortable_value)
Equivalently, you could do:
print(sorted(numbers, key=extract_sortable_value))
Demo: https://repl.it/repls/MiserableDarkPatches
You are not sorting numbers but strings, which explains the "weird" result. Instead, change your type to float and sort the resulting list:
In [3]: sorted([[float(el[0][1:])] for el in numbers])
Out[3]:
[[2159.1099999999997],
[3411.1400000000003],
[10014.710000000001],
[10014.83],
[11853.300000000001],
[19060.010000000006]]
I need the el[0] because every number is inside its own list, which is not a good style, but I guess you have your reasons for this. The [1:] strips away the $ sign.
EDIT really good point made in the comments. More robust solution:
from decimal import Decimal
import decimal
decimal.getcontext().prec = 4
sorted([Decimal(el[0][1:]) for el in numbers])
Out[8]:
[Decimal('2159.1099999999997'),
Decimal('3411.1400000000003'),
Decimal('10014.710000000001'),
Decimal('10014.83'),
Decimal('11853.300000000001'),
Decimal('19060.010000000006')]
Your numbers are currency values. So as pointed out in the comments below, it might make sense to use Python's decimal module which offers several advantages over the float datatype. (See link for further information.)
If, however, this is only an exercise for better getting to know Python, as I suspect. You might look for a simpler solution:
The reason, why your sorting doesn't work, is because your numbers are stored in the list inside another list as a string. You have to convert them to integers or floats before sorting has the effect you're looking for:
numbers=[
['$10014.710000000001'],
['$10014.83'],
['$11853.300000000001'],
['$19060.010000000006'],
['$2159.1099999999997'],
['$3411.1400000000003']
]
numbers_float = [float(number[0][1:]) for number in numbers]
numbers_float.sort()
print(numbers_float)
Which prints:
[2159.1099999999997, 3411.1400000000003, 10014.710000000001, 10014.83, 11853.300000000001, 19060.010000000006]
When you look at float(number[0][1:]), then [0] takes the first (and only) number of your (inner) number list, [1:] strips the $ sign and finally float does the conversion to floating point number.
If you want the $ sign back:
for number in numbers_float:
print("${}".format(number))
Which prints:
$2159.1099999999997
$3411.1400000000003
$10014.710000000001
$10014.83
$11853.300000000001
$19060.010000000006

Optimizing min function for changing lists

I am dealing with a problem where I need to keep track of the minimum number in a list. However, this list is constantly diminishing, say like from a million elements to a single element. I was looking for a way to avoid checking the minimum value everytime I got a one element smaller list. Like keeping track of the minimum element and if it is removed the next minimum becomes the minimum. I want to accomplish this in linear time.(It should be achievable given the mechanics of the problem)
What I thought of since I started that, I can use collections Counter to count elements in the list. Then I find the minimum(already O(2*n)), and everytime I remove an element, I subtract 1 from the value of the dictionary key. However when the minimum number's count is depleted, I would still require to find the second minimum element so it could replace it.
Please help me find a solution to this. I would say this is an interesting problem.
Let's say your program would take some time to sort that list
a = [10,9,10,8,7,6,5,4,3,2,1,1,1,0] # you're just removing
a = sorted(a) #sort ascending
# then you remove stuff from your list
# but always a[0] is minimum element
min = a[0] #you must be careful, there must be at least one item so check that before
#getting the min
So there is no need for searching it every time

Insert number to a list

I have an ordered dictionary like following:
source =([('a',[1,2,3,4,5,6,7,11,13,17]),('b',[1,2,3,12])])
I want to calculate the length of each key's value first, then calculate the sqrt of it, say it is L.
Insert L to the positions which can be divided without remainder and insert "1" after other number.
For example, source['a'] = [1,2,3,4,5,6,7,11,13,17] the length of it is 9.
Thus sqrt of len(source['a']) is 3.
Insert number 3 at the position which can be divided exactly by 3 (eg. position 3, position 6, position 9) if the position of the number can not be divided exactly by 3 then insert 1 after it.
To get a result like folloing:
result=([('a',["1,1","2,1","3,3","4,1","5,1","6,3","7,1","11,1","13,3","10,1"]),('b',["1,1","2,2","3,1","12,2"])]
I dont know how to change the item in the list to a string pair. BTW, this is not my homework assignment, I was trying to build a boolean retrival engine, the source data is too big, so I just created a simple sample here to explain what I want to achive :)
As this seems to be a homework, I will try to help you with the part you are facing problem with
I dont know how to change the item in the list to a string pair.
As the entire list needs to be updated, its better to recreate it rather than update it in place, though its possible as lists are mutable
Consider a list
lst = [1,2,3,4,5]
to convert it to a list of strings, you can use list comprehension
lst = [str(e) for e in lst]
You may also use built-in map as map(str,lst), but you need to remember than in Py3.X, map returns a map object, so it needs to be handled accordingly
Condition in a comprehension is best expressed as a conditional statement
<TRUE-STATEMENT> if <condition> else <FALSE-STATEMENT>
To get the index of any item in a list, your best bet is to use the built-in enumerate
If you need to create a formatted string expression from a sequence of items, its suggested to use the format string specifier
"{},{}".format(a,b)
The length of any sequence including a list can be calculated through the built-in len
You can use the operator ** with fractional power or use the math module and invoke the sqrt function to calculate the square-root
Now you just have to combine each of the above suggestion to solve your problem.

Data Structures - lists

This is homework but the lesson gives me the answer already. I'm having trouble putting the words from the answer to the line of code
#Calculate all the primes below 1000
result = [1]
candidates = range(3, 1000)
base = 2
product = base
while candidates:
while product < 1000:
if product in candidates:
candidates.remove(product)
product = product + base
result.append(base)
base = candidates[0]
product = base
del candidates[0]
result.append(base)
print result
This is a version of "The Sieve of Erastothenes."
This is the explanation that was given to me.
New things in this example…
The built-in function range actually returns a list that can be used like all other lists. (It includes the first index, but not the last.) A list can be used as a logic variable. If it is not empty, then it is true — if it is empty, then it is false. Thus, while candidates means “while the list named candidates is not empty” or simply “while there are still candidates”. You can write if someElement in someList to check if an element is in a list. You can write someList.remove(someElement) to remove someElement from someList. You can append an element to a list by using someList.append(something). Actually, you can use + too (as in someList = someList+[something]) but it is not as efficient. You can get at an element of a list by giving its position as a number (where the first element, strangely, is element 0) in brackets after the name of the list. Thus someList[3] is the fourth element of the list someList. (More on this below.) You can delete variables by using the keyword del. It can also be used (as here) to delete elements from a list. Thus del someList[0] deletes the first element of someList. If the list was [1,2,3] before the deletion, it would be [2,3] afterwards.
Before going on to explaining the mysteries of indexing list elements, I will give a brief explanation of the example.
This is a version of the ancient algorithm called “The Sieve of Erastothenes” (or something close to that). It considers a set (or in this case, a list) of candidate numbers, and then systematically removes the numbers known not to be primes. How do we know? Because they are products of two other numbers.
We start with a list of candidates containing numbers [2..999] — we know that 1 is a prime (actually, it may or may not be, depending on who you ask), and we wanted all primes below 1000. (Actually, our list of candidates is [3..999], but 2 is also a candidate, since it is our first base). We also have a list called result which at all times contains the updated results so far. To begin with, this list contains only the number 1. We also have a variable called base. For each iteration (“round”) of the algorithm, we remove all numbers that are some multible of this base number (which is always the smallest of the candidates). After each iteration, we know that the smallest number left is a prime (since all the numbers that were products of the smaller ones are removed — get it?). Therefore, we add it to the result, set the new base to this number, and remove it from the candidate list (so we won’t process it again.) When the candidate list is empty, the result list will contain all the primes. Clever, huh?
What I don't understand is where they say, 'we remove all numbers that are some multiple of this base number.' Where is that in the line of code? Can someone explain line by line what the program is doing? I am a newb at this trying to understand the mechanics on each line of code and why. Thanks for any assistance.
At the start of each of the while candidates: loops, product equals base. Then in that loop you have another loop, while products < 1000. At the end of this loop you increment product by base. So product goes through each multiple of base. You then remove all the values of product which is where you "remove multiples of the base number".
Basically what the program is doing is:
...
set product to base
for each candidate
for each multiple of base, referred to as 'product'
remove product from candidates
set base to new value
reset product to new base
...

Categories

Resources