如何在xrange中'格式化'我的迭代器以添加“标准化”后缀?像“nice_name_1”,“nice_name_2”等。我的当前迭代器增加了3,因为我在其中使用其他for循环。谢谢
objs = ['pencil','pen','keyboard','table','phone']
for i in xrange(0, len(objs), 3):
#...
for n, obj in enumerate(objs[i:i+3]):
print '...'
#...
print 'nice_name_' + str(i)
# Result:
...
...
...
nice_name_0
...
...
nice_name_3
答案 0 :(得分:1)
print 'nice_name_' + str(i // 3)
或者最好
print('nice_name_{}'.format(i//3 + 1)) # start counting at 1