组合数据类型, 分为序列类型, 集合类型和映射类型.
序列类型 序列类型有列表与集合, 列表常用操作如下:
点击显/隐代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ll = [1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ] ll.append(10 ); ll.extend((15 , 20 )); ll.insert(0 , 0 ); print (ll) ll.remove(15 ); ll.pop(0 ); print (ll); ll[0 ] = 20 ; print (ll) print (ll[5 ]); print (ll.index(5 )); print (len (ll)); print (max (ll)); print (ll.count(20 ))print (520 in ll); print (ll + [123 ]); print (ll * 2 )print (ll.copy()) print (ll.reverse())
3. 集合 因为去重, 因此使用场景: 元素去重 因为无序, 不支持下标操作
点击显/隐代码 1 2 3 4 5 6 7 aset = {1 , 2 , 3 , 4 , 1 } aset.add(5 ) aset.update((10 , 20 )) print (aset)
4. 字典 点击显/隐代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 dict = {'name' :'koi_nl' , 'sex' :'👨' }dict ['name' ] = 'Koi_NL' dict ['id' ] = '001' dict .pop('id' )for key in dict : print (key, end = ' ' ) for value in dict .values(): print (value, end = ' ' ) for item in dict .items(): print (item, end = ' ' )
3. 推导式
列表推导式 有句口诀: 空列表尾加法无脑列表推导式就是了
点击显/隐代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 def isPrime (): """判断函数是否为质数""" n = eval (input ("输数" )) for i in range (2 , n // 2 + 1 ): if n % i == 0 : return False return True def isPrime (n ): ll = [] for i in range (2 , n // 2 + 1 ): ll.append(n % i) return 0 not in ll """空列表尾加法, 我嗅到了列表推导式的味道""" def isPrime (n ): return 0 not in [n % i for i in range (2 , n // 2 + 1 )]def genPrime (a, b ): """将 a, b 区间的质数以列表形式显示...当然字符串也行, 我只是为了列表推导式""" ll = [] for n in range (a, b + 1 ): if isPrime(n): ll.append(n) return ll """空列表尾加法, 我嗅到了列表推导式的味道""" def genPrime (a, b ): return [n for n in range (a, b + 1 ) if isPrime(n)]def genPrime (a, b ): return [n for n in range (a, b + 1 ) if 0 not in [n % i for i in range (2 , n // 2 + 1 )]]print (genPrime(1 , 100 ))
集合推导式 还是老样子, 空集合增加法无脑集合推导式就行了
点击显/隐代码 1 2 3 4 5 6 7 8 list = [1 ,2 ,3 ]set = set ()for i in list : set .add(i * 2 ) set = {i * 2 for i in list }print (set )
字典推导式
点击显/隐代码 1 2 3 4 5 6 7 8 9 10 11 list = ['name' ,'age' ]list_f = ['Koi_NL' ,19 ] dict = {list [i]:list_f[i] for i in range (len (list ))}print (dict )dict ['weight' ] = 50 del dict ['name' ]a ={key:value for key,value in dict .items() if value > 25 } print (a)
4. jieba 库 第三方库, 需要安装. 中文分词库
点击显/隐代码 1 2 3 4 5 6 7 from jieba import *setLogLevel(logging.INFO) print (lcut("锦鲤未离在中华人民共和国" )) print (lcut("锦鲤未离在中华人民共和国" , cut_all=True )) print (lcut_for_search("锦鲤未离在中华人民共和国" )) add_word("锦鲤未离" ) print (lcut("锦鲤未离在中华人民共和国" ))
5. 综合案例
重复元素判定. 编写一个函数, 用列表作为参数, 如果一个函数在该列表种出现了超过一次, 返回 True, 反之则反, 但不改变列表的值.
点击显/隐代码 1 2 3 4 5 6 7 def judge (list ): t =set (list ) if len (t) !=len (list ): return True else : return False print (judge(list (input ("输值 (例: 123456): " ))))
文本词频统计. 来吧, 统计下三国演义的人物出场统计吧!
点击显/隐代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 from jieba import *; setLogLevel(logging.INFO) excludes = {'将军' , '却说' , '丞相' } txt = open ("三国演义.txt" , "r" , encoding='utf-8' ).read() words = lcut(txt) counts = {} for word in words: if len (word) == 1 : continue elif word == "孔明曰" or word == "孔明" : rword = "诸葛亮" else : rword = word counts[rword] = counts.get(rword, 0 ) + 1 for word in excludes: del (counts[word]) items = list (counts.items()) items.sort(key=lambda x: x[1 ], reverse=True ) for i in range (15 ): word, count = items[i] print (word, "\t" , count)
生日悖论. 指如果一个房间里有23 或以上人,那么至少有两个人生日相同的 概率大于50%。编写程序,输出在不同随机样本数量下,23 个人中至少两个人生日相同的概率。
点击显/隐代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 from random import *"""只有一行, 我嗅到了匿名函数的味道""" is_same = lambda lst: len (lst) > len (set (lst)) """空列表尾增法, 我嗅到了列表推导式的味道↓""" def generate (): return [randint(1 , 365 ) for i in range (23 )]def count_same (): count = 0 for i in range (10000 ): birthday = generate() if is_same(birthday): count += 1 return count print (f"概率是{(float (count_same) / 10000 ) * 100 } %" )
点击显/隐代码
评论区
欢迎你留下宝贵的意见,昵称输入QQ号会显示QQ头像哦~