001 Python list 索引

來源:互聯網
上載者:User

標籤:[]   name   insert   copy   log   clear   names   end   color   

# Python 3 基礎文法
  ● list 索引
    ○ -1代表最後一個
    ○ list可以放置各種各樣的類型

# -*- coding: UTF-8 -*-names = [‘Mark‘, ‘coloe‘,1,2,3,4,5,3.14,True]print(type(names))print(names)print(names[1])print(names[0])print(names[-1])

 

  ● list 嵌套使用

1 # -*- coding: UTF-8 -*-2 names = [‘Mark‘, ‘coloe‘, [‘I‘, ‘Love‘, ‘PoEdu‘,‘!‘,],1,2,3,4,5,3.14,True, [‘I‘, ‘Love‘, ‘Mark‘,‘!‘,]]3 print(type(names))4 print(names)5 print(names[2])        #嵌套的list6 print(names[-1])        #嵌套的list7 print(names[-1][1], names[2][2])    #擷取嵌套list 的值 並輸出

 

  ● list append[]
    末尾加入

1 # -*- coding: UTF-8 -*-2 names = [‘Mark‘, ‘coloe‘, [‘I‘, ‘Love‘, ‘PoEdu‘,‘!‘,]]3 print(names)4 names.append(‘Google‘)5 print(names)6 names.append(‘Baidu‘)7 print(names)8 names.append(‘PoEdu‘)9 print(names)

 

  ● list insert[]
    指定位置插入

    參數1:位置

    參數2:插入的值

1 # -*- coding: UTF-8 -*-2 names = [‘Mark‘, ‘coloe‘, [ ‘Love‘, ‘PoEdu‘,‘!‘,]]3 print(names)4 names.insert(1,‘胡蘿蔔‘)5 print(names)6 names.insert(4,‘So Much !‘)7 print(names)

 

 

  ● list clear()
    清空索引

1 # -*- coding: UTF-8 -*-2 names = [‘Mark‘, ‘coloe‘, [ ‘Love‘, ‘PoEdu‘,‘!‘,]]3 names.insert(1,‘胡蘿蔔‘)4 names.insert(4,‘So Much !‘)5 names.clear()6 print(names)

 

  ● list copy()
    複製索引

1 # -*- coding: UTF-8 -*-2 names = [‘Mark‘, ‘coloe‘, [ ‘Love‘, ‘PoEdu‘,‘!‘,]]3 other = names.copy()4 print(names)5 print(other)

 

  ● list pop()
    刪除索引末尾
    參數1:刪除指定位置的資料
    感覺跟出棧一樣

1 # -*- coding: UTF-8 -*-2 names = [‘A‘, ‘B‘, ‘C‘, ‘D‘, ‘E‘, ‘F‘, ‘G‘, ‘H‘, ‘I‘, ‘J‘, ‘K‘,]3 print(names)4 names.pop()5 print(names)6 names.pop(0)7 print(names)8 names.pop(3)9 print(names)

 

  ● Python 元組
    Python的元組與列表類似,不同之處在於元組的元素不能修改。
    元組使用小括弧,列表使用方括弧。
    元組建立很簡單,只需要在括弧中添加元素,並使用逗號隔開即可。

1 # -*- coding: UTF-8 -*-2 names = (‘A‘, ‘B‘, ‘C‘, ‘D‘, ‘E‘, ‘F‘, ‘G‘, ‘H‘, ‘I‘, ‘J‘, ‘K‘)3 print(type(names))4 print(names)5 #元祖訪問6 print(names[0])7 print(names[1:5])8 print(names[6:])9 print(names[6:-1])

 

001 Python list 索引

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.