如何在“ .get(startindex [,endindex])”表达式中将变量用作索引而不是数字?我想要这样的东西:
for i in range(1, n):
a = text.get("i.0", "i.end" )
b = text.get("i+1.0", "i+1.end" )
代替:
a = text.get("1.0", "1.end" )
b = text.get("2.0", "2.end" )
a = text.get("3.0", "3.end" )
b = text.get("4.0", "4.end" ) etc...
在第一个代码序列中,我收到“错误文本索引“ i.0””错误。
仅凭这些信息很难说出它的目的,但是直接回答您的问题,您可以使用字符串插值 python.org如.format方法:
for i in range(1, n):
a = text.get("{0}.0".format(i), "{0}.end".format(i) )
b = text.get("{0}.0".format(i + 1), "{0}.end".format(i + 1))
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句