D11 列表 list 元祖 字典dict

取值

name = "alexdfg"
print(name[3:5])

取出 ex

name = "alexdfg"
print(name[3])

取出e

列表 list

list 类型 列表

li = [1,12 ,19 ,"age" ,["alex","lalala"],188,True]

 

可以用  while 和 for 循环

但是循环的元素是每一个 段

li = [1, 3, "alex", "bbb", 332]
s = " "
for i in li :
    s = s + str(i)
print(s)

打出    13alexbbb332

虽然说 把一个list 变成一个 str    list 里面可以用这种方法   但没看懂

ok 列表的魔法

li = [11, 22, 33, 22, 44]
# 参数
# 1.append 原来值最后追加
# 对象.方法(..) # li对象调用append方法
# li.append(5)
# li.append("alex")
# li.append([1234,2323])
# print(li)

# 2 clear() 清空列表
# li.clear()
# print(li)

# 3 copy() 拷贝,浅拷贝
# v = li.copy()
# print(v)
# 4. 计算元素出现的次数
# v = li.count(22)
# print(v)

# 5. .extend 扩展原列表 把新的列表加入旧的列表里,参数:可迭代对象
# li = [11, 22, 33, 22, 44]
# li.append([9898,"不得了"])
# [11, 22, 33, 22, 44, [9898, '不得了']]

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

# 6.index 根据值获取当前值索引位置(左边优先)
# li = [11, 22, 33, 22, 44]
# v= li.index(22)
# print(v)

# 7.insert 在指定索引位置插入元素
# li = [11, 22, 33, 22, 44]
# li.insert(0,99)
# print(li)

# 8、pop 删除某个值(1.指定索引;2. 默认最后一个),并获取删除的值
# li = [11, 22, 33, 22, 44]
# v = li.pop()
# print(li)
# print(v)

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


# 9.remove 删除列表中的指定值,左边优先
# li = [11, 22, 33, 22, 44]
# li.remove(22)
# print(li)
# PS: 对于删除列表的功能有 1 pop 2 remove 3 del li[0] del li[7:9] 4 clear

# 10 reverse 将当前列表进行翻转
# li = [11, 22, 33, 22, 44]
# li.reverse()
# print(li)

# 11 列表的排序
# li = [11,44, 22, 33, 22]
# li.sort() 从小到大排列


# li.sort(reverse=True) 从大到小排列


# print(li)
### 欠
# cmp
# key
# sorted

元祖 

# 6.元组的一级元素不可修改/删除/增加
# tu = (111,"alex",(11,22),[(33,44)],True,33,44,)
# # 元组,有序。
# # v = tu[3][0][0]
# # print(v)
# # v=tu[3]
# # print(v)
# tu[3][0] = 567
# print(tu)

字典 dict

# 字典
# dict
# dict
# dic = {
# "k1": 'v1',
# "k2": 'v2'
# }
# 1 根据序列,创建字典,并指定统一的值
# v = dict.fromkeys(["k1",123,"999"],123)
# print(v)

# 2 根据Key获取值,key不存在时,可以指定默认值(None)
# v = dic['k11111']
# print(v)
# v = dic.get('k1',111111)


# print(v)

# 3 删除并获取值
# dic = {
# "k1": 'v1',
# "k2": 'v2'
# }
# v = dic.pop('k1',90)
# print(dic,v)
# k,v = dic.popitem()
# print(dic,k,v)

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

# 5 更新
# dic = {
# "k1": 'v1',
# "k2": 'v2'
# }
# dic.update({'k1': '111111','k3': 123})
# print(dic)
# dic.update(k1=123,k3=345,k5="asdf")
# print(dic)

# 6 keys() 7 values() 8 items() get update
##########

 

# 1、基本机构
# info = {
# "k1": "v1", # 键值对
# "k2": "v2"
# }
#### 2 字典的value可以是任何值
# info = {
# "k1": 18,
# "k2": True,
# "k3": [
# 11,
# [],
# (),
# 22,
# 33,
# {
# 'kk1': 'vv1',
# 'kk2': 'vv2',
# 'kk3': (11,22),
# }
# ],
# "k4": (11,22,33,44)
# }
# print(info)

#### 3 布尔值(1,0)True为1 False为0 、列表、字典不能作为字典的key
# info ={
# 1: 'asdf',
# "k1": 'asdf',
# True: "123",
# # [11,22]: 123
# (11,22): 123,
# # {'k1':'v1'}: 123
#
# }
# print(info)

# 4 字典无序

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

# 5、索引方式找到指定元素
# info = {
# "k1": 18,
# 2: True,
# "k3": [
# 11,
# [],
# (),
# 22,
# 33,
# {
# 'kk1': 'vv1',
# 'kk2': 'vv2',
# 'kk3': (11,22),
# }
# ],
# "k4": (11,22,33,44)
# }
# # v = info['k1']
# # print(v)
# # v = info[2]
# # print(v)
# v = info['k3'][5]['kk3'][0]
# print(v)

# 6 字典支持 del 删除
# info = {
# "k1": 18,
# 2: True,
# "k3": [
# 11,
# [],
# (),
# 22,
# 33,
# {
# 'kk1': 'vv1',
# 'kk2': 'vv2',
# 'kk3': (11,22),
# }
# ],
# "k4": (11,22,33,44)
# }
# del info['k1']
#
# del info['k3'][5]['kk1']
# print(info)

# 7 for循环
# dict
# 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)
#
# for item in info.keys():
# print(item)

# for item in info.values():
# print(item)

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

# for k,v in info.items():
# print(k,v)

# True 1 False 0
# info ={
# "k1": 'asdf',
# True: "123",
# # [11,22]: 123
# (11,22): 123,
# # {'k1':' v1'}: 123
#
# }
# print(info)

####################### 整理 #################

# 一、数字
# int(..)
# 二、字符串
# replace/find/join/strip/startswith/split/upper/lower/format
# tempalte = "i am {name}, age : {age}"
# # v = tempalte.format(name='alex',age=19)
# v = tempalte.format(**{"name": 'alex','age': 19})
# print(v)
# 三、列表
# append、extend、insert
# 索引、切片、循环
# 四、元组
# 忽略
# 索引、切片、循环 以及元素不能被修改
# 五、字典
# get/update/keys/values/items
# for,索引

# dic = {
# "k1": 'v1'
# }

# v = "k1" in dic
# print(v)

# v = "v1" in dic.values()
# print(v)
# 六、布尔值
# 0 1
# bool(...)
# None "" () [] {} 0 ==> False

#判断方法
bool({})
bool([])
bool("")

都会出现 False

posted @ 2018-07-11 15:01  Mr喃先森  阅读(168)  评论(0编辑  收藏  举报