1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| >>> a = [1,2,3,4,5] >>> a[0] 1 >>>a[-1] 5 >>> a[0:] [1,2,3,4,5] >>> a[:-1] [1,2,3,4] # a[start:end:step] >>> a[0:-1:2] [1,3] # using slice object # slice(start,end,step) >>> s = slice(0,-1,2) >>> a[s] [1,3] # Get index and item in loop >>> a = range(3) >>> for idx,item in enumerate(a): ... print (idx,item), ... (0, 0) (1, 1) (2, 2) # Transfer two list into tuple list >>> a = [1,2,3,4,5] >>> b = [2,4,5,6,8] >>> zip(a,b) [(1, 2), (2, 4), (3, 5), (4, 6), (5, 8)] # with filter >>> [x for x in range(5) if x>1] [2, 3, 4] >>> _ = ['1','2',3,'Hello',4] >>> f = lambda x: isinstance(x,int) >>> filter(f,_) [3, 4]
|