我打算向许多号码发送消息,但这是我的代码和问题(代码变得更短,所需行在此处):
msg = 'test message'
phone_numbers = ['+989111111111', '+989111111112', '+989111111113', '+989111111114']
hours = range(0, 25)
minutes = range(0, 60)
for phone_number in phone_numbers:
for hour in hours:
for minute in minutes:
sndmsg(phone_number, msg, hour, minute)
我知道我的方法是错误的,因为这是输出,但是我不确定如何解决。谷歌搜索对此没有帮助。
输出:
test message to +989111111111 will be sent on 0 0
test message to +989111111111 will be sent on 0 1
test message to +989111111111 will be sent on 0 2
...
test message to +989111111112 will be sent on 0 0
test message to +989111111112 will be sent on 0 1
test message to +989111111112 will be sent on 0 2
我想要的输出将是:
test message to +989111111111 will be sent on 0 0
test message to +989111111112 will be sent on 0 1
test message to +989111111113 will be sent on 0 3
我想像上面的输出一样在每分钟内将消息发送到每个号码,我怎么能做到这一点?
尝试使用发电机生成小时和分钟,并使用电话号码进行压缩:
msg = 'test message'
phone_numbers = ['+989111111111', '+989111111112', '+989111111113', '+989111111114']
def gen_hour_min():
hours = range(0, 25)
for hour in hours:
minutes = range(0, 60)
for minute in minutes:
yield hour, minute
for phone_number, hour_min in zip(phone_numbers, gen_hour_min()):
hour, minute = hour_min
print(phone_number, hour, minute)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句