1. 程式人生 > >python數據類型:tuple、dict及其方法

python數據類型:tuple、dict及其方法

asdf 修改 進行 key存在 join int 找到 efault spa

1.tu(對象)對應的tuple(類)

元組是對列表的二次加工,元素不可被修改,不能被增加或者刪除

tu = (111,"alex",(11,22),[(33,44)],True,33,44,)

一般寫元組的時候,推薦在最後加入逗號(,),為了做區分

2.tuple類中提供的方法

(1)索引取值

tu = (111,"alex",(11,22),[(33,44)],True,33,44,)
v = tu[0]
print(v)

輸出結果為:111

(2)切片取值

tu = (111,"alex",(11,22),[(33,44)],True,33,44,)
v = tu[0:2]
print
(v)

輸出結果為:(111, ‘alex‘)

(3)可以被for循環,可叠代對象

tu = (111,"alex",(11,22),[(33,44)],True,33,44,)
for item in tu:
    print(item)

輸出結果為:

111
alex
(11, 22)
[(33, 44)]
True
33
44

(4)轉換

a.字符串轉換為元組

s = "asdfasdf0"
v = tuple(s)
print(v)

輸出結果為:(‘a‘, ‘s‘, ‘d‘, ‘f‘, ‘a‘, ‘s‘, ‘d‘, ‘f‘, ‘0‘)

b.列表轉換為元組

li = ["asdf","asdfasdf
"] v = tuple(li) print(v)

輸出結果為:(‘asdf‘, ‘asdfasdf‘)

c.元組轉換為列表

tu = ("asdf","asdf")
v = list(tu)
print(v)

輸出結果為:[‘asdf‘, ‘asdf‘]

d.元組轉換為字符串

tu = ("asdf","asdf")
v = "_".join(tu)
print(v)

輸出結果為:asdf_asdf

(5)元組的一級元素不可修改/刪除/增加,元祖裏面嵌套其他的數據類型可以被修改/刪除/增加

tu = (111,"alex",(11,22),[(33,44)],True,33,44,)
tu[
3][0] = 567 print(tu)

輸出結果為:(111, ‘alex‘, (11, 22), [567], True, 33, 44)

(6)獲取指定元素在元組中出現的次數

tu = (11,22,33,44)
v=tu.count(22)
print(v)

輸出結果為:1

(7)獲取指定元素在元組中的位置

tu = (11,22,33,44)
v=tu.index(22)
print(v)

輸出結果為:1

3.字典結構及特點

dic = {
"k1": ‘v1‘, #鍵值對
"k2": ‘v2‘
}

(1)字典的value可以是任何值

(2)列表、字典不能作為字典的key;布爾值作為key時會和1即true,0即false重復

info ={
    1: asdf,
    "k1": asdf,
    True: "123",
    (11,22): 123,
}
print(info)

輸出結果為:{1: ‘123‘, (11, 22): 123, ‘k1‘: ‘asdf‘} 1: ‘asdf‘就沒有顯示,因為True和1這個key重復了

(3)字典是無序的(無法通過切片的形式進行索引)

4.dict類中提供的方法

(1)索引方式找到指定元素

info = {
    "k1": 18,
    2: True,
    "k3": [
        11,
        [],
        (),
        22,
        33,
        {
            kk1: vv1,
            kk2: vv2,
            kk3: (11,22),
        }
    ],
    "k4": (11,22,33,44)
}
v = info[‘k3‘][5][‘kk3‘][0]
print(v)

輸出結果為:11

(2)字典支持 del 刪除(註意觀察結果無序)

info = {
    "k1": 18,
    2: True,
    "k3": [
        11,
        [],
        (),
        22,
        33,
        {
            kk1: vv1,
            kk2: vv2,
            kk3: (11,22),
        }
    ],
    "k4": (11,22,33,44)
}
del info[k3][5][kk1]
print(info)

輸出結果為:{‘k3‘: [11, [], (), 22, 33, {‘kk3‘: (11, 22), ‘kk2‘: ‘vv2‘}], 2: True, ‘k1‘: 18, ‘k4‘: (11, 22, 33, 44)}

(3)for循環(但是不能進行while循環,因為無序)

a.輸出key

info = {
    "k1": 18,
    2: True,
    "k3": [
        11,
        [],
        (),
        22,
        33,
        {
            kk1: vv1,
            kk2: vv2,
            kk3: (11,22),
        }
    ],
    "k4": (11,22,33,44)
}
for item in info:
    print(item)

輸出結果為:

k1
2
k4
k3

和命令for item in info.keys():輸出結果相同

b.輸出value

info = {
    "k1": 18,
    2: True,
    "k3": [
        11,
        [],
        (),
        22,
        33,
        {
            kk1: vv1,
            kk2: vv2,
            kk3: (11,22),
        }
    ],
    "k4": (11,22,33,44)
}
for item in info.values():
    print(item)

輸出結果為:

[11, [], (), 22, 33, {‘kk3‘: (11, 22), ‘kk2‘: ‘vv2‘, ‘kk1‘: ‘vv1‘}]
True
(11, 22, 33, 44)
18

c.同時輸出key和value

info = {
    "k1": 18,
    2: True,
    "k3": [
        11,
        [],
        (),
        22,
        33,
        {
            kk1: vv1,
            kk2: vv2,
            kk3: (11,22),
        }
    ],
    "k4": (11,22,33,44)
}
for k,v in info.items():
    print(k,v)

輸出結果為:

2 True
k1 18
k4 (11, 22, 33, 44)
k3 [11, [], (), 22, 33, {‘kk2‘: ‘vv2‘, ‘kk1‘: ‘vv1‘, ‘kk3‘: (11, 22)}]

和下面命令輸出結果相同

for item in info.keys():
    print(item,info[item])

(4)根據序列,創建字典,並指定統一的value

v = dict.fromkeys(["k1",123,"999"],123)
print(v)

輸出結果為:{‘999‘: 123, ‘k1‘: 123, 123: 123}

(5)刪除並獲取到刪除的值

dic = {
    "k1": v1,
    "k2": v2
}
v = dic.pop(k1,90)
print(dic,v)

輸出結果為:{‘k2‘: ‘v2‘} v1

dic = {
    "k1": v1,
    "k2": v2
}
k,v = dic.popitem()
print(dic,k,v)

輸出結果為:{‘k1‘: ‘v1‘} k2 v2

pop是根據指定key刪除,popitem是隨機刪除

(6)設置值。若設置的key存在,不設置,並獲取當前key對應的值;若設置的key存在,設置,並獲取當前設置的key對應的值

dic = {
    "k1": v1,
    "k2": v2
}
v = dic.setdefault(k1111,123)
print(dic,v)

輸出結果為:{‘k2‘: ‘v2‘, ‘k1‘: ‘v1‘, ‘k1111‘: ‘123‘} 123

dic = {
    "k1": v1,
    "k2": v2
}
v = dic.setdefault(k1,123)
print(dic,v)

輸出結果為:{‘k2‘: ‘v2‘, ‘k1‘: ‘v1‘} v1

》》》以下為比較常用的常用的方法

(7)根據key獲取值,key不存在時,可以指定默認值(None)

dic = {
    "k1": ‘v1‘,
    "k2": ‘v2‘
}
v = dic.get(‘k1‘,111111)
print(v)

輸出結果為:v1

(8)更新

dic = {
    "k1": v1,
    "k2": v2
}
dic.update({k1: 111111,k3: 123})
print(dic)

輸出結果為:{‘k3‘: 123, ‘k2‘: ‘v2‘, ‘k1‘: ‘111111‘}

和命令dic.update(k3=123,k1=‘111111‘)輸出結果相同

python數據類型:tuple、dict及其方法