赞
踩
- #1.10从序列中一处重复项且保持元素间顺序不变
- def dedupe(items):
- seen = set() #若序列中的值是可哈希的(不可变,整数浮点数字符串元组),那么这是可通过集合和生成器解决
- for item in items:
- if item not in seen:
- yield item #每次返回一个不再seen中的元素,其实就相当于返回给了调用外面的list,list是有插入顺序关系的`
- seen.add(item)
-
- a = [1,2,6,4,1,2,9,4,9]
- print(list(dedupe(a)))
- #在不可哈希对象中去除重复项,稍作修改即可
- def dedupe(items,key=None): #key的作用是指定一个函数用来将序列中元素转换为可哈希类型,这么做目的是为了检测重读对象
- seen = set()
- for item in items:
- val = item if item is None else key(item)
- if val not in seen:
- yield item
- seen.add(val)
-
- a = [{'x':1,'y':2},{'x':1,'y':3},{'x':1,'y':2},{'x':2,'y':4}]
- print(list(dedupe(a,key=lambda d:(d['x'],d['y'])))) #d['x']相等且d['y']相等
- print(list(dedupe(a,key=lambda d:d['x']))) #只按d['x']进行筛选
- def merge_dict(x,y):
- for k,v in x.items():
- if k in y.keys():
- y[k] += v
- else:
- y[k] = v
- a,b=0,1
- while i<100:
- print(b)
- a,b=b,a+b
- def test(number):
- def test_in(number_in):
- print('number_in%d'%number_in)
- return number + number_in
- return test_in
- ret = test(20) #给test函数赋值,这个20就是给参数number
- print(ret(200)) #这里的200给number_in
- print(ret(100))
- #返回
- #number_in200
- #220
- #number_in100
- #120
-
- def line_cof(a,b):
- def line(x):
- return a*x+b
- return line
- line1 = line_cof(1,1) #x+1
- line2 = line_cof(4,5) #4x+5
- print(line1(5))
- print(line2(5))
- #返回值
- #6
- #25
- def makebold(fn):
- def warp():
- return "<a>"+fn()+"<a>"
- return warp
- def makeitalic(fn):
- def warp():
- return "<b>"+fn()+"<b>"
- return warp
- @makebold #这句话相当于makebold(test1),也就是把当前函数传过去
- def test1():
- return "test1"
- @makeitalic
- def test2():
- return "test2"
-
- @makebold
- @makeitalic
- def test3(): #函数和装饰器是倒着执行的,从下往上,相当于makeold(makeitalic(test3))
- return "test3"
-
- print(test1())
- print(test2())
- print(test3())
- DIGITS={'0':0,'1':1,'2':2,'3':3,'4':4,'5':5,'6':6,'7':7,'8':8,'9':9}
- def str2float(s):
- s=s.split('.')
- if s[0]==0:
- return 0+reduce(lambda x,y:x/10+y , map(lambda x:DIGITS[x],s[1][::-1]))/10
- else:
- return reduce(lambda x,y:x*10+y,map(lambda x:DIGITS[x],s[0]))+reduce(lambda x,y:x/10+y , map(lambda x:DIGITS[x],s[1][::-1]))/10
- print(str2float('123.0456'))
- print(str2float('0.0456'))
- #注意上面小数点后面是逆置后元素然后进行x/10+y。最后返回各位是有值得,所以进行/10操作
- 1 li=[1,2,3,4,5,1,2,3]
- 2 new_li=[]
- 3 for i in li:
- 4 if i not in new_li:
- 5 new_li.append(i)
- 6 print(new_li)
- 1 li=[1,2,3,4,5,1,2,3]
- 2 new_li=list(set(li))
- 3 new_li.sort(key=li.index)
- 4 print(new_li)
- #1.13通过公共键对字典列表排序,根据一个或多个字典中的值来对列表排序
- #利用operator中的itemgetter函数对这类结构排序很简单
- rows = [
- {'fname': 'Brian', 'lname': 'Jones', 'uid': 1003},
- {'fname': 'David', 'lname': 'Beazley', 'uid': 1002},
- {'fname': 'John', 'lname': 'Cleese', 'uid': 1001},
- {'fname': 'Big', 'lname': 'Jones', 'uid': 1004}
- ]
- from operator import itemgetter
- rows_by_name = sorted(rows,key=itemgetter('fname')) #按照fname的大小对字典进行排序
- rows_by_uid = sorted(rows,key=itemgetter('uid')) #按照uid 对字典进行排序
- print(rows_by_name)
- print(rows_by_uid)
- #itemgetter()函数可以接受多个键
- rows_by_lfname = sorted(rows,key=itemgetter('lname','fname')) #按照lname,fname同时排序
- print(rows_by_lfname)
- #也可以自定义,用lambda函数取代itemgetter()函数的功能
- rows_by_name = sorted(rows,key=lambda x:x['fname']) #按照fname的值排序
- print(rows_by_name)
- rows_by_lfname = sorted(rows,key=lambda x:(x['lname'],x['fname']))
- print(rows_by_lfname)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。