Python基本數據類型之列表

一、列表list是Python中非常常見的數據類型,所以掌握它的使用是非常有必要的

1、列表定義: list = [var1, var2, var3...]

2、列表的增、刪、改、查

3、列表腳本操作符

4、列表類中的函數方法

5、列表與字符串類型的轉換操作

 

二、列表練習源碼加註釋

 

# !/usr/bin/env python
# -*- coding:utf8 -*-
# 列表:list

tempstr = """                           -----序列的特點概述-----
序列是Python中最基本的數據結構。序列中的每個元素都有索引,第一個索引是0,後面依次遞增。
序列的內置類型,列表、元組、字典、集合。
序列都可以進行的操作包括索引,切片,加,乘,檢查成員。
Python已經內置確定序列的長度以及確定最大和最小的元素的方法。
列表是最常用的Python數據類型,它可以作爲一個方括號內的逗號分隔值出現。
列表的數據項不需要具有相同的類型,可以是任意類型"""
print(tempstr)

print("\n-----列表的定義、增刪改查:list = [var1, var2, var3...]-----")
# <editor-fold desc="列表的定義、增刪改查">
list1 = ['Google', 'wangdoudou', 1997, 2000]
list2 = [1, 2, 3, 4, 5]
list3 = ["a", "b", "c", "d"]

print("\n使用索引訪問列表中的單個元素:list1[0]:", list1[0])
print("使用指定區間訪問列表中的元素:list2[1:5]:", list2[1:5])
print("從右側開始讀取倒數第二個元素:list2[-2]:", list2[-2])
print("輸出從指定索引開始後的所有元素:list2[1:]:", list2[1:])

print("\nlist1中原始的第三個元素爲 : ", list1[2])
list1[2] = 2018
print("list1中更新後(list1[2] = 2018)的第三個元素爲:", list1[2])

print("\nlist1的原始列表爲:", list1)
del list1[0]
print("list1刪除第一個元素之後的列表爲:", list1)
# </editor-fold>

print("\n-----列表腳本操作符-----")
# <editor-fold desc="列表腳本操作符">
list4 = ["he", 269, 'a', "雷嚎啊"]
list5 = [1, 2, 3, 4, 5]

print("list4的列表長度爲:", len(list4))
print("list4和list5相加的結果爲:", list4 + list5)
print("list5的乘法運算結果爲:", list5 * 5)
print("判斷列表中是否包含要查找的元素:", 3 in list5)

list5 += [6, 7, 8, 9]
print("使用+=符號拼接列表list5 += [6, 7, 8, 9]:", list5)

for item in list5:
    print("列表的迭代(循環)", item)

list6 = ['a', 'b', 'c']
list7 = [1, 2, 3]
list8 = [list6, list7]
print("嵌套後的列表爲:", list8)
print("根據索引查看嵌套列表中的元素list8[0]:", list8[0])
print("查看嵌套中的元素:", list8[0][1])
# </editor-fold>

print("\n-----列表函數&方法-----")
# <editor-fold desc="列表函數&方法">
list9 = ['a', 'b', 'c']
print("list9的原始元素爲:", list9)
list10 = [1, 2, 3]
print("list10的原始元素爲:", list9)

list9.append('d')
print("list.append(self, p_object):在列表末尾添加新的對象,list9 =", list9)
print("list.count(self, value):統計某個元素在列表中出現的次數:", list9.count('a'))

list11 = list(range(5))  # 創建 0-4 的列表
list10.extend(list11)
print("list.extend(self, iterable):在列表末尾一次性追加另一個序列中的多個值(用新列表擴展原來的列表:", list10)

print("list.index(self, value, start=None, stop=None):從列表中找出某個值第一個匹配項的索引位置,'d'在list9中的索引是:", list9.index('d'))

list10.insert(3, 50)
print("list.insert(self, index, p_object):將指定對象插入列表的指定位置,在list10的索引爲3的位置插入值50後的結果爲:", list10)

temp1 = list9.pop()
print("list.pop([index=-1]]):移除列表中的一個元素,index -- 可選參數,要移除列表元素的索引值,不能超過列表總長度,默認爲 index=-1,刪除最後一個列表值:默認移除結果", list9, temp1)
temp2 = list9.pop(2)
print("list.pop([index=-1]]):指定索引移除結果", list9, temp2)

list10.remove(3)
print("list.remove(obj):移除列表中某個值的第一個匹配項:", list10)

list10.reverse()
print("list.reverse():反向列表中元素:", list10)

list10.sort()
print("list.sort(cmp=None, key=None, reverse=False):對原列表進行排序,使用默認排序的結果是:", list10)
list10.sort(reverse=True)
print("list.sort(reverse=True),使用倒序排序的結果是:", list10)


def takeSecond(elem):
    return elem[1]  # 獲取列表的第二個元素


templist = [(2, 2), (3, 4), (4, 1), (1, 3)]
templist.sort(key=takeSecond)  # 指定第二個元素排序
print('list.sort(key=takeSecond):使用指定規則進行排序的結果是:', templist)

list11 = [123, "xdsf", 'd', "呵呵"]
print("原始列表爲:", list11)
list11.clear()
print("list.clear():清空列表,使用clear清空後的列表爲:", list11)

list12 = [123, "xdsf", 'd', "呵呵"]
list13 = list12.copy()
print("list.copy():複製列表:", list13)
# </editor-fold>

print("\n-----列表與字符串類型的轉換-----")
# <editor-fold desc="列表與字符串類型的轉換">
strtest = "dstgdesghrehrerey"
new_list = list(strtest)
print("將字符串‘dstgdesghrehrerey’轉換成列表,列表中的元素是字符串的每一個單個元素,結果是:", new_list)

new_list1 = ["wangdoudou", 123, 'ab']
new_str = ""
for item in new_list1:
    new_str += str(item)
print("當列表中有數字和字符串類型時,需要使用循環先將列表中每個元素轉換成字符串,再做拼接:", new_str)

new_list2 = ["dsgfds", "dsegew", "tyutyu", "asawer"]
new_str2 = "".join(new_list2)
print("當列表中是純字符串的時候,使用join()函數直接進行字符串連接:", new_str2)
# </editor-fold>

 

三、練習源碼輸出實例:


                                                                           -----序列的特點概述-----
序列是Python中最基本的數據結構。序列中的每個元素都有索引,第一個索引是0,後面依次遞增。
序列的內置類型,列表、元組、字典、集合。
序列都可以進行的操作包括索引,切片,加,乘,檢查成員。
Python已經內置確定序列的長度以及確定最大和最小的元素的方法。
列表是最常用的Python數據類型,它可以作爲一個方括號內的逗號分隔值出現。
列表的數據項不需要具有相同的類型,可以是任意類型

-----列表的定義、增刪改查:list = [var1, var2, var3...]-----

使用索引訪問列表中的單個元素:list1[0]: Google
使用指定區間訪問列表中的元素:list2[1:5]: [2, 3, 4, 5]
從右側開始讀取倒數第二個元素:list2[-2]: 4
輸出從指定索引開始後的所有元素:list2[1:]: [2, 3, 4, 5]

list1中原始的第三個元素爲 :  1997
list1中更新後(list1[2] = 2018)的第三個元素爲: 2018

list1的原始列表爲: ['Google', 'wangdoudou', 2018, 2000]
list1刪除第一個元素之後的列表爲: ['wangdoudou', 2018, 2000]

-----列表腳本操作符-----
list4的列表長度爲: 4
list4和list5相加的結果爲: ['he', 269, 'a', '雷嚎啊', 1, 2, 3, 4, 5]
list5的乘法運算結果爲: [1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
判斷列表中是否包含要查找的元素: True
使用+=符號拼接列表list5 += [6, 7, 8, 9]: [1, 2, 3, 4, 5, 6, 7, 8, 9]
列表的迭代(循環) 1
列表的迭代(循環) 2
列表的迭代(循環) 3
列表的迭代(循環) 4
列表的迭代(循環) 5
列表的迭代(循環) 6
列表的迭代(循環) 7
列表的迭代(循環) 8
列表的迭代(循環) 9
嵌套後的列表爲: [['a', 'b', 'c'], [1, 2, 3]]
根據索引查看嵌套列表中的元素list8[0]: ['a', 'b', 'c']
查看嵌套中的元素: b

-----列表函數&方法-----
list9的原始元素爲: ['a', 'b', 'c']
list10的原始元素爲: ['a', 'b', 'c']
list.append(self, p_object):在列表末尾添加新的對象,list9 = ['a', 'b', 'c', 'd']
list.count(self, value):統計某個元素在列表中出現的次數: 1
list.extend(self, iterable):在列表末尾一次性追加另一個序列中的多個值(用新列表擴展原來的列表: [1, 2, 3, 0, 1, 2, 3, 4]
list.index(self, value, start=None, stop=None):從列表中找出某個值第一個匹配項的索引位置,'d'在list9中的索引是: 3
list.insert(self, index, p_object):將指定對象插入列表的指定位置,在list10的索引爲3的位置插入值50後的結果爲: [1, 2, 3, 50, 0, 1, 2, 3, 4]
list.pop([index=-1]]):移除列表中的一個元素,index -- 可選參數,要移除列表元素的索引值,不能超過列表總長度,默認爲 index=-1,刪除最後一個列表值:默認移除結果 ['a', 'b', 'c'] d
list.pop([index=-1]]):指定索引移除結果 ['a', 'b'] c
list.remove(obj):移除列表中某個值的第一個匹配項: [1, 2, 50, 0, 1, 2, 3, 4]
list.reverse():反向列表中元素: [4, 3, 2, 1, 0, 50, 2, 1]
list.sort(cmp=None, key=None, reverse=False):對原列表進行排序,使用默認排序的結果是: [0, 1, 1, 2, 2, 3, 4, 50]
list.sort(reverse=True),使用倒序排序的結果是: [50, 4, 3, 2, 2, 1, 1, 0]
list.sort(key=takeSecond):使用指定規則進行排序的結果是: [(4, 1), (2, 2), (1, 3), (3, 4)]
原始列表爲: [123, 'xdsf', 'd', '呵呵']
list.clear():清空列表,使用clear清空後的列表爲: []
list.copy():複製列表: [123, 'xdsf', 'd', '呵呵']

-----列表與字符串類型的轉換-----
將字符串‘dstgdesghrehrerey’轉換成列表,列表中的元素是字符串的每一個單個元素,結果是: ['d', 's', 't', 'g', 'd', 'e', 's', 'g', 'h', 'r', 'e', 'h', 'r', 'e', 'r', 'e', 'y']
當列表中有數字和字符串類型時,需要使用循環先將列表中每個元素轉換成字符串,再做拼接: wangdoudou123ab
當列表中是純字符串的時候,使用join()函數直接進行字符串連接: dsgfdsdsegewtyutyuasawer
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章