python 学习笔记 2

 第二章 元组和列表



  • Example 1

  • x='abc';y=10;z=100.2;

  • arr1=[x,y,30,z] # define an array

  • arr2=[] #define the empty array

  • print(x[0]) # output “a”, string is also an array

  • print(x[-1]) # output “c”, -1 means n-1 (n is the length)

  • print(x[-2]) # ouput 'b', -2, means n-2

  • print(len(arr1)) # output 4

  • 2 dimentional array arr3=[[10,20,30],['a', 'b', 'c']]

  • print(arr3[0] ) # [10,20,30]

  • print(arr3[0][1]) # 20

  • ['a','b'] * 2 # ['a', 'b', 'a', 'b']

  • ['a', 'b'] + [10] # ['a', 'b', 10]

  • numbers = [1,2,3,4,5,6,7,8,9,10]; print(numbers[3:6]); # 4,5,6 , index 3,4 and 5

  • numbers = [1,2,3,4,5,6,7,8,9,10]; print(numbers[-3:-1]); # 8, 9

  • numbers = [1,2,3,4,5,6,7,8,9,10]; print(numbers[-3:]); # 8, 9,10

  • numbers[:] ==> numbers

  • numbers[:3] ==> numbers[0:3]

  • *  numbers[1:8:2] ==> 2 is steps
    *  check whether some element is in a list : 2 in [1,2,3] ==> true ; 2 in ['1', '2', '3'] ==> false
    *  use min() and max() to get the min value and the max value from a list, 
    e.g., min([2,1,5]) ==> 1 ; max([2,1,5]) ==> 5
    ** if the element in the array( or list) is not number, an error will report
    e.g., min([2, '1', 5]) ==> error
    *  change string to list ==> list(strVar)
    e.g., x=list('hello') ==> x is ['h', 'e', 'l', 'l', 'o']
    *  delete one element from list
    e.g., x is ['h', 'e', 'l', 'l', 'o'] , and run del x[2] ==> x is ['h', 'e', 'l', 'o']
    *  assign by slice :x=['h', 'e', 'l', 'l', 'o']; x[1:3] = list('ABCD') ==> x=['h', 'A', 'B', 'C', 'D', 'l', 'o']
    *  use x[2:5]=[] to remove the elements 2, 3, 4
    *  x is ['e', 'A', 'B', 'C'], call x[0::2]=[100, 200]
       result : x is [100, 'A', 200, 'C']
    *  use append x is [100, 'A', 200, 'C'], x.append(200), new x is [100, 'A', 200, 'C', 200]
    *  x.count(200) is 2 ==> count means count the number of the input value
    *  x.extend() x.extend([5,6]) ==> result x is [100, 'A', 200, 'C', 200, 5, 6]
    *  index(), means get the index of a specific value x.index(5) ==> 5, x.index('A') ==> 1
    if the value to search by index() doesn't exist, an error will report
    *  insert an element x.insert(3, 'four') ==> insert value 'four' to position 3
    *  pop() remove and return the last value; x.pop(2) return the x[2] and remove it from x
    *  x.remove('C') will remove the first 'C' from x ( only remove one element, not all the element matched the criteria) , if the element doesn't exist, an error will report
    *  revert a list x.revert()
    *  sort the list x.sort() ==> all the elements should be the same type so that they could be sort
    *  sort by the length of the string ==> x.sort(key=len)
    *  revert sort ==> x.sort(reverse=True)
    *  sort by specific function ==> x.sort(cmpfunc)
    *  元组 (1,2,3)
    *  empty tuple ==> () ; one element tuple ==> (2,)
    *  change list to tuple : tuple([1,2,3])
    *  some functions used  by list or tuple
        **  cmp(x,y) compare x and y, return -1, 0, 1
        **  len(seq), calculate the length of seq
        **  list(seq), convert seq to list
        **  tuple(seq), convert seq to tuple
        **  max(args), min(args)
        **  reversed(seq)
        **  sorted(seq)

posted on 2012-02-15 22:04  learner  阅读(149)  评论(0)    收藏  举报

导航