python迴圈陣列下標
阿新 • • 發佈:2018-12-11
http://outofmemory.cn/code-snippet/3741/accessing-the-index-in-python-for-loops
但是有時候我們會需要在便利陣列的同時訪問下標,這時候可以藉助於enumerate函式來實現,例如:
l = [1,2,3]for index,val in enumerate(l):
print 'index is %d, val is %d' % (index,val)
#輸出下標
# for index_i,r in enumerate(rs):
# print(index_i)
# for index_j,str in enumerate(r):
# print(index_j)
def array_diff(a, b):
c=[]
for index,val in enumerate(a):
if val in b:
continue
c.append(val)
print(c)
def do():
a=[1,2,3]
b=[2]
c=[]
for index,val in enumerate(a):
if val in b:
continue
c.append(val)
print(c)
if(__name__=="__main__"):
array_diff([1,2,2,2,3,4,5], [2,5])
# do()