[每日一講] Python系列:列表與元組

參考文檔 https://docs.python.org/zh-cn/3.7/tutorial/introduction.html#listshtml

"""
DATA STRUCTURE
Container: Sequence
—— String、List & Tuple
List can be Changed or Updated. But, Tuple can not be.
To define a list, you can use symbol '[ ]', tuple use symbol '( )'python

數據結構
容器:序列
—— 字符串、列表 & 元組
列表是可變序列。而元組不能夠。
定義一個列表,能夠使用符號'[ ]',元組則使用符號'( )'數據結構

"""app


列表的用法

  • 做爲 list 用
    見演示代碼.net

  • 做爲隊列用
    隊列 (queue) 做爲一種線性的數據結構,其特色是 FIFO (先進先出),隊列只能在隊頭作刪除操做,在隊尾作插入操做。
    在 Python 中,若是須要用到隊列,則須要使用 collections.deque() 。deque 爲雙端隊列,即在隊頭和隊尾都可進行插入或刪除操做。code

  • 做爲棧用
    棧(stack) 做爲一種線性的數據結構,其特色是 FILO (先進後出),其限定只能在棧頂進行插入和刪除操做。
    棧頂與棧底:容許元素插入與刪除的一端稱爲棧頂,另外一端稱爲棧底。
    壓棧:棧的插入操做,叫作進棧,也稱壓棧、入棧。
    彈棧:棧的刪除操做,也叫作出棧。
    在 Python 中,使用 list.append() 即爲壓棧;使用 list.pop() 即爲出棧,注意該方法是返回出棧的內容。htm


演示代碼

#! /usr/bin/python
# coding:utf-8


class ListAction:

    def __init__(self):
        pass

    @staticmethod
    def list_init():
        greeting = [None]
        print(greeting)

    @staticmethod
    def list_index():

        # note: Do not be out of range
        # 注意:別越界
        greeting = 'hello'
        print(greeting[0])

    @staticmethod
    def list_slicing():
        greeting = 'hello'
        print(greeting[2:3])

    @staticmethod
    def list_slicing_step():
        # note: the third num means the step.The num is 1 Default.
        # 注意:第三位數意味着步長,默認步長爲1
        greeting = 'hello'
        print(greeting[::2])

    @staticmethod
    def list_connect():
        greeting = 'hello'
        friend = 'world'
        print(greeting + '' + friend)

    @staticmethod
    def list_repeat():
        greeting = 'hello'
        print(greeting * 3)

    @staticmethod
    def list_in():
        greeting = input("Input one character:")
        if 'o' in str(greeting):
            print(True)
        elif 'O' not in str(greeting):
            print('O is not in the list')
        else:
            print('Input character O')

    @staticmethod
    def list_length():
        greeting = input("Input something,I can calculate the length:")
        # 字符串拼接。注意類型須相同
        print('OK, Length is' + ' ' + str(len(greeting)))

    @staticmethod
    def to_list(para):
        print(list(para))

    @staticmethod
    def list_to_string(lst):
        try:
            to_string = ''.join(lst)
            print(to_string)
        except TypeError:
            print('must be string-list')

    @staticmethod
    def act():
        lst = list()
        lst.append('li')
        lst.extend(['st'])
        print(lst)


if __name__ == '__main__':
    ListAction.list_init()
    ListAction.list_index()
    ListAction.list_slicing()
    ListAction.list_repeat()
    ListAction.list_slicing_step()
    ListAction.list_in()
    ListAction.list_connect()
    ListAction.list_length()
    ListAction.to_list('HELLO')
    ListAction.list_to_string(lst=['1', '2'])
    ListAction.act()

對列表進行分段blog

In [1]: def te():
   ...:     a = [1,2,3,4,5,6,7,8,9]
   ...:     matrix = []
   ...:     for i in range(0,len(a),5):
   ...:         matrix.append(a[i:i+5])
   ...:     print(matrix)
   ...:

In [2]: te()
[[1, 2, 3, 4, 5], [6, 7, 8, 9]]

對兩個列表的處理隊列

可參考文檔 https://blog.csdn.net/muumian123/article/details/81979084utf-8

求列表的差集-屬於 A 列表,不屬於 B 列表

方法一:
In [9]: def te():
   ...:     a = set([1,2,3])
   ...:     b = set([2,3,4])
   ...:     print(list(a-b))
   ...:

In [10]: te()
[1]


方法二:
In [11]: def te():
    ...:     a = [1,2,3]
    ...:     b = [2,3,0]
    ...:     for i in a:
    ...:         if i not in b:
    ...:             print(i)
    ...:

In [12]: te()
1
相關文章
相關標籤/搜索