天天看點

python-清單與元組

.在python中,是最基本的資料結構是序列(sequence).
  序列中的每個元素被配置設定一個序号----即元素的位置,也稱為索引(第一個索引從開始,依次類推)

.序列
   清單和元組差別在于,清單可以修改,元組不可修改。

   test = ['test hello',]
   print test[]  #resuslt is 'test hello'
   print test[]  #result is 36
   print test    #result is ['test hello',36]


   test = ['test hello',]
   value = ['value hello',]
   new = [test, value]
   print new #restulst [['test hello',40],['value hello',30]]

   備注:容器資料結構,容器基本包含其的對象的任意對象。序列和映射是兩類容器。
         序列中的每個元素都有自己的編号,而映射有一個名字(稱為鍵)


.通用序列操作。
    所有的序列都可以進行某種特定的操作,比如索引(index),分片(slicing),加(adding),乘(multplying)
    以及檢查某個元素的序列的成員。除此之外還可以計算序列的長度,找出最大元素和最小元素的内鍵函數

    .索引(index)
    test = 'hello'
    print test[] #resutl is h

    負數索引(從右邊開始查詢)
    print test[-]   #resuslt is o

    直接對傳回結果進行索引操作:
    test = raw_input("Year: ")[] #input 2016
    print test  #傳回結果是test序列的第四個元素 6



    索引示列:
    months = [
        'January',
        'Febuary',
        'March',
        'April',
        'May',
        'June',
        'July',
        'August',
        'September',
        'October',
        'November',
        'Decmber'
    ]
    #以下1-31的數字作為結尾的清單 
    endings = ['st','nd','rd'] +  * ['th']\
             +['st','nd','rd'] +  * ['th']\
             +['st']
    year = raw_input("Year: ") 
    month = raw_input("Month (1-12): ") 
    day  = raw_input('Day(1-31): ') 

    #記得要将月份和天數-1,獲得正确索引
    month_name = months[month_number -]
    ordinal = day + endings[day_number -]
    print month_name +' '+ ordinal + ' '+ year


   ..分片
    與使用索引來通路單個元素類似,可以使用分片來通路一定範圍内的元素。
    分片能過冒号隔開的兩個索引來實作

    test = 'http://www.perl.org'

    print test[:] #reulst is perl
    print test[:-]  #result is perl ,可以使用負數來操作

    test = [,,,,,,,,,]
    print test[:]  #resutl is 4,5,6
    print test[-:-] #resutl is 8,9,10

    按步長分片
    test=[,,,,,,,,,]
    print test[::] #按一個一個來分片範圍0:10 result si 1,2,3,4,5,6,7,8,9,19
    print test[::] #範圍是0:10,每兩步進行分片,result is 1,3,5,7,9
    print test[::]  # result is 1,5,9

    從右邊按步長分處
    print test[::-] # 9,8,7,6,5

    ..序列相加

    [,,] + [,,]  #result is [1,2,4,5,6]
    'hello' + 'world' #result is helloworld

    ..乘法(重複次數)
    'perl' *   #'perlperlperlperlperl'

    [] *  # result is [6,6,6,6,6]

    空清單可以用[]表示

    None是一個python内鍵值,表示這裡什麼都沒有 #perl同類值是undef,代表這裡空無一物,走開走開

    ..檢測成員(布爾值)
    test = 'hello'
    'h' in test  #result is True
    'w' in test  #result is False

    test = ['laomeng','wang','lisi'] 
    raw_input('Enter your name: ') in test
    #laomeng result is True

   ..計算長度,最小值和最大值 
      len(),
      min(),
      max()

      test=[,,,,,,]
      len(test)  #result is 7
      minx(test)  #result is 1
      max(test)   #result is 7


 .清單
   清單是可以變化的--可以改變清單的内容,并且清單有很多有用的,專門的方法

   ..list()函數
      list()函數适用于所有類型的序列,而不隻是字元串
     建立清單
     list('hello') 
      ['h','e','l','l','o']

   ..删除元素
   test =['abc','abd','abe','abf']
   del test[]  #删除第三個元素

   ..分片指派
   test = list('Perl')  #'P','e','r','l'

   test[:] = list('ar') #result is 'P','e','a','r'
   test=[,]
   test[:] =[,,]  #result is ['1,2,3,4,5]

   删除元素
   test =[,,,,]
   test[:]=[] #result is [1,5]

 .清單方法
  test =[,,]

  ..append方法用于在末尾增加新的元素
  test = [,,,]
  append.test[]  #resut is [1,2,3,4,5]

  在perl腳本有同樣的功能
  my @test = qw(1 2 3 4);
  push @test,;
  print "@test\n";  #result is 1 2 3 4 5

  ..count統計某個元素在清單出現的次數
  test = ['a','b','a','b','a','d']
  test.count('a')  # result is 3

  ..extend方法可以在序列末尾追加多個值
  test = [,,,,]
  b=[,,]
  test.extend(b)  #rest 1,2,3,4,5,5,7,8

 ..index方法用于從清單中找出某個值的位置
 test =['abc','abd','abe','abf','abg']
 test.index('abe')  #result is 2

 ..insert方法用于将對象插入清單中
 test = [,,,,]
 test.insert(,'hello') #result is 1,2,3,'hello',4,5

 ..pop方法移除清單中的一個元素,預設是最後一個元素,pop是有傳回值 
 test = [,,,,]
 test.pop()   #result is 1,2,3,4

 test.append(pop()) #result is 1,2,3,4,5

 ..remove方法用于移除清單中某個比對的值
 test =['a','b','c','d']
 test.remove('a')  #result is b,c,d

 ..reverse結果取反存放
 x=[,,,]
 x.reverse()  #result is 4,3,2,1

 x = 'hello'
 x.list(reverse()) #result is o,l,l,h

 ..sort方法是進行排序
 x=[,,,,,];
 x.sort()  #result is 1,2,4,6.7,9


 y =x[:]  #:切片包包含所有的值
 y.sort()  #result is 1,2,4,6.7,9

.元組: 不可改變序列
,,  #restult is (1,2,3)
()    #空元組

..tulpe函數功能與list函數功能基本上一樣
tulpe([,,])  #result is (1,2,3)

.元組的基本操作
x = ,,
x[]  #result is 2
x[:] #result is 1,2


總結:
  序列:是一種資料結構,它包含了元素都行了編号(從開始).
        序列包括了清單,字元串,元組。其中清單可以改變修改的
        而元組是不可以修改的。通過分片捷捷操作符可以通路序列的一部分(範圍),其中分片需要兩個索引來
        指出分片的起始和結束位置。

   序列成員檢測。in操作符可以檢查一個值是否存在于序列中。