01 元组、字典

1. 元组

1.1 元组数据可以查询,不可修改,可用切片法
1.2 元组写在小括号里,元素间用逗号隔开,元组中只有一个元素时,后面要加 ‘,’
1.3 元组可以在映射(和集合的成员)中当作键使用,列表不可以
1.4 元组作为很多内建函数和方法的返回值存在

top = ()  #空元组
top = (12,)   #一个元素的元组

2. 字典

2.1 创建方式

(1)dict1=dict([('name','apple'),]) 注意逗号
(2)dict1={'name':'apple','age':22,'height',176} 常用

2.2 特点

是Python中唯一的映射类型,采用键值对(key-value)的形式存储数据。Python对key进行哈希函数运算,根据计算结果决定value的存储地址。

字典是除列表外Python中最灵活的内置数据结构类型。

列表有序,字典无序;字典中元素用 key 存取,列表是偏移(位置)存取。
字典是无序存储,key必须是可哈希的(不可变类型)

不可变类型:整形、字符串、元组
可变类型:列表、字典

2.3 增
dic={}

dic['name']='Apple'
dic['age']=12
print(dic)  #{'name': 'Apple', 'age': 12}

#setdefault 键存在,不改动,返回字典中相应的key对应的value
a=dic.setdefault('name','Odd')

#键不存在,在字典中增加键值对,返回value
b=dic.setdefault('hobby','motor')

print(a)    #Apple
print(b)    #motor
print(dic)  #{'name': 'Apple', 'age': 12, 'hobby': 'motor'}
2.4 查
dic={'name': 'Apple', 'age': 12, 'hobby': 'motor'}

print(dic['name'])  # Apple
#print(dic['cheese'])    # KeyError: 'cheese'

print(dic.get('age'))   # 12
print(dic.get('ages'))   # None

print(dic.items())  # dict_items([('name', 'Apple'), ('age', 12), ('hobby', 'motor')])  
print(dic.keys())   # dict_keys(['name', 'age', 'hobby'])
print(dic.values()) # dict_values(['Apple', 12, 'motor'])

print(list(dic.values()))   # ['Apple', 12, 'motor']    转成列表

print(type(dic.values()))   # <class 'dict_values'> 变量类型
2.5 改
dic={'name': 'Apple', 'age': 12, 'hobby': 'motor'}

dic['name']='Odd'
dic1={'sex':'male','birth':'2006'}
dic.update(dic1)
print(dic)  # {'name': 'Odd', 'age': 12, 'hobby': 'motor', 'sex': 'male', 'birth': '2006'}

#用 key改对应 value
2.6 删
dic={'name': 'Apple', 'age': 12, 'hobby': 'motor'}

#dic.clear()    # 清空
#print(dic)  # {}

del dic['name'] # 删除指定键值对
print(dic)  # {'age': 12, 'hobby': 'motor'}

dic={'name': 'Apple', 'age': 12, 'hobby': 'motor'}

a=dic.popitem() # 随机删,并以元组返回值
print(a,dic)    # ('hobby', 'motor') {'name': 'Apple', 'age': 12}

dic={'name': 'Apple', 'age': 12, 'hobby': 'motor'}

print(dic.pop('age'))   # 定向删除,返回value
print(dic)  # {'name': 'Apple', 'hobby': 'motor'}
2.7 other operation

(1)dict.fromkeys

dic=dict.fromkeys(['no1','no2','no3'],'test')
print(dic)  # {'no1': 'test', 'no2': 'test', 'no3': 'test'} 适用于初始化

dic['no1']='abc'
print(dic)  # {'no1': 'abc', 'no2': 'test', 'no3': 'test'}  改值还是用key

dic=dict.fromkeys(['no1','no2','no3'],['test1','test2'])
print(dic)  # {'no1': ['test1', 'test2'], 'no2': ['test1', 'test2'], 'no3': ['test1', 'test2']} 后面一直是value

dic['no1'][0]='test0'   
print(dic)  # {'no1': ['test0', 'test2'], 'no2': ['test0', 'test2'], 'no3': ['test0', 'test2']} 改一个就都改了

(2)d.copy() 对字典 d 进行浅复制,返回一个和 d 有相同键值对的新字典
(3)字典嵌套
(4)sorted(dict) :对字典内键值对进行排序,无特殊要求,按 key 排序

    dic={'1':'22','4':'55','3':'66'}
    print(sorted(dic))  # ['1', '3', '4']
    print(sorted(dic.items()))  # [('1', '22'), ('3', '66'), ('4', '55')]
    print(sorted(dic.keys()))   # ['1', '3', '4']
    print(sorted(dic.values())) # ['22', '55', '66']

(5)字典的遍历

dic= {'name': 'Odd', 'age': 12, 'hobby': 'motor', 'sex': 'male', 'birth': '2006'}

for i in dic:
    print(i,dic[i])
    #name Odd
    # age 12
    # hobby motor
    # sex male
    # birth 2006

for items in dic.items():
    print(items)
    # ('name', 'Odd')
    # ('age', 12)
    # ('hobby', 'motor')
    # ('sex', 'male')
    # ('birth', '2006')

for k,v in dic.items():
    print(k,v)
    # name Odd
    # age 12
    # hobby motor
    # sex male
    # birth 2006
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值