简体   繁体   中英

How can I print X quantity of strings in Y amount of time?

Is really bothering me to try figure this out. I can explain myself: I need for example, to print 5 strings per second, so in 1 second I need to print those 5 strings, and the next second another 5. How can I divide the amount of time a loop (as I think it should be) runs in a second, so I can print the 5 strings in that time, and printed in equaly amount of time?

["text1", "text2", "text3", "text4", "text5"]

each one printed in 200ms?

how can I control in a loop the time of each iteration?

import time

start=time.time()    
for k in tablestrings:
    print k
    time.sleep(0.2)

will work fine, but might give you some problems if you're looking for high precision

import time

start=time.time()    
for k in range(len(tablestrings)):
    release=False
    while not release:
        if time.time()>start+0.2*k:
            release=True
    print tablestrings[k]

should derive less from the 1 print every 0.2 seconds rule

Try something like:

import time

texts = ["text1", "text2", "text3", "text4", "text5"]

for text in texts:
  print(text)
  time.sleep(0.2)

https://docs.python.org/2/library/time.html for Python 2.

https://docs.python.org/3/library/time.html for Python 3.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM