![](https://img-blog.csdnimg.cn/20201014180756926.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
Python高级编程
CEO赵林1007
未来无限可能
展开
-
Python高级编程-如何快速找到多个字典中的公工键(key)
1.第一种方案(一般的) >>> from random import randint ,sample >>> sample ('abcdefgh',4) ['g', 'b', 'c', 'd'] >>> sample ('abcdefgh',randint (4,7)) ['h', 'e', 'a', 'c', 'b', 'f'] >>> {x:randint (1,4)for x in sam原创 2017-11-06 19:17:38 · 388 阅读 · 0 评论 -
Python高级编程-如何实现反向迭代?
class Flotrange: def __init__(self,start,end,step=0.5): self.start=start self.end=end self.step=step def __iter__(self ):#正向 t = self.start while t<=sel原创 2017-11-05 17:32:52 · 268 阅读 · 0 评论 -
Python高级编程--如何实现可迭代对象和迭代器对象?(未完待续)
>>> l=[1,2,3,4] >>> s='abcdef' >>> for x in l:print x SyntaxError: Missing parentheses in call to 'print' >>> for x in l: print(x) 1 2 3 4 >>> for x in s : print (x) a b c d e f >>>原创 2017-11-05 17:31:10 · 268 阅读 · 0 评论 -
Python高级编程-如何实现用户的历史记录功能?
>>> from collections import deque >>> q=deque([],6) >>> q.append(1) >>> q deque([1], maxlen=6) >>> q.append(2) >>> q.append(3) >>> q.append(4) >>> q.append(5) >>> q.append(6) >>> q deque([1, 2, 3, 4,原创 2017-11-05 17:28:20 · 320 阅读 · 0 评论 -
Python高级编程-如何让字典保持有序?
d=[] d['jim']=(1,40) d['len']=(2,45) d['zil']=(3,49) for k in d:print k ....... from collections import OrderedDict d= OrderedDict() d['jim']=(1,40) d['len']=(2,45) d['zil']=(3,49) for k in d:print k原创 2017-11-05 17:26:38 · 273 阅读 · 0 评论 -
Python高级编程-如何根据字典中值得大小,对字典中的项排序
>>> from random import randint >>> { x:randint (60,100) for x in 'xyondfuh'} {'x': 91, 'y': 90, 'o': 67, 'n': 64, 'd': 70, 'f': 96, 'u': 72, 'h': 68} >>> d={ x:randint (60,100) for x in 'xyondfuh'} >>原创 2017-11-05 17:24:37 · 239 阅读 · 0 评论 -
Python高级编程-如何统计序列中元素的出现频度?
>>> from random import randint >>> data = [randint (0,20) for _ in range (30)] >>> data [0, 16, 15, 8, 2, 12, 1, 4, 7, 3, 9, 18, 11, 16, 5, 3, 19, 11, 18, 4, 9, 19, 2, 3, 7, 17, 1, 8, 9, 9] >>> c =dic原创 2017-11-05 17:23:02 · 276 阅读 · 0 评论 -
Python高级编程-如何在列表,字典,集合中根据条件筛选数据?
列表 1.用 filter过滤负数 from random import randint data=[randint(-20,20) for _ in range(10) ] data filter(lambda x:x>=0,data) 2.用列表解析 [ x for x in data if x>=0] 哪个更好? 用timeit测试时间 timeit filter(lambda x:原创 2017-11-05 17:21:14 · 323 阅读 · 0 评论 -
Python高级编程-如何为元祖中的每个元素命名,提高程序可读性?
NAME,AGE,SEX,EMAIL(4) student= ('jim',16,'male','jim123@.com') #name print student [NAME] #age if student[AGE]>16: pass #sex if student [SEX]=='male': pass student= ('jim',16,'male','jim123原创 2017-11-05 17:17:12 · 1245 阅读 · 0 评论 -
Python高级编程-如何读写文本文件?
>>> f=open ('py3test.txt','wt',encoding='utf8') >>> f.write('你好,我爱编程') 7 >>> f.close() >>> f=open ('py3test.txt','rt',encoding='utf8') >>> s=f.read() >>> print(s) 你好,我爱编程 >>>原创 2017-11-06 21:26:00 · 333 阅读 · 0 评论 -
Python高级编程-如何对字符串进行左,右,居中对齐?
>>> d={'dniudc':500.0,'jdsc':0.05,'kjdks':255,'jksdc':400.3,'djfvd':58} >>> d.keys() dict_keys(['dniudc', 'jdsc', 'kjdks', 'jksdc', 'djfvd']) >>> map(len,d.keys()) >>> max(map(len,d.keys())) 6 >>> w=原创 2017-11-06 21:24:38 · 667 阅读 · 0 评论 -
Python高级编程-如何将多个小字符串拼接成一个大的字符串?
方法一 >>> s1='jkhdsfhnvv' >>> s2='1546336' >>> s1+s2 'jkhdsfhnvv1546336' 方法二 >>> ';'.join(['abd','158','jdshf']) 'abd;158;jdshf' >>> ''.join(['abd','158','jdshf']) 'abd158jdshf' >>> l=['abc',125,58,'x原创 2017-11-06 21:22:28 · 420 阅读 · 0 评论 -
Python高级编程-如何判断字符串a是否是以字符串b开头或结尾?
>>> import os,stat >>> os.listdir('.') ['DLLs', 'Doc', 'haha.txt', 'hbcdfv.py', 'helloworld.py', 'hhh.py', 'include', 'itchat.pkl', 'jksv.py', 'Lib', 'libs', 'LICENSE.txt', 'man', 'NEWS.txt', 'python.原创 2017-11-06 21:21:42 · 981 阅读 · 0 评论 -
Python高级编程-如何拆分含有多种分隔符的字符串?
>>> s= 'ab;cd|efg|hi,jkl|mn\topq;rst,uvw\txyz' >>> s.split(';') ['ab', 'cd|efg|hi,jkl|mn\topq', 'rst,uvw\txyz'] >>> c=s.split(';') >>> map(lambda x:x.split('|'),c) >>> t=[] >>> map(lambda x: t.exten原创 2017-11-06 21:17:25 · 270 阅读 · 0 评论 -
Python高级编程-如何对迭代器进行切片操作?
>>> from itertools import islice >>> l=range(30) >>> l range(0, 30) >>> t= iter(l) >>> for x in islice(t,5,10): print (x) 5 6 7 8 9 >>> for x in t: print (x) 10 11 12 13 14 15 16 17原创 2017-11-06 19:19:13 · 948 阅读 · 0 评论 -
Python高级编程-如何在在一个for循环中迭代多个可迭代对象
>>> from random import randint >>> chinase = [randint(60,100) for _ in range (25)] >>> chinase [83, 62, 84, 84, 85, 80, 67, 97, 95, 99, 95, 76, 76, 89, 83, 80, 68, 94, 91, 91, 67, 61, 70, 83, 96] >>>原创 2017-11-05 17:36:28 · 503 阅读 · 0 评论