python3-基础3

列表 list[ ]  作用   --  存储多个值,多个元素

索引   list[num]

切片  list[:3]

追加  list.append('lalaal')

删除  list.pop()       删除最后一位   list.pop(num) 指定下标 

         list.remove('lalala')  删除指定值

长度 len(list)  统计列表里面元素个数

包含  in        x  in  list   判断x是否在列表中

 

 

##其他操作

清空列表   list.clear()

复制列表  list.copy()

统计元素重复次数  list.count('lala')

添加多个值  list.extend(['la1','la2','la3'])

查下标  list.index('la')

列表反转  list.reverse()

列表排序  list.sort()

 

队列: 先进先出

入队 .append     fifo=[]

   fifo.append('一')

   fifo.append('二')

   fifo.append('三')

出队.pop(0)

   fifo1 = fifo.pop(0)

   fifo2 = fifo.pop(0)

   fifo3 = fifo.pop(0)

 

入队.insert(指定位置插入):

fifo.insert(0,'一')

fifo.insert(0,'二')

fifo.insert(0,'三')

出队 .pop()

fifo.pop()

 

堆栈:先进后出

 

 列表元素赋值

list=['la', 18, [1988, 3, 31]]

name, age, birth = list

print(name)

print(age)

print(birth)

 

 

===============

元组 tuple()  --  就是一个不可变的列表,主要是用来读操作,是可以当做字典的key的

####小练习:买商品加入到购物车,商品信息为字典,购物车信息为列表,选中的商品信息为元组(商品名称、单价、数量)。

循环字典:

msg_dic={
'apple':10,
'benz':100000,
'mac':3000,
'lenovo':30000,
'pen':10,
}

for i in msg_dic:
print(i,msg_dic[i]

 1 ##商品字典 mag_dic{}
 2 msg_dic={
 3 'apple':10,
 4 'tesla':100000,
 5 'mac':3000,
 6 'lenovo':30000,
 7 'chicken':10,
 8 }
 9 #购物车列表 goods_l[]
10 goods_l = []
11 # for i in msg_dic:
12 #     print(i,msg_dic[i])
13 
14 while True:
15     for key in msg_dic:   #循环显示商品信息
16         print('\033[46mName:{name}  Price:{price}\033[0m]'.format(price=msg_dic[key], name=key))
17     choice = input('your goods name >>:  ').strip()  #选择商品
18 #     price=msg_dic[choice]
19     if choice == 0 or choice not in msg_dic: continue
20     count = input('your count >> : ').strip()  #购买商品个数
21     if count.isdigit():
22         count = int(count)
23         goods_l.append((choice,msg_dic[choice], count))  #添加已选商品信息到购物车
24 
25     print(goods_l)

 

字典 dict{} 存取多个值,key:value  成对出现,一一对应,取值速度快

key 是不可变类型(数字、字符串、元组), value是任意类型

创建  dict1 = {'name':'lalala', 'age' : 18 , 'sex' : 'male' ,}

          dic = dict(a=1, b=2 , c=3)

添加  dict1['hight' ] = 180

删除  dict1.pop('key')

不会抛出异常    dict1.pop('keyx' , '没有这个值啊')    会返回‘’没有这个值‘

 

取值  dict1.get()  用法 同》.pop()

dict1.get('key' , 'not key')     若没有这个key,不会报错,返回 not key

 

dict1.popitem 返回key,value   以元组的形式

dict1.keys()  返回所有的key

dict1.values()  返回所有的值

dict.items()   取出所有的键值组合,以列表中的元组形式出现

for key,value   in  dict.items();

print(key,value)

 

dict1.update()   更新老的字典

 手动增加字典内容

 1 d1 = {}
 2 print(d1)      #{}
 3 d1['name'] = 'lalal'
 4 d1['age'] = 28
 5 d1['sex'] = 'male'
 6 d1['hobby'] = []     # 存在多个爱好
 7 d1['hobby'].append('basketball')
 8 d1['hobby'].append('swimming')
 9 print(d1) 
10   #{'name': 'lalal', 'age': 28, 'sex': 'male', 'hobby': ['basketball', 'swimming']}
11 
12 #或者用setdefault
13 d2 = d1.setdefault('hahaha' , [])
14 print(d1)   
15   #{'name': 'lalal', 'age': 28, 'sex': 'male', 'hobby': ['basketball', 'swimming'], 'hahaha': []}
16 print(d2)
17   #[]
18 
19 d3 = d1.setdefault('hahaha' , []).append('play1')
20 d3 = d1.setdefault('hahaha' , []).append('play2')
21 print(d1)
22   #{'name': 'lalal', 'age': 28, 'sex': 'male', 'hobby': ['basketball', 'swimming'], 'hahaha': ['play1', 'play2']}
23 print(d2)
24   #['play1', 'play2']
25 print(d3)
26   #None
 

 例子1

 1 #下列集合中的数字[23,28,39,30,68,79,77,89,105,63],将所有不小于66的值,保存在第一个key键中,所有小于66的值保存在第二个key键中。
 2 nums = [23,28,39,30,68,79,77,89,105,63]
 3 dic = {'k1' : [] , 'k2' : []}
 4 print(dic)
 5 for i in nums:
 6     print(i)
 7     if i >= 66 :
 8         dic['k1'].append(i)
 9     else:
10         dic['k2'].append(i)
11 print(dic)        
 

例子2

 1 #统计s =  "hello lalala you are a good man ha ha ha ha good"  中,各个单词的个数
 2 s = "hello lalala you are a good man ha ha ha ha good"
 3 words = s.split()  #以空格分隔
 4 print(words)
 5 dic = {}
 6 for word in words :
 7     print(word)
 8     if word not in dic:
 9         dic[word] =  1     #如果word中的元素不在dic中,那么相当于 {‘hello’ : 1}
10     else:
11         dic[word] += 1     #如果word中的元素在dic中,那么相当于 {‘hello’ : 2}
12 print(dic)

 

集合set{}   作用主要是:去重,关系运算 

     集合内的元素必须都是不可变类型,即可hash类型

     集合内的元素,不可重复,具有唯一性

     集合是无序的

s1 = set('hello')

print(s1,type(s1))           #   {'l', 'h', 'e', 'o'} <class 'set'>

 

删除元素

s1.pop()   随机删除一个值

s1.remove('aaa')   删除指定值,如果该值不存在,会报错

 s1.discard(''aaaa)   删除指定值,如果该值不存在,那么不会报错

 

关系运算

s1 = {1,3,8,16,18,29}

s2 = {16, 39, 18, 8, 7, 5}

 

交集

s3 = s1 & s2

或    s3 = s1.intersection(s2)

 

并集

s4 = s1 | s2

或    s4 = s1.union(s2)

 

差集

s5 = s1 - s2

或   s5 = s1.difference(s2)

 

对称差集(排除并集后的值)

s6 = s1 ^ s2

或   s6 = s1.symmetric_difference(s2)

 父集、子集

 

练习

 1 # 有如下列表l,列表元素为不可hash类型,去重,得到新列表,且新列表一定要保持列表原来的顺序
 2 #注:字典是可变类型,不能作为集合的元素
 3 l=[
 4     {'name':'egon','age':18,'sex':'male'},
 5     {'name':'alex','age':73,'sex':'male'},
 6     {'name':'egon','age':20,'sex':'female'},
 7     {'name':'egon','age':18,'sex':'male'},
 8     {'name':'egon','age':18,'sex':'male'},
 9 ]
10 # set1 = set(l)   不能直接转为集合,会报错
11 # print(set1)
12  #  解决方法一  不用集合
13 l1=[]
14 for i in l:
15     if i not in l1:
16         l1.append(i)
17 print(l1)
18  #  解决方法二  借助集合去重
19 l1=[]  #创建空列表
20 s=set()  #创建空集合
21 for i in l:    #循环列表中的元素
22 #     print(i)   #元素均为字典,需要吧字典转化为可以放在集合里面的类型,比如元组,使用 dict.items() 
23 #     tuple1 = i.items()
24 #     print(tuple1)
25 #     if i not in s:   #判断列表元素是不是在集合中
26 #         s.add(i)
27 #         l1.append(i)
28 
29 # print(l1) 
30     val = (i['name'],i['age'],i['sex'])   #应为字典的key都一样,所以将字典里的值组成一个元组
31     print(val)
32     if val not in s:  #以元组为集合的元素,进行循环查找
33         s.add(val)     #若不在元组中就添加
34         l1.append(i)   #同时将对应的字典作为元素,添加到新建的列表中
35 print(l1)        
36     
37   

 

转载于:https://www.cnblogs.com/Albert-w/p/10711004.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值