我有一个这样的列表,从0到1000。
x = ['0_1','0_2','0_3' ..., '1000_1','1000_2','1000_3']
每当索引中的字符变化时,我都需要对它进行计数,如下所示。
list_leng = len(x)
for i in range(0, list_leng):
y = x[i]
z = y[0]
print this should iterate through all the list and only print when the z number changes ' # how should I make it print '
答案 0 :(得分:1)
如果我理解你的问题,答案必须是这样的;
comparisonText = ""
for each in x:
preNumber = each.split('_')[0]
if comparisonText != preNumber:
print preNumber, 'is different than previous.'
comparisonText = preNumber #change the comparisonText