如果我有这样的代码更好循环,字符串处理(蟒)
s = 'abcdefghi'
for grp in (s[:3],s[3:6],s[6:]):
print "'%s'"%(grp)
total = calc_total(grp)
if (grp==s[:3]):
# more code than this
p = total + random_value
x1 = my_function(p)
if (grp==s[3:6]):
# more code than this
p = total + x1
x2 = my_function(p)
if (grp==s[6:]):
# more code than this
p = total + x2
x3 = my_function(p)
如果组是第一组,该组执行代码,如果该组是第二组,使用所述执行代码从第一组执行的代码生成的值,对于第三组使用从第二组代码生成的值生成的值相同:
如何整理这些以使用更好的循环?
感谢
对不起,我忘了包含我的'total = calc_total(grp)'语句 – joec 2010-03-02 18:30:54