day12—列表、元组、字典基本语法

一、list类中提供的方法

**********************灰魔法**************************

1. 原来值最后追加 append()

li = [11, 22, 33, 22, 44]
li.append(5)
li.append("alex")
li.append([1234,2323])
print(li)       # [11, 22, 33, 22, 44, 5, 'alex', [1234, 2323]]

2. 清空列表 clear()

li = [11, 22, 33, 22, 44]
li.clear()
print(li)       # []

3. 计算元素出现的次数 count()

li = [1,2,2,3,42,3,2]
v = li.count(2)
print(v)     # 3

4. 扩展原列表,参数:可迭代对象 append(), extend()

li = [11, 22, 33, 22, 44]
li.append([9898,"不得了"])
print(li)     # [11, 22, 33, 22, 44, [9898, '不得了']]
li = [11, 22, 33, 22, 44]
li.extend([9898,"不得了"])
print(li)     # [11, 22, 33, 22, 44, 9898, '不得了']

5. 根据值获取当前值索引位置(左边优先)index()

li = [11, 22, 33, 22, 44]
v= li.index(22)
print(v)     # 1

6. 在指定索引位置插入元素 insert()

li = [11, 22, 33, 22, 44]
li.insert(0,99)
print(li)     # [99, 11, 22, 33, 22, 44]

7. 删除某个值(1.指定索引;2. 默认最后一个),并获取删除的值 pop()

li = [11, 22, 33, 22, 44]
# v = li.pop()
# print(li)     # [11, 22, 33, 22]
v = li.pop(2)
print(li)       #[11, 22, 22, 44]

8. 删除列表中的指定值,左边优先 remove()

li = [11, 22, 33, 22, 44]
li.remove(22)
print(li)     # [11, 33, 22, 44]

9. 将当前列表进行翻转 reverse()

li = [11, 22, 33, 22, 44]
li.reverse()
print(li)     # [44, 22, 33, 22, 11]

10. 列表的排序(默认为 False) sort()

li = [11, 44, 22, 33, 22]
li.sort()
li.sort(reverse=True)
print(li)     # [44, 33, 22, 22, 11]

 

*************************深灰魔法**************************

1. 索引取值
print( li [3] )


2. 切片,切片结果也是列表
print( li [3 : -1])

3. for循环 

for item in li:
print(item)

4. 列表元素修改 

li = [11, 44, 22, 33, 22]
li[1] = 120
print(li)     # [11, 120, 22, 33, 22]
li[1] = [11,22,33,44]
print(li)     # [11, [11, 22, 33, 44], 22, 33, 22]

5. 删除 del()

li = [11, 44, 22, 33, 22]
del li[1]
print(li)     # [11,22,33,22]

6. 切片

li = [11, 44, 22, 33, 22]
li[1:3] = [120,90]
print(li)     # [11, 120, 90, 33, 22]
del li[2:4]
print(li) # [11, 120, 22]

7. in 操作

li = [1, 12, 9, "age", ["石振文", ["19", 10], "庞麦郎"], "alex", True]
v1 = "石振文" in li
print(v1)     # False
v2 = "age" in li
print(v2)     # True

8. 操作

li = [1, 12, 9, "age", ["石振文", ["19", 10], "庞麦郎"], "alex", True]
print(li[4][1][0])     # 19
print(li[4][1])        # ['19', 10]

9. 字符串转换为列表

a = 123
a = str(a)
new_a = list(a)  # 字符串可以转换为列表,数值不可以
print(new_a)     # ['1', '2', '3']

10. 列表转换为字符串

li = [11,22,33,"123","alex"]
s = ""
for i in li:
    s = s + str(i)
print(s)     # 112233123alex

或者用 join 的方法

li = ["123","alex"]
v = "".join(li)
print(v)     # 123alex

11. 字符串修改

v = "alex"
v = v.replace('l','el')
print(v)     # aelex

 

二、元组类中提供的方法

1. 索引
    v = tu[0]
    print(v)

2. 切片
    v = tu[0:2]
    print(v)

3.  可以被 for 循环,可迭代对象

    for item in tu:

    print(item)

4. 字符串转换为元组

s = "asdfas"
li = ["asdf","asdfas"]
tu = ("asdf","as")

v = tuple(s)
print(v)     # ('a', 's', 'd', 'f', 'a', 's')

v = tuple(li)
print(v)     # ('asdf', 'asdfas')

v = list(tu)
print(v)     # ['asdf', 'as']

v = "_".join(tu)
print(v)     # asdf_as

li = ["asdf","asdfasdf"]
li.extend((11,22,33,))
print(li)    # ['asdf', 'asdfasdf', 11, 22, 33]

5. 元组的一级元素不可修改/删除/增加

tu = (111,"alex",(11,22),[(33,44)],True,33,44,)
# 元组,有序。
v = tu[3][0][0]
print(v)     # 33
v=tu[3]
print(v)     # [(33, 44)]
tu[3][0] = 567
print(tu)     # (111, 'alex', (11, 22), [567], True, 33, 44)
tu[3] = '12'    # 报错

 

三、字典类中提供的方法

基本结构

1、字典的 value 可以是任何值——列表、元组、字典、布尔值等

2、布尔值(1,0)、列表、字典不能作为字典的 key,但元组可以

3、索引方式找到字典的元素

4、字典支持 del 删除

info = {
    "k1": 18,
    "k3": [
        11,
        [],
        (),
        {
            'kk1': 'vv1',
            'kk2': 'vv2',
            'kk3': (11,22),
        }
    ],
    2: (11,22,33)
}
del info['k1']
print(info)     # {2: (11, 22, 33), 'k3': [11, [], (), {'kk2': 'vv2', 'kk1': 'vv1', 'kk3': (11, 22)}]}
del info['k3'][3]['kk1']
print(info)     # {2: (11, 22, 33), 'k3': [11, [], (), {'kk2': 'vv2', 'kk3': (11, 22)}]}

5、 for 循环

info = {
    2: True,
    "k3": [
        11,
        [],
        (),
        {
            'kk1': 'vv1',
            'kk2': 'vv2',
            'kk3': (11,22),
        }
    ],
    4: (11,22,33,44)
}
for item in info:
    print(item)             # 2
                            # 4
                            # k3
for item in info.keys():
    print(item)             # 2
                            # 4
                            # k3
for item in info.values():
    print(item)             # 11, [], (), {'kk2': 'vv2', 'kk1': 'vv1', 'kk3': (11, 22)}]
                            # True
                            # (11, 22, 33, 44)
for k,v in info.items():
    print(k,v)              # 2 True
                            # 4 (11, 22, 33, 44)
                            # k3 [11, [], (), {'kk1': 'vv1', 'kk3': (11, 22), 'kk2': 'vv2'}]

6. 根据序列,创建字典,并指定统一的值    fromkeys()

v = dict.fromkeys(["k1",123,"999"],123)
print(v)      # {123: 123, '999': 123, 'k1': 123}
v = dict.fromkeys(["k1",123,"999"])
print(v)      # {123: None, '999': None, 'k1': None}

7. 根据Key获取值,key不存在时,可以指定默认值(None)

dic = {"k1":"v1"}
v = dic['k1']
print(v)       # v1
v = dic.get('k11111')
print(v)       # None 如果用dic则会报错
v = dic.get('k11111','asdf')
print(v)       # asdf  如果不存在,就传递asdf

8. 删除并获取值  pop(), popitem()

dic = {
    "k1": 'v1',
    "k2": 'v2'
}
v = dic.pop('k1',90)
print(dic,v)            # {'k2': 'v2'} v1      如果没有k1,就传递90
k,v = dic.popitem()     # 随机删除
print(dic,k,v)          # {} k2 v2   

9. 设置值  setdefault()

dic = {
    "k1": 'v1',
    "k2": 'v2'
}
v = dic.setdefault('k1','123')      # 已存在,不设置,获取当前key对应的值
print(dic,v)       # {'k1': 'v1', 'k2': 'v2'} v1
v = dic.setdefault('k3','123')      # 不存在,设置,获取当前key对应的值
print(dic,v)       # {'k1': 'v1', 'k2': 'v2', 'k3': '123'} 123

10. 更新(两种写法都可以) update()

dic = {
    "k1": 'v1',
    "k2": 'v2'
}
dic.update({'k1': '111111','k3': 123})
print(dic)       # {'k2': 'v2', 'k1': '111111', 'k3': 123}
dic.update(k1=123,k3=345,k5="asdf")
print(dic)       # {'k5': 'asdf', 'k2': 'v2', 'k1': 123, 'k3': 345}
posted @ 2018-10-06 12:54  *精灵鼠*  阅读(194)  评论(0编辑  收藏  举报