python資料結構

來源:互聯網
上載者:User

標籤:python 資料結構

字串處理:

name=‘go die‘

    - capitalize 首字母大寫

    - casefold 將所有大寫變成小寫

    - lower 將所有大寫變小寫

    - center 文本置中

    - print(name.center(20,‘*‘))

    *******go die*******

    - count(g,start,end) 查詢字元的出現次數,start為起始位置,end為結束位置

    - endswith(‘‘) 是否以某個字元結尾

    - startswith() 是否以某個字元開頭

    - find() 找到子序列的索引位置:不存在返回-1

        index()

    - 字串格式化

        end = ‘我是:{0};年齡:{1};性別:{2}‘

        v = end.format(‘go‘,19,‘ok‘)

    - 判斷是否是數字、漢字

        isalnum()

        isalpha()

    - 是否是表示符

        isidentifier()

    - 是否全部小寫

        islower()

    - 是否全部大寫

        isupper()

    - 是否含有可列印的字元

        isprintable()

    - 元素拼接

        name = ‘go‘

        v=‘_‘.join(name)

        li=[‘fir‘,‘sec‘,‘thr‘]

        v2=‘_‘.join(li)

    - 左右填充

         name.rjust(20,‘*‘) 

    - 替換

        replace()

    - 轉換成位元組

        name.encode(encoding=‘utf-8‘)


**********************************************

int 整數

    age=4

    age.bit_length() 最少位元

    

***********************************************

bool 布爾值

v=0

v=""

v=[]

bool值都為false


*************************************************

list 列表

user_list = [‘李泉‘,‘劉一‘,‘劉康‘,‘豆豆‘,‘小龍‘]

    - 追加 append()

    - 清空 clear()

    - 淺拷貝 copy

    - 計數 count()

    - 擴充 extend(‘tom‘)

    - 刪除並擷取元素 pop(1)

    - 刪除值 remove(‘劉一‘)

    - 翻轉 reverse()

    - 排序 sort() 從小到大

        sore(reverse=True) 從大到小

**********************************************

tuple:元組的元素是不可以修改的

    user_tuple = (‘alex‘,‘eric‘,‘seven‘,‘alex‘)

    - 擷取個數 user_tuple.count(‘alex‘)

    - 擷取索引位置

            user_tuple(‘seven‘)


************************************************

字典

    dic = {‘k1‘:‘v1‘,‘k2‘:‘v2‘}

    - 清空 dic.clear()

    - 增加,如果存在則不做操作

        dic.setdefault(‘k1‘,11111)

    - 批量增加或修改

        dic.update({‘k3‘:‘v3‘,‘k4‘:‘v4‘})


************************************************

s1 = {"alex",‘eric‘,‘tony‘,‘李泉‘,‘李泉11‘}

s2 = {"alex",‘eric‘,‘tony‘,‘劉一‘}


1.s1中存在,s2中不存在

 v = s1.difference(s2)

print(v)

#### s1中存在,s2中不存在,然後對s1清空,然後在重新複製

s1.difference_update(s2)

print(s1)


2.s2中存在,s1中不存在

v = s2.difference(s1)

print(v)


3.s2中存在,s1中不存在

s1中存在,s2中不存在

 v = s1.symmetric_difference(s2)

print(v)

4. 交集

v = s1.intersection(s2)

print(v)

5. 並集

v = s1.union(s2)

print(v)

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.