>>> print(dict(zip('abcd',range(4))))
{'a': 0, 'b': 1, 'c': 2, 'd': 3}
>>> print('ok') if a==1 else 'ko'
>>> a=1
>>> print('ok') if a==1 else 'ko'
ok
>>> def test(m):
return 'a' if m==1 else 'b'
>>> test(1)
'a'
>>> list1=((1,'a'),(2,'b'))
>>> print({x[0]:x[1] for x in list1})
{1: 'a', 2: 'b'}
>>> print({x:y for x in range(4) for y in range(10,14)})
{0: 13, 1: 13, 2: 13, 3: 13}
>>> {x:y for x,y in zip(range(4),range(10,14))}
{0: 10, 1: 11, 2: 12, 3: 13}
>>> dict(zip(list(range(4)), list(range(10,14))))
{0: 10, 1: 11, 2: 12, 3: 13}
>>> print({x:y for x in range(4) for y in range(4)})
{0: 3, 1: 3, 2: 3, 3: 3}
>>> import heapq
>>> nums=[19,2,39,100,80]
>>> print(heapq.nlargest(3,nums))
[100, 80, 39]
>>> print(heapq.nsmallest(3,nums))
[2, 19, 39]
>>> student=[{'names':'CC','score':100,'height':189},
{'names':'BB','score':10,'height':169},
{'names':'AA','score':80,'height':179}]
>>> print(heapq.nsmallest(2,student,key=lambda x:x['height']))
[{'names': 'BB', 'score': 10, 'height': 169}, {'names': 'AA', 'score': 80, 'height': 179}]
>>> print(heapq.nsmallest(3,student,key=lambda x:x['height']))
[{'names': 'BB', 'score': 10, 'height': 169}, {'names': 'AA', 'score': 80, 'height': 179}, {'names': 'CC', 'score': 100, 'height': 189}]
>>> list1=['to_pickle','to_records','to_sparse','to_sql','to_stats','update']
>>> print(filter(lambda x:x.startswith('update'),list1))
<filter object at 0x0000000002EB5EF0>
>>> import re
>>> print(filter(lambda x:re.findall(u'(to_)',x),list1))
<filter object at 0x0000000002EB5FD0>
>>>
list2=[{'aa':100,'bb':200},{'a1':300,'cc':400}]
>>> print(filter(lambda x:'aa' in x.keys(),list2))
<filter object at 0x0000000002EB5CF8>
>>> s=[1,[2,[3,4]]]
>>> res=[]
>>> def fun(s):
for i in s:
if isinstance(i,list):
fun(i)
else:
res.append(i)
>>> fun(s)
>>> print(fun(s))
None
>>> print(res)
[1, 2, 3, 4, 1, 2, 3, 4]
>>> print([x/2 for x in range(10) if x%2==0])
[0.0, 1.0, 2.0, 3.0, 4.0]
>>> print([x for x in range(30) if x%2==0 and x%6==0])
[0, 6, 12, 18, 24]
>>> print([x+1 if x>=5 else x*10 for x in range(10)])
[0, 10, 20, 30, 40, 6, 7, 8, 9, 10]
>>> print(res)
[1, 2, 3, 4, 1, 2, 3, 4]
>>> print(fun(s))
None
>>> print(res)
[1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4]
>>> list_of_list=[[1,2,3],[4,5,6],[7,8]]
>>> print([y for x in list_of_list for y in x ])
[1, 2, 3, 4, 5, 6, 7, 8]
#快速生成列表的写法
all = []
for x in list_of_list:
for y in x:
all.append(y)
print(all)
>>> options={'code':'utf-8'}
>>> base_headers={
'User_Agent':100,
'Accept-Encoding':'gzip,deflate,sdch',
'Accept-Language':'zh-CN,zh;q=0.8'
}
>>> headers=dict(base_headers,**options)
>>> print(headers)
{'User_Agent': 100, 'Accept-Encoding': 'gzip,deflate,sdch', 'Accept-Language': 'zh-CN,zh;q=0.8', 'code': 'utf-8'}
>>> base_headers.update(options)
>>> print(base_headers)
{'User_Agent': 100, 'Accept-Encoding': 'gzip,deflate,sdch', 'Accept-Language': 'zh-CN,zh;q=0.8', 'code': 'utf-8'}
>>>
>>>
珍藏的一些好的Python代码,技巧
最新推荐文章于 2020-12-20 21:01:31 发布
该博客转自简书,可能包含珍藏的Python代码与技巧相关内容,但原文仅给出链接,未展示具体信息。
2万+

被折叠的 条评论
为什么被折叠?



