python列表元组集合字典_Python基本数据类型--列表、元组、字典、集合

#!/usr/bin/env python#!-*- coding:utf-8 -*-#write by congcong

'''字典是一种 key - value 的数据类型

例如:

info = {

"Jack":[21,'学生','Hgzx','good'],

"Alice":[20,'会计','Hgsf','great'],

"Bob":[23,'老师','Hgsf','happy']

}

特性:

key - value 结构

key必须是可hash,且必须为不可变的数据类型,必须唯一

value 可存放任意多个值、可修改、可以不唯一

字典无序

查找速度快'''info={"Jack":[21,'学生','Hgzx','good'],"Alice":[20,'会计','Hgsf','great'],"Bob":[23,'老师','Hgsf','happy']

}#字典的查找

print(info['Jack']) #输出:[21, '学生', 'Hgzx', 'good']#字典的修改

info['Jack'][0] = 22

print(info['Jack']) #输出:[22, '学生', 'Hgzx', 'good']#字典的添加

info['Rose'] = [19,'学生','Hgsf','upset']print(info)#输出:{'Jack': [22, '学生', 'Hgzx', 'good'], 'Alice': [20, '会计', 'Hgsf', 'great'], 'Bob': [23, '老师', 'Hgsf', 'happy'], 'Rose': [19, '学生', 'Hgsf', 'upset']}#字典的删除

info_del1 = info.pop('Bob') #默认删除末尾的元素,可删除指定的字符串,并返回所删除的信息,输出:[23, '老师', 'Hgsf', 'happy']

info_del2 = info.popitem() #随机删除字符串,并返回所删除的字符串,输出:('Rose', [19, '学生', 'Hgsf', 'upset'])

del info['Jack'] #删除指定的字符串

print(info_del1,info_del2,info) #删除之后的字典:{ 'Alice': [20, '会计', 'Hgsf', 'great']}

#字典的复制

n1 = {'Alice': [20, '会计', 'Hgsf', 'great']}print(type(n1)) #输出数据类型,

n2 =n1.copy()print(n2) #输出:{'Alice': [20, '会计', 'Hgsf', 'great']}

n1['Alice'][0] = 21 #仍然一改全改

print(n1,n2) #{'Alice': [21, '会计', 'Hgsf', 'great']} {'Alice': [21, '会计', 'Hgsf', 'great']}

#判断要查找的字符串是否在字典内

print('Alice' in info,'Frank'in info) #in方法,输出;True False

#获取指定的字符串内容

print(info.get('Alice')) #get方法,输出;[20, '会计', 'Hgsf', 'great']

print(info.get('Frank')) #没有找到时,返回 None ,输出:None

print(info['Alice']) #输出;[20, '会计', 'Hgsf', 'great'],没找到时会报错

#字典的清空

print(info.clear()) #输出:None

#仅输出字典的 keys 或 values

info = {'lufei':[666,'hope'],'qiqi':[999,'hard'],'keke':[333,'will']}

info_keys= info.keys() #只输出字典的keys

info_values = info.values() #只输出字典的values

print(info_keys,info_values) #输出:dict_keys(['lufei', 'qiqi', 'keke']) dict_values([[666, 'hope'], [999, 'hard'], [333, 'will']])

#将字典转成列表

dit_items = info.items() #将字典转成列表

print(dit_items) #输出:dict_items([('lufei', [666, 'hope']), ('qiqi', [999, 'hard']), ('keke', [333, 'will'])])

#创建新字典,以序列seq中元素做字典的键,value为字典所有键对应的初始值。

names = ['cc','linux','python']

info2= dict.fromkeys(names)#fromkeys() 函数用于创建一个新字典,以序列seq中元素做字典的键,value为字典所有键对应的初始值。

print(info2) #输出;{'cc': None, 'linux': None, 'python': None}

info3 = dict.fromkeys(names,666)print(info3) #输出:{'cc': 666, 'linux': 666, 'python': 666}

#字典的更新

n1 = {'first':1,'second':2,'three':3}

n2= {'four':4,'five':5}

n1.update(n2)#将字典 n2 更新到字典 n1中

print(n1) #输出;{'first': 1, 'second': 2, 'three': 3, 'four': 4, 'five': 5}

#setdefault() 方法和get()方法类似, 键存在时,返回值;如果键已经不存在于字典中,将会添加键并将值设为默认值。

print(n1.setdefault('four')) #输出:4,键'four'存在时返回值

n1.setdefault('six')print(n1) #输出:{'first': 1, 'second': 2, 'three': 3, 'four': 4, 'five': 5, 'six': None}

#字典的循环

for k inn1:print(k,n1[k])'''输出:

first 1

second 2

three 3

four 4

five 5

six None'''

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值