一、字典
1.字典定义
#定义字典 d = {} print(d) print(type(d)) #key一般情况下使用字符串类型的数据充当,理论上不可变的类型都可以作为key #只要是可hash的对象都可以作为key d = {1:"one",2:"two"} print(d) d = {"one":"星期一","tow":"星期二"} print(d)
2.字典的访问
d = {"one":"星期一","two":"星期二","three":"星期三","four":"星期4"} print(d) print(d['one']) print(d['two']) print(d['three']) #赋值 d['four'] = '星期四' print(d) print("___________________________________________________________________________") print(d['one']) print(d.get('one')) #下标方式在访问数据时,如果key不存在,就会报错 #get方法方式 访问数据时,如果key不存在,返回none print(d['two']) print(d.get('two')) print(d.get("asd"))
3.字典的遍历
d = {"one":"星期一","two":"星期二","three":"星期三","four":"星期4"} for k in d: print(k) print("____________________________________________________________________") #方法1 for k in d: print(f'for in : {k} ---> {d[k]}') print("____________________________________________________________________") #方法2 print(d.keys()) for k in d.keys(): print(f'keys : {k} ---> {d[k]}') #方法三 print("____________________________________________________________________") print(d.values()) for v in d.values(): print(f'values: {v} ') #方法四 item里面每一个元素都是一个元组 print("____________________________________________________________________") print(d.items()) for i in d.items(): print(f' items : {i[0]} ---> {i[1]}') print("____________________________________________________________________") #解包 for k,v in d.items(): print(f' items : {k} ---> {v}') a,b,c,d,e = 1,2,3,4,5 print(a) print(e) #解包编写赋值方式 item = (1,2,3,4,5) a,b,c,d,e = item print(a) print(e)
4.字典删除
d = {"one":"星期一","two":"星期二","three":"星期三","four":"星期4"} #出栈 d.popitem() print(d) d.popitem() print(d) #pop后面接key可以指定删除的键值对 print("_______________________________________________________________") d = {"one":"星期一","two":"星期二","three":"星期三","four":"星期4"} d.pop('one') print(d)
5.字典练习
def creat_card(): card = {} card['name'] = input("请输入姓名: ") card['age'] = input("请输入年龄: ") card['address'] = input("请输入地址: ") return card def show_card(card): for k in card: print(f'{k} : {card.get(k)}') c = creat_card() show_card(c)
6.有序字典
构建有序字典
from collections import OrderedDict my_dict = OrderedDict() my_dict['one'] = 1 my_dict['two'] = 2 my_dict['three'] = 3 my_dict['four'] = 4 my_dict['five'] = 5 print(my_dict)
7.集合
主要的作用是去重
#集合定义 s = {} print(type(s)) s = {1,2,2,3,234,4,5,6666,5,5,5} print(s)
注意集合是不支持下标的
遍历用for in
#集合定义 s = {} print(type(s)) s = {1,2,2,3,234,4,5,6666,5,5,5} print(s) for i in s: print(i)
8.类型转化问题
s = "hello" #字符串转化成列表 l = list(s) print(l) print(type(l)) print("_______________________________________________________________") #集合是无序的 l = set(l) print(l) print(type(l)) #列表转化集合 print("_______________________________________________________________") l = list(s) l1 = set(l) print(type(l1)) print(l1) #列表和集合转化成字符串 print("_______________________________________________________________") l = list(s) s1 = str(l) print(type(s1)) print(s1) l = set(s) s1 = str(l) print(type(s1)) print(s1)
但是这里转化成str类型并不是拼接
9.公共方法
s = "hello" + "itcast" print(s) s = [1,2,3,4] + [1,23,4,5] print(s) s = ('a','b') + ('c','b') print(s) s = "h1" * 4 print(s)
python内置函数
list1 = [1,23,23,4,111,23,111,4,5] print(max(list1)) print(min(list1))
二、列表推导式
1.基本操作
创建一个具有一百个数字的列表
#for in c_1 = [] for i in range(100): c_1.append(i) print(len(c_1)) print(c_1) print("_________________________________________________________________________") #推导式 c_1 = [i for i in range(100)] #注意,表达的变量使用要和循环中的变量一致 print(len(c_1)) print(c_1)
2.在循环中使用if
a = [x for x in range(3,10) if x%2==0] print(a) print("______________________________________________________________") a = [x for x in range(3,10) if x%2!=0] print(a) print("______________________________________________________________") a = [x for x in range(3,10)] print(a)
3.多个for
#两个for a = [(x,y) for x in range(1,3) for y in range(3)] print(a) print("_____________________________________________________________________________") #多个for a = [(x,y,z) for x in range(1,3) for y in range(3) for z in range(5,10) if z%2!=0] print(a)