[英]Call function multiple times with a loop
I have made a function which produces a map, which I will turn into a gif by plotting 24 versions of the map representing the 24 hours in a day.我制作了一个生成地图的函数,我将通过绘制代表一天 24 小时的 24 个版本的地图将其转换为 gif。 I can call my map the following way.
我可以通过以下方式调用我的地图。
get_gif(0.0, '00:00:00', '00.png')
to print the next map I will call the following:要打印下一张地图,我将调用以下内容:
get_gif(1.0, '01:00:00', '01.png')
And so on until up to my final call which will look like this:依此类推,直到我的最后一次电话会议看起来像这样:
get_gif(23.0, '23:00:00', '23.png')
How would I make a loop so that I can make all 24 calls in one go?我将如何进行循环,以便一次性拨打所有 24 个电话? My first argument is a float between
0.0
to 23.0
(the hours of the day), then a string for the time which I am printing in the map, then another string which is the file name.我的第一个参数是
0.0
到23.0
(一天中的小时)之间的浮点数,然后是我在地图中打印的时间的字符串,然后是另一个字符串,即文件名。
For python3.6 and above:对于python3.6及以上:
for hour in range(24):
get_gif(float(hour), f"{hour:02}:00:00", f"{hour:02}.png")
What you need is a string formatter.你需要的是一个字符串格式化程序。 In python that would be something like
在 python 中,这将类似于
'{0}.png'.format(n)
In python the '{0}'.format(n) will replace the {0} with the number n.在 python 中,'{0}'.format(n) 将用数字 n 替换 {0}。 The variable n can be updated with a for loop.
变量 n 可以用 for 循环更新。 Unfortunately it seems that your file contain things like '01.png' so you cannot do
不幸的是,您的文件似乎包含“01.png”之类的内容,因此您不能这样做
'{0}.png'.format(1)
Because the name will become 1.png.因为名字会变成1.png。 For this reason you can use an if clause and use
因此,您可以使用 if 子句并使用
'0{0}.png'.format(n)
If n is smaller then 10.如果 n 小于 10。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.