# -*-coding:utf-8 -*- import time def ListRepeat(list, duration): """ call ListRepeat(list, duration), result should be like below: ['0', '1', '2', '3', '4', '5'] ['1', '2', '3', '4', '5', '6'] ['2', '3', '4', '5', '6', '7'] ['3', '4', '5', '6', '7', '8'] ['4', '5', '6', '7', '8', '9'] ['5', '6', '7', '8', '9', '0'] ['6', '7', '8', '9', '0', '1'] ['7', '8', '9', '0', '1', '2'] ['8', '9', '0', '1', '2', '3'] ['9', '0', '1', '2', '3', '4'] """ n = 0 while 1: if n <= len(list): if n <= len(list) - duration: print list[n: n + duration] else: # len(list) - duration < n < len(list): print list[n:] + list[:(duration - (len(list) - n))] else: n -= len(list) print list[n: n + duration] n += 1 time.sleep(1) a = [str(i) for i in range(10)] if __name__ == '__main__': ListRepeat(a, 6)