1. 程式人生 > >一起學python 6 列表、元組、字典、運算子、集合 方法總結

一起學python 6 列表、元組、字典、運算子、集合 方法總結

列表

1.簡介


列表就是可變的陣列,之所以是可變的是因為隨隨便便就可以增加和減少操作。形式如下:list = []。 訪問列表主要是索引和切片,索引從左到右第一個是0,從右到左第一個是-1;切片包含起始的數字,但不包含末尾的數字。

Example:(訪問列表兩種方式,單個元素訪問通常下標,多個元素訪問通常切片。)
    list=[1,2,9,4]
    print list[0],list[1:3]
    print list[::-1] 步長切片
Result: 1 [2,9]
    [4,9,2,1]

2.追加

Example1:(單一追加)
    dota=[1,2,3]
    dota.append(9)
    print dota
Result: [1,2,3,9]

Example2:(列表和列表之間追加)
    dota1=[1,2,3]
    dota2=[4,5,6]
    dota1.extend(dota2)
Result: [1,2,3,4,5,6]

Example3:(+連線,*重複)
    dota1=[1,2,3,4]
    dota2 = [3,4]
    dota3 = dota1+dota2
    dota4 = dota2*2
    print dota3,dota4
Result:[1,2,3,4,3,4] [3,4,3,4]

3.插入指定位置

Example:
    dota=[1,2,3]
    dota.insert(2,9)
    print dota
Result: [1,2,9,3]

4.刪除列表元素

Example1:(刪除末尾元素)
    dota=[1,2,3]
    dota.pop()
    print dota
Result: [1,2]

Example2:(刪除指定位置的元素)
    dota=[1,2,3]
    dota.pop(0)
    print dota
Result: [2,3]

Example3:(del通過下標刪除序列元素)
    dota=[1,2,3]
    del dota[0]
    print dota
Result: [2,3]

    Example4:(remove刪除指定元素,ps用於移除列表中某個值的第一個匹配項)
    dota=[1,3,4,5,6,4]
    dota.remove(4)
Result:dota[1,3,5,6,4]

5.統計列表元素

Example:(統計列表中元素出現的次數)
    dota=[1,2,3,4,4,4,4,4]
    num=dota.count(4)
    print num
Result: 5

6.查詢元素返回其第一次出現的下標

Example:
    dota = [1,1,2,3,2]
    num=dota.index(2)
    print num
Result: 2

7.列表其它技巧

Example1:列表巢狀列表,形成二維陣列
    s = ['python', 'java', ['asp', 'php'], 'scheme']
    s[2][1]
Result: php

Example2:列表中資料型別可以混雜
    s = ['python',1,True]

Example3:其他資料型別轉換成列表(也可以說是列表的其他寫法)
    d=list('abcd')
    print d,type(d)
Result: ['a', 'b', 'c', 'd']

元組

1.簡介

元組是不可變陣列,因此當定義一個tuple時候它的元素就必須被確定下來,不能改變;list和tuple區別主要表現在list可以變能給下標賦值修改,而tuple不能;因此tuple不可變程式碼更安全,如果可能,能用tuple代替list就儘量用tuple,比如你寫了一個API,然後要交給別人來對接,但是你想保證你的程式碼的安全以及不想讓別人動你的程式碼,此時tuple就是相對合適的選擇。

2.查詢元素返回其第一次出現的下標

Example:
    dota = (1,1,2,3,2)
    num=dota.index(2)
    print num
Result: 2

3.統計元組中元素出現的次數

Example:
    dota=(1,2,3,4,4,4,4,4)
    num=dota.count(4)
    print num
Result: 5

4.元組其它技巧

Example1: 其他型別轉換成元組
    a=[1,2,3,4]
    b=tuple(a)
    print b
Result:b=(1,2,3,4)

Example2: 區別點
    a= (4) 型別type(a)是int
    a=(4,) 型別type(a)是tuple

字典

簡介:

格式如下:d = {key1 : value1, key2 : value2 }方法.keys()返回字典所有鍵的列表,.items()返回字典所有值的列表。

常用方法:
    Example1:訪問元素
    a={'1':'dota','2':'dota2'}
    a['1']
    Result:‘dota’

    Example2:
    a={'1':'dota','2':'dota2'}
    a[a.keys()[0]]
    Result:dota

    Example3:
    a.items顯示所有元素
    for k,v in d.iteritems():迭代器生成
    print k ,v
    
    Example4:
    dict(zip(‘abc’,range(2)))字典生成
    {k:v for k,v in zip(“abc”,range(3))}生成字典

運算子

+ 連線操作
* 重複操作
in 如果在指定的序列中找到值返回True
not in 檢查一個值是否出現在一個序列中
is ,not is 判斷指標是否相同(可以連線成比較兩個字元是否相同的物件)
& 交集
| 並集
- 差集
^ 對稱差集(不會同時存在)
and 和
or 或者(可以提供預設值)
/ 表示 浮點數除法,返回浮點結果;// 表示整數除法。
不支援a++ ++a,因為他把+看成單目運算子
is和==區別:
is判斷的是a物件是否就是b物件,是通過id來判斷的。
==判斷的是a物件的值是否和b物件的值相等,是通過value來判斷的。
例如:
In [1]: d={'sf':'ok','sk:':'not ok'}
Out[2]: {'sf': 'ok', 'sk:': 'not ok'}
In [3]: dd =d.copy()
In [4]: d is dd
Out[4]: False
In [5]: d == dd
Out[5]: True

Example:
x=None
y=x or 0
y
0

集合

Example:
>>> li=['a','b','c','a']
>>> se =set(li)
>>> se
set(['a', 'c', 'b'])
>>>list(set(['a', 'c', 'b']))