"It would seem silly that Python would not have this kind of capability, because if not you wouldn't be able to redefine any variable passed through a for loop if it is part of a list that is being passed through." - That's how most programming languages work. To allow this capability would be bad because it would create something called side-effects, which make code obtuse.
Additionally this is a common programming pitfall because you should keep data out of variable names: see http://nedbatchelder.com/blog/201112/keep_data_out_of_your_variable_names.html (especially the list of similar questions; even if you aren't dealing with variables names, you are at least trying to deal with the variable namespace). The remedy is to work at "one level higher": a list or set in this case. This is why your original question is not reasonable. (Some versions of python will let you hack the locals()
dictionary, but this is unsupported and undocumented behavior and very poor style.)
You can however force python to use side-effects like so:
scores = [99.1, 78.3, etc.]
for i,score in enumerate(scores):
scores[i] = int(score)
the above will round scores down in the scores
array. The right way to do this however (unless you are working with hundreds of millions of elements) is to recreate the scores
array like so:
scores = [...]
roundedScores = [int(score) for score in scores]
If you have many things you want to do to a score:
scores = [..., ..., ...]
def processScores(scores):
'''Grades on a curve, where top score = 100%'''
theTopScore = max(scores)
def processScore(score, topScore):
return 100-topScore+score
newScores = [processScore(s,theTopScore) for s in scores]
return newScores
sidenote: If you're doing float calculations, you should from __future__ import division
or use python3, or cast to float(...)
explicitly.
If you really want to modify what is passed in, you can pass in a mutable object. The numbers you are passing in are instances of immutable objects, but if for example you had:
class Score(object):
def __init__(self, points):
self.points = points
def __repr__(self):
return 'Score({})'.format(self.points)
scores = [Score(i) for i in [99.1, 78.3, ...]]
for s in scores:
s.points += 5 # adds 5 points to each score
This would still be a non-functional way to do things, and thus prone to all the issues that side-effects cause.