Is there an elegant, Pythonic way to count processed data?
本问题已经有最佳答案,请猛点这里访问。
我经常在循环中执行耗时的处理步骤。下面的方法是我如何跟踪处理的位置。在运行脚本时,是否有一种更优雅的、Python式的计算处理数据的方法?
1 2 3 4 5 6 7 | n_items = [x for x in range(0,100)] counter = 1 for r in n_items: # Perform some time consuming task... print"%s of %s items have been processed" % (counter, len(n_items)) counter = counter + 1 |
是的,
1 2 3 | for i,r in enumerate(n_items,1): # Perform some time consuming task print('{} of {} items have been processed'.format(i, len(n_items))) |
第二个参数确定