python 列表常用操作

來源:互聯網
上載者:User

標籤:sed   迭代   insert   反轉   一個   操作   strong   xtend   擴大   

1、 input 和 raw_input 的區別

raw_input() 直接讀取控制台的輸入

input() 接收字元時必須加雙引號,否則會報錯

2、 Python 常見的列表操作

a).相加(相當於串連)

>>> [1,2,3]+[4,5,6]

[1, 2, 3, 4, 5, 6]

b).列表乘以一個數字相當於將列表擴大N

>>> [1,2,3]*2

[1, 2, 3, 1, 2, 3]

c).append insert 的區別

insert 必須指定index,append 直接在最後加入

>>> a=[1,2,3]

>>> a.append(4)

>>> a

[1, 2, 3, 4]

>>> a=[1,2,3]

>>> a.insert(1,4)

>>> a

[1, 4, 2, 3]

d).pop remove 的區別

pop 預設是把最後一位給刪除,也接受參數index來刪除,remove是按只來刪除

>>> a=[1,2,3]

>>> a.pop()

>>> a

[1, 2]

3

>>> a=[1,2,3]

>>> a.pop(0)

1

>>> a=[2,3,4]

>>> a.remove(2)

>>> a

[3, 4]

e) x.reverse() 和 reversed(x) 傳回值有什麼區別

前者返回的是x反轉完成之後的結果,後者是返回迭代器

>>> a=[1,2,3]

>>> a.reverse()

>>> a

>>> a=[1,2,3]

>>> b=list(reversed(a))

>>> b

[3, 2, 1]

f)其他的列表操作 index、count、extend

python 列表常用操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.