我有:
count = 0
i = 0
while count < len(mylist):
if mylist[i + 1] == mylist[i + 13] and mylist[i + 2] == mylist[i + 14]:
print mylist[i + 1], mylist[i + 2]
newlist.append(mylist[i + 1])
newlist.append(mylist[i + 2])
newlist.append(mylist[i + 7])
newlist.append(mylist[i + 8])
newlist.append(mylist[i + 9])
newlist.append(mylist[i + 10])
newlist.append(mylist[i + 13])
newlist.append(mylist[i + 14])
newlist.append(mylist[i + 19])
newlist.append(mylist[i + 20])
newlist.append(mylist[i + 21])
newlist.append(mylist[i + 22])
count = count + 1
i = i + 12 …Run Code Online (Sandbox Code Playgroud) list=['a','a','x','c','e','e','f','f','f']
i=0
count = 0
while count < len(list)-2:
if list[i] == list[i+1]:
if list [i+1] != list [i+2]:
print list[i]
i+=1
count +=1
else:print "no"
else:
i +=1
count += 1
Run Code Online (Sandbox Code Playgroud)
我越来越:
else:print "no"
^
IndentationError: unexpected indent
Run Code Online (Sandbox Code Playgroud)
我试图只打印匹配以下元素的elts,但不打印后面的元素.我是Python的新手,我不确定为什么这不起作用.
mylist="'a','b','c'"
count=0
i=0
while count< len(mylist):
if mylist[i]==mylist[i+1]:
print mylist[i]
count +=1
i +=1
Run Code Online (Sandbox Code Playgroud)
错误:
File "<string>", line 6, in <module>
IndexError: string index out of range
Run Code Online (Sandbox Code Playgroud)
我假设当它到达最后一个(第n个)元素时,它找不到n + 1来比较它,所以它给了我一个错误.
有趣的是,我认为我之前已经完成了这个并且没有在更大的列表中出现这个问题:这是一个例子(由Raymond Hettinger负责修复它)
list=['a','a','x','c','e','e','f','f','f']
i=0
count = 0
while count < len(list)-2:
if list[i] == list[i+1]:
if list [i+1] != list [i+2]:
print list[i]
i+=1
count +=1
else:
print "no"
count += 1
else:
i +=1
count += 1
Run Code Online (Sandbox Code Playgroud)
为了按照我尝试的方式浏览列表,是否有任何修复以便我不会"超出范围?" 我计划在一个非常大的列表上实现它,例如,我必须检查"list [i] == list [i + 16]".在将来,我想添加条件,如"if int(mylist [i + 3]) …
任何人都可以在此代码中看到错误吗?我认为它应该打印two1 [i],但它什么都不打印.
two1=[1,2,10,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2]
count = 0
i = 0
while count < 4:
#if i == len(two1)-6: break
#else:
if abs(int(two1[i+2]) - int(two1[i+3]))>10 and abs(int(two1[i+4]) -abs(int(two1[i+5]))<10
print two1[i]
count = count + 1
i = i + 6
if abs(int(two1[i+2]) - int(two1[i+3]))<10 and abs(int(two1[i+4]) - abs(int(two1[i+5])) > 10:
print two1[i]
count = count + 1
i = i + 6
else:
count = count + 1
i = i + 6
Run Code Online (Sandbox Code Playgroud)