day05:python列表方法

來源:互聯網
上載者:User

標籤:python   列表   

1、list.append(obj)在列表末尾添加新的對象,且一次只添加一個元素。

>>> list = [123, ‘kobego‘, [1, 2, 3], (1, 2)]>>> list.append([24, 23])>>> list[123, ‘kobego‘, [1, 2, 3], (1, 2), [24, 23]]

2、list.extend(seq)在列表末尾可一次性添加多個列表元素。

>>> list = [123, ‘kobego‘, [1, 2, 3], (1, 2)]>>> list.extend([24, 23])>>> list[123, ‘kobego‘, [1, 2, 3], (1, 2), 24, 23]

3、list.pop(obj=list[-1])移除列表中的某一個元素(預設為末尾元素),且返回該移除元素的值。

>>> list = [123, ‘kobego‘, [1, 2, 3], (1, 2)]>>> list.pop()(1, 2)>>> list[123, ‘kobego‘, [1, 2, 3]]

4、list.remove(obj)移除列表中某一個元素的第一個匹配項,沒有傳回值。

>>> list = [123, ‘kobego‘, 123, [1, 2, 3], (1, 2)]>>> list.remove(123)>>> list[‘kobego‘, 123, [1, 2, 3], (1, 2)]

5、list.index(obj)從列表中找出某個值第一個匹配項的索引位置,未找到則會報錯。

>>> list = [123, ‘kobego‘, 123, [1, 2, 3], (1, 2)]>>> list.index(123)0

6、list.insert(index, obj)將指定對象插入列表的指定位置。

>>> list = [123, ‘kobego‘, 123, [1, 2, 3], (1, 2)]>>> list.insert(1, ‘kobego24‘)>>> list[123, ‘kobego24‘, ‘kobego‘, 123, [1, 2, 3], (1, 2)]

7、list.reverse()對列表的元素進行反向排序。

>>> list = [123, ‘kobego‘, 123, [1, 2, 3], (1, 2)]>>> list.reverse()>>> list[(1, 2), [1, 2, 3], 123, ‘kobego‘, 123]

8、list.sort([func])對原列表進行排序。

>>> list = [‘james‘, ‘kobego‘, ‘curry‘]>>> list.sort()>>> list[‘curry‘, ‘james‘, ‘kobego‘]

day05: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.