日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
IteratorandGenerator-2

lambda表達(dá)式

匿名函數(shù)

# lambda  params:expression
# params : 可選參數(shù),逗號(hào)分割變量名
# expression:表達(dá)式,可以用條件語(yǔ)句,沒有return或yield

# 用法:
    # 簡(jiǎn)單計(jì)算:lambda x: x*2
    # 添加條件: lambda x :True if x > 8 else False
    # 多個(gè)參數(shù) lambda x, y : x+y
    # 用作排序: users.sort(key = lambda x(x[0], x[1]))
    # 默認(rèn)字典:①defaultdic(lambda:0)
                # ② defaultdict(lambda:'bj')
                # ③ defaultdic(lambda :(0, 0))
    # 用作參數(shù):① filter ② map
    # 要點(diǎn):① 語(yǔ)法糖,用來(lái)創(chuàng)建函數(shù)對(duì)象  ② 表達(dá)式不要太復(fù)雜
f = lambda x :x*2
f(3)
6
f = lambda x : True if x > 8 else False  # 在if前面的是要返回的值
f(3)
False
f(9)
True
f = lambda x, y : x + y
f(8,9)
17
users = [('coop', 23), ('lilei', 26), ('hmm', 15), ('zhangsan', 40)]
users.sort(key=lambda x: (x[0],x[1])) # sort中有參數(shù)key,按照key的值進(jìn)行排序,以x[0]
# 的值為key # 按照字母順序排序x[0]指出了是coop,lilei,hmm等
users
[('coop', 23), ('hmm', 15), ('lilei', 26), ('zhangsan', 40)]
users.sort(key=lambda x:(x[1],x[0]))  # 按數(shù)字大小排序
users
[('hmm', 15), ('coop', 23), ('lilei', 26), ('zhangsan', 40)]
from collections import defaultdict
d = defaultdict(lambda :0)  # 默認(rèn)賦值 等同于defaultdict(lambda:int)
只寫出key,而沒有指定value,默認(rèn)從int中取值,即0,而不限與0
d['a']
0
d
defaultdict(()>, {'a': 0})
d['b']
0
d
defaultdict(()>, {'a': 0, 'b': 0})
d['c'] = 'coop'
d
defaultdict(()>, {'a': 0, 'b': 0, 'c': 'coop'})
city = defaultdict(lambda : 'bj')
city
defaultdict(()>, {})
city['shanghai']
'bj'
city
defaultdict(()>, {'shanghai': 'bj'})
city['coop']
'bj'
city
defaultdict(()>, {'shanghai': 'bj', 'coop': 'bj'})
point = defaultdict(lambda : (0, 0))
point[1]
(0, 0)
point[0]
(0, 0)
point['pp'] = (1,2)
point
defaultdict(()>,
            {1: (0, 0), 0: (0, 0), 'pp': (1, 2)})

有些代碼----一行搞定

# all(iterable)   Return True if all elements of the iterable are true (or if the iterable is empty). 都是真 
# any(iterable)Return True if any element of the iterable is true. If the iterable is empty, return False   任意
# min
# max
# sum
# reversed
# sorted
# zip  # 對(duì)應(yīng)合并
# []列表  列表推到是
# ()生成器   生成器
# {k, v}    字典推倒
# set/{}    去重
# if 三元操作符   xx  if x else y
# enumerate   返回枚舉,帶索引
m = [1,2,3,0]
all(m)  # 可能是函數(shù)等
"""Signature: all(iterable, /)
Docstring:
Return True if bool(x) is True for all values x in the iterable
括號(hào)里面必須是可迭代的對(duì)象,如果可迭代對(duì)象里面的每一個(gè)都是True,則返回True
"""
False
m = [1,2,3,4,5]
all(m)
True
def f1():
    return True
def f2():
    return False
def f3():
    return True
all([f1(), f2(), f3()])  # [],可迭代的對(duì)象
False
# any
any([f1(), f2(), f3()])
True
m
[1, 2, 3, 4, 5]
min(m)
1
max(m)
5
sum(m)
"""Signature: sum(iterable, start=0, /)
Docstring:
Return the sum of a 'start' value (default: 0) plus an iterable of numbers
"""
"Signature: sum(iterable, start=0, /)\nDocstring:\nReturn the sum of a 'start' value (default: 0) plus an iterable of numbers\n"
sum(m)
15
reversed(m)
"""Init signature: reversed(self, /, *args, **kwargs)
Docstring:     
reversed(sequence) -> reverse iterator over values of the sequence

Return a reverse iterator"""
'Init signature: reversed(self, /, *args, **kwargs)\nDocstring:     \nreversed(sequence) -> reverse iterator over values of the sequence\n\nReturn a reverse iterator'
reversed(m)
for i in reversed(m):
    print(i)
5
4
3
2
1
[i for i in reversed(m)]
[5, 4, 3, 2, 1]
[i for i in reversed(range(8))]
[7, 6, 5, 4, 3, 2, 1, 0]
m
[1, 2, 3, 4, 5]
m = [2,3,90,5,4,34,22,55]
m.sort()
m
[2, 3, 4, 5, 22, 34, 55, 90]
m = [2,3,90,5,4,34,22,55]
sorted(m)
[2, 3, 4, 5, 22, 34, 55, 90]
sorted?
"""Signature: sorted(iterable, /, *, key=None, reverse=False)
Docstring:
Return a new list containing all items from the iterable in ascending order.

A custom key function can be supplied to customize the sort order, and the
reverse flag can be set to request the result in descending order.
Type:      builtin_function_or_method"""
m
[2, 3, 90, 5, 4, 34, 22, 55]
sorted(m, reverse=False)
[2, 3, 4, 5, 22, 34, 55, 90]
sorted(m, reverse=True)
[90, 55, 34, 22, 5, 4, 3, 2]
m1 = [-9,-3,-5,-7,3,4,6]
sorted(m1)
[-9, -7, -5, -3, 3, 4, 6]
sorted(m1, key = abs)
[-3, 3, 4, -5, 6, -7, -9]
zip?
"""Init signature: zip(self, /, *args, **kwargs)
Docstring:     
zip(iter1 [,iter2 [...]]) --> zip object

Return a zip object whose .__next__() method returns a tuple where
the i-th element comes from the i-th iterable argument.  The .__next__()
method continues until the shortest iterable in the argument sequence
is exhausted and then it raises StopIteration."""
x = [1,2,4,5,6,7,8]
y = ['q','b','c','d','e']
zip(x,y)
print(zip(x,y))
print(list(zip(x,y)))
[(1, 'q'), (2, 'b'), (4, 'c'), (5, 'd'), (6, 'e')]
print(dict(zip(x,y)))
{1: 'q', 2: 'b', 4: 'c', 5: 'd', 6: 'e'}
name = ['coop','hmm','lilei']
city = ['beijing','shanghai','shenzhen']
dict(zip(name,city))
{'coop': 'beijing', 'hmm': 'shanghai', 'lilei': 'shenzhen'}
# 列表推導(dǎo)式
[i for i in range(10)]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[i^2 for i in range(10)]
[2, 3, 0, 1, 6, 7, 4, 5, 10, 11]
# 生成器表達(dá)式()
(x**3 for i in range(8))
 at 0x0000012FCD4B73B8>
for y in (x**3 for x in range(8)):
    print(y)
0
1
8
27
64
125
216
343
# 字典推導(dǎo)式
info = dict(zip(name, city))
info
{'coop': 'beijing', 'hmm': 'shanghai', 'lilei': 'shenzhen'}
info_new = {}
for k, v in info.items():
    info_new[k] = v.upper()
print(info_new)
{'coop': 'BEIJING', 'hmm': 'SHANGHAI', 'lilei': 'SHENZHEN'}
{k:v.upper() for k, v in info.items()}  # 與上面同樣的效果
{'coop': 'BEIJING', 'hmm': 'SHANGHAI', 'lilei': 'SHENZHEN'}
# 去重set
s = [1,1,2,3,4,5,4,5,64,6,4,3,6]
set(s)
{1, 2, 3, 4, 5, 6, 64}
s
[1, 1, 2, 3, 4, 5, 4, 5, 64, 6, 4, 3, 6]
list(set(s))
[64, 1, 2, 3, 4, 5, 6]

三元操作符

if True:
    pass
else:
    pass
True if s =='a' else False
False
s
[1, 1, 2, 3, 4, 5, 4, 5, 64, 6, 4, 3, 6]
s = 'a'
True if  s =='a' else False 
True

分享題目:IteratorandGenerator-2
分享路徑:http://www.dlmjj.cn/article/jhecdi.html