How to dynamically update values to arguments in Python loop?
Python newbie here:
I'm writing a market simulation in Python using Pysage, and want to generate an arbitrary number of agents (either buyers or sellers) using the mgr.register_actor()
function, as follows:
for name, maxValuation, endowment, id in xrange(5):
mgr.register_actor(Buyer(name="buyer001", maxValuation=100, endowment=500),"buyer001")
update name, maxValuation, endowment, id
What is a succinct, pythonic way 开发者_运维百科to run that function call so that, each time the loop is run, the values of name, maxValuation, endowment, and id are changed, e.g. to name="buyer002", name="buyer003"...; maxValuation=95, maxValuation=90...; endowment=450, endowment=400...; "buyer002", "buyer003"...
and so on.
I have tried different for-loops and list comprehensions, but haven't found a way yet to dynamically update the function arguments without running into type issues.
Thanks in advance!
You can prepare the names
, maxValuations
, and endowments
as lists (or iterators), then use zip to group corresponding elements together:
names=['buyer{i:0>3d}'.format(i=i) for i in range(1,6)]
maxValuations=range(100,75,-5)
endowments=range(500,250,-50)
for name, maxValuation, endowment in zip(names,maxValuations,endowments):
mgr.register_actor(
Buyer(name=name, maxValuation=maxValuation, endowment=endowment),name)
Regarding the format string, '{i:0>3d}'
:
I refer to this "cheat sheet" when I need to build a format string:
http://docs.python.org/library/string.html#format-string-syntax
replacement_field ::= "{" field_name ["!" conversion] [":" format_spec] "}"
field_name ::= (identifier|integer)("."attribute_name|"["element_index"]")*
attribute_name ::= identifier
element_index ::= integer
conversion ::= "r" | "s"
format_spec ::= [[fill]align][sign][#][0][width][.precision][type]
fill ::= <a character other than '}'>
align ::= "<" | ">" | "=" | "^"
"=" forces the padding to be placed after the sign (if any)
but before the digits. (for numeric types)
sign ::= "+" | "-" | " "
" " places a leading space for positive numbers
width ::= integer
precision ::= integer
type ::= "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" |
"o" | "x" | "X" | "%"
So, it breaks down like this:
field_name
/
{i:0>3d}
\\\\
\\\`-type ("d" means integer)
\\`-width
\`-alignment (">" means right adjust)
`-fill character
精彩评论